Skip to content

Inserting, updating, deleting records

ahmetalpbalkan edited this page Jun 5, 2011 · 3 revisions

As described in the article Making your old Java classes ORMAN Entities, our @Entity-annotated classes extend Model class most of the time.

Model class offers you CRUD (create, read, update, delete) operations on the entity instance. Recall the Student entity from that article, we'll add a @PrimaryKey field to it;

class Student extends Model<Student>{
    public int id;
    public String name;
    public Date registrationDate;
    public float gpa;

Let's play with it:

Student s = new Student(); = "John Doe";

With insert() method, we have just saved our s instance to the database and database generated some auto-increment number for id field and the framework binded that value to id field. Now our entity is persisted.

Let's change something and update its database record:

s.gpa = 4.00;
s.registrationDate = new Date();

We've just replaced old null values for gpa and registrationDate with new values. Now the instance is persisted Now we can delete this object just by saying


Let's say we haven't deleted the object but we changed its id to 5. Now we have a dirty (unsaved, detached) instance. We can't use this instance for other queries such as delete() (because it will delete item with id=5, which is not safe) and it will throw UnableToPersistDetachedEntityException if we do so. Similarly, if you want to update a non-inserted record, you'll again get UnableToPersistDetachedEntityException.

Here's how we create/update/delete records (entity instances).

Something went wrong with that request. Please try again.