-
Notifications
You must be signed in to change notification settings - Fork 20
Property Annotation
Sai Pullabhotla edited this page Oct 2, 2017
·
1 revision
The Property
annotation is used to explicitly mark a field of an entity as a persistable field. By default, all fields of entity are treated as persistable, as long as they -
- have an accessor and a mutator method
- do not have
@Ignore
annotation
The Property
annotation allows for the following customization:
- Define the name of the property to be used in the Google Cloud Datastore. If a property name is not set explicitly, the field name will be used as the property name.
- Control whether or not a property is to be indexed. By default, all properties are indexed.
- Specify the optionality - whether or not to store the property if the value is
null
. By default, a property is always stored into the Datastore, even if its value is null.
In the example below -
- Field
firstName
is mapped to a property namedfname
. In addition, first name is not indexed. - Field
lastName
is mapped to a property namedlname
. - Field
birthDate
is mapped to a property nameddob
(date of birth). - Field
ethnicity
is marked asoptional
. This means, ifethnicity
isnull
, the entity manager does not write theethnicity
property to the Cloud Datastore.
import java.time.LocalDate;
import com.jmethods.catatumbo.Entity;
import com.jmethods.catatumbo.Identifier;
import com.jmethods.catatumbo.Ignore;
import com.jmethods.catatumbo.Property;
@Entity
public class Person {
@Identifier
private long id;
@Property(name = "fname", indexed = false)
private String firstName;
@Property(name = "lname")
private String lastName;
@Property(name = "dob")
private LocalDate birthDate;
@Ignore
private int age;
@Property(optional = true)
private String ethnicity;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public LocalDate getBirthDate() {
return birthDate;
}
public void setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEthnicity() {
return ethnicity;
}
public void setEthnicity(String ethnicity) {
this.ethnicity = ethnicity;
}
}
import com.jmethods.catatumbo.EntityManager;
import com.jmethods.catatumbo.EntityManagerFactory;
public class PersonDAO {
private static PersonDAO instance = new PersonDAO();
private EntityManager em;
public static PersonDAO getInstance() {
return instance;
}
private PersonDAO() {
em = EntityManagerFactory.getInstance().createDefaultEntityManager();
}
public Person create(Person person) {
return em.insert(person);
}
public Person update(Person person) {
return em.update(person);
}
public void delete(Person person) {
em.delete(person);
}
public Person read(long id) {
return em.load(Person.class, id);
}
}
import java.time.LocalDate;
import com.jmethods.catatumbo.EntityManagerException;
public class CreatePerson {
public static void main(String[] args) {
Person person = new Person();
person.setFirstName("John");
person.setLastName("Doe");
person.setBirthDate(LocalDate.of(1990, 2, 15));
try {
PersonDAO dao = PersonDAO.getInstance();
person = dao.create(person);
System.out.printf("Person entity with ID %d was created successfully", person.getId());
} catch (EntityManagerException e) {
e.printStackTrace();
}
}
}
The screenshot below illustrates the Person entity created in the Cloud Datastore:
Note that -
- Property names are not the same as field names, instead they are the names specified in the
Property
annotation. - Column heading
fname
is grayed out to indicate that thefname
property is not indexed - The property
ethnicity
does not exist becauseethnicity
isnull