-
Notifications
You must be signed in to change notification settings - Fork 246
Defining Entities
requery works by using abstract classes or interfaces to define the structure of an entity. These classes should be marked with the @Entity
annotation. Classes marked with this annotation will have a corresponding Entity class generated for them by the requery annotation processor. This generated class will inherit from the original class, and be used as the proxy class to store the state of the object. This is how requery avoids reflection.
Example:
@Entity
abstract class AbstractPerson {
@Key @Generated
int id;
String name;
String email;
Date birthday;
@OneToMany
Set<Phone> phoneNumbers;
@Transient
String link; // this field is not persisted because of @Transient
}
This will generate a Person.java
file in the same package as the source class with the required entity attributes and state.
Alternatively you can also define the entity as an interface:
@Entity
public interface Person {
@Key @Generated
int getId();
String getName();
String getEmail();
Date getBirthday();
@OneToMany
Result<Phone> getPhoneNumbers();
}
This will generate a file called PersonEntity.java
which contains the required attributes and implements the Person interface.
The name of the generated file can be customized by specifying the name()
property on the Entity
annotation. Whether to use abstract classes or interfaces is up to the user, requery is not opinionated about how those classes are defined. Use whatever best fits your use case.
A EntityModel is a grouping of Entity
types belonging to a specific database or schema. A file called Models.java is automatically generated that contains model definitions based on the entity classes you have defined. Use this model to create storage objects for working the model through a database. As described here.
This table illustrates the default class names when using @Entity on your domain classes. You can always specify the desired class name in the annotation. However below are the defaults for hypothetical 'User' entity when this isn't specified.
your class name | object type | generated name |
---|---|---|
AbstractUser | class | User |
BaseUser | class | User |
User | interface | UserEntity |
IUser | interface | User |
Use @Table
and @Column
to provide more mapping options than what can be defined than using just Classes and fields. These affect both query and table generation.
@Table(name = "")
@Entity
public class Person {
...
}
@Column(name = "_name", length = 32, unique = true, nullable = false)
String name;
@ForeignKey(update = ReferentialAction.CASCADE, referencedColumn = "_id")
@OneToOne
Address getAddress();
@Index("weight_index")
int getWeight();
@Index("name_index")
String getFirstName();
@Index("name_index")
String getLastName();
@Index("name_index")
@Column(unique = true)
String getFirstName();
@Index("name_index")
@Column(unique = true)
String getLastName();
See the javadocs for complete definitions.