-
Notifications
You must be signed in to change notification settings - Fork 45
Making your old java classes orman entities
In your programs, you model your real-world entities as Java classes. Therefore, in this framework you should have your database entities as Java objects. For example, we will have students in our database. In usual Java you should have something like:
class Student {
public int id;
public String name;
public Date registrationDate;
public float gpa;
}
In order to convert this POJO (plain old Java object) class to an ORMAN entity you need to add a few things.
@Entity
class Student extends Model<Student>{
public int id;
public String name;
public Date registrationDate;
public float gpa;
}
Let's understand what's going on here. Using @Entity annotation, you have indicated that this class is an entity for the framework. Extending the Model<?> class allows you to use many methods such as insert(), update(), delete() on your Student instances.
Note: Every entity should have
@Entityannotation, howeverModelextension provides helper methods for operations on the objects.
There you go. Your Java class is now an entity.
If you want to declare fields in your entity class as non-public (private, default, protected) you have to define public getter and setter methods. If you have a non-public field named gpa, you should have getter and setters named getGpa() and setGpa().
Note: For boolean fields, getter methods can be
getFieldNameorisFieldName.
For any field, getter and setter can have the same name as field name. e.g.
public float gpa()can be your getter method signature andpublic void gpa(float g)can be your setter.
Setters always have 1 parameter as argument and getters have no arguments.
If no suitable getter-setter