Hibernate Mapping Identity
Identity of an object is the property which is mapped to primary key of the row in the database. The id of the entity is mapped by @Id annotation@Entity @Table(name="Student") public class Student { //Id will correspond the primary key in the database private Long id; protected String name; ... //Id - Represents that it is a primary key column //GeneratedValue - How the key to be generated //column - Column to which this property is mapped @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="STUDENT_ID") public Long getId() { return id; } public void setId(Long id) { this.id = id; } ...
Hibernate supports many identifier generation strategy which supports all the major databases
Hibernate does not allows to change the Id once it has been set. It throws an exception if an attempt is made
student = new Student(); Long studentId = (Long)session.save(student); //student.setId(80L); - This will throw an exception
Right now JPA supports limited identifier generation strategy. So if the hibernate generation strategies need to be employed than do as follows:
@Entity @org.hibernate.annotations.GenericGenerator( name="hibernate-native", strategy="native") public class Student{ private Long id; protected String name; @Id @GeneratedValue(generator="hibernate-native") @Column(name="STUDENT_ID") public Long getId() { return id; }
It's possible to write one's own identifier generation strategy by implementing IdentifierGenerator interface. However try to use one of the existing generator as Hibernate already has very rich identifier generators.
Back to Hibernate Index
Back To Java Home
Back To Home
Sidebar
Last wiki comments
- AOP: Thanks
- Lalit Bhatt: Superb Collection
- Lalit Bhatt: J2EE training
- Introduction to ORM: timberland shoes
- Introduction to ORM: jordan shoes
- Introduction to ORM: nike air max
- Pune Tourist Spots: KONARK PARK CLOSED
- jQuery Form Validations: Jquery Developer
- Spring Introduction: RG
- SOAP: Re: Assertion
Sidebar
Random Pages
- What markets work on?
- Why projects fail?
- Bharat Band - Jai ho
- The concept of Nation
- Don't hide complexity if it cannot be handled in a robustway
Last blog post comments
-
Bharat Band - Jai ho: How do we protest?
Wed 18 of Aug., 2010 13:13 IST
-
Divided by Destiny: Contact
Fri 23 of July, 2010 16:02 IST
-
Future of Java: thesis writing
Sat 17 of July, 2010 01:50 IST
-
Hang till Death Mr. Kasab: some change
Mon 28 of June, 2010 16:03 IST
-
God Religion : Why we are confused?: Re: Is GOD Necessary?
Tue 15 of June, 2010 17:29 IST
-
God Religion : Why we are confused?: Is GOD Necessary?
Tue 15 of June, 2010 13:06 IST
-
The reason in religion: good
Wed 10 of Mar., 2010 18:30 IST
-
The confusion of Design Patterns: I think at macro level you are right...
Tue 23 of Feb., 2010 03:31 IST
-
The Indian Municipality: Comment
Fri 22 of Jan., 2010 13:20 IST
-
What Government should do?: Re: Review of the Indian Law and Order and Justice Dispensation regime.
Fri 22 of Jan., 2010 13:16 IST
Post new comment