Wednesday, April 23, 2014

pojo id in hibernate

  • Suppose you are developing a course management system for a training center.
The first class you create for this system is Course.
This class is called an entity class or a persistent class because it represents a  real-world entity and its instances will be persisted to a database.
Remember that for each entity class to be persisted by an ORM framework, a default constructor with no argument is required.


public class Course {
private Long id;
...
// Constructors, Getters and Setters
}

For each entity class, you must define an identifier property to uniquely identify an entity.
It’s a best practice to define an auto-generated identifier because this has no business meaning and thus won’t be changed under any circumstances.
this identifier will be used by the ORM framework to determine an entity’s state.
If the identifier value is null, this entity will be treated as a new and unsaved entity.
When this entity is persisted, an insert SQL statement will be issued; otherwise, an update statement will.
To allow the identifier to be null, you should choose a primitive wrapper type like
java.lang.Integer and java.lang.Long for the identifier

Spring Recipes: A Problem-Solution Approach

No comments:

Post a Comment