I started using Objectify
it as a library to work more comfortably with it Google Datastore
, Google App Engine
but I have a doubt and I don't know the difference between using Ref
and Key
in the definition of properties of my entity.
For example:
@Entity
public class Car {
@Id
Long id;
Key<User> driver;
}
either
@Entity
public class Car {
@Id
Long id;
Ref<User> driver;
}
Apparently both generate a relationship with the User entity but I don't understand what the difference is between using one or the other since when making queries or inserting data they behave exactly the same.
Does anyone know what the fundamental difference is?
The only difference between
Key <>
andRef <>
, is that theRef <>
is a container that alsoKey <>
contains a reference to the actual entity. It does not hold real references, the value will come from the entity that has been previously loaded into the session cache.With
Ref
andLoad
, you may have fewer calls to the database; It depends on your usage profile and the form of your data.ref is a wrapper, which includes the key class and makes the call to load the corresponding entity, either from the cache or by reading the database.
In most cases it is more comfortable to use Ref
Here and here are a couple very detailed explanations of their differences and the behavior of
Ref