×
☰ See All Chapters

JPA Identifier Property

  • All entity classes must declare one or more fields which together form the persistent identity of an instance. Those fields are called as identifier fields/identifier properties. Entity identity tests whether two persistent objects represent the same state in the database. 

  • It is mapped to the primary key column of the database.  

  • Among the declared properties make at least one property as identifier property. This is done in xml mapping file (orm.xml) or using @Id annotations. In database it is not mandatory to have any constraint (primary key) on any column, but it is mandatory to have one identifier property in persistent class. But if there is a primary key constraint in DB table then, identifier property should be mapped to that column using xml mapping file (orm.xml) or using @Id annotations.  

  • When persistence class object is created, then the corresponding value of identifier property in the DB table is known as identity value.  

                          jpa-identifier-property-0
 
  • JPA identifies each persistence class object by using its identity value. 

  • When modification is done in persistence class object the JPA internally generates update query by taking identity value as criteria value to reflect the changes in relevant record of the DB table 

  • When modification is done in table row directly then JPA uses select query by taking persistence class object identity value to reflect that changes to the related java object. 

  • When changes are made to the identity property value either in DB table or in persistent object, JPA will keep synchronization. In this case JPA may even create new persistent object if necessary. 

  • We can have one or more variables as identifier properties.  

  • If only one member variable of persistence class is configured as identity field then it is called singular identity field. 

  • If multiple member variables of persistence class are configured as identity field then it is called composite identity field. 

  • If persistence class related DB table is having unique key constraint column then take that column related member variable in persistence class and configure that one as singular identity field. 

  • If persistence class related DB table is having composite primary key constraint on multiple columns, then take those multiple column related member variables of persistence class and configure them as composite identity field. 

  • If persistence class related DB table is having no constraints, programmer should analyze and guess multiple columns in which duplicate values will not be there, and then programmer has to take these multiple column members of persistence class to configure them as composite identity field. 


All Chapters
Author