Two Methods for Automatic Generation of Hibernate primary key ID

Source: Internet
Author: User

Version: hibernate-3.2.7

One method: Use uuid to generate a unique primary key. In this way, if the Object id is null, a UUID is automatically generated during storage. If the id is not null, this record is located in the database and then updated. If not found, an exception is thrown.

Xxxclass. hbm. xml Code

  1. <Id name ="Id"Type ="Java. lang. String"Column ="ID">
  2. <Generator class ="Uuid. hex"/>
  3. </Id>

Xxxclass. java code

  1. Public class XXXClass {
  2. /** Id */
  3. Private String id;
  4. Public void setId (String id ){
  5. This. id = id;
  6. }
  7. Public String getId (){
  8. Return id;
  9. }
  10. }

Method 2: Use org. hibernate. id. UUIDHexGenerator generates a unique primary key. In this way, if the Object id is null, a UUID is automatically generated during storage. If the id is not null, this record is located in the database and updated. If no record is found, insert is executed.

Xxxclass. hbm. xml Code

  1. <Id name ="Id"Type ="Java. lang. String"Column ="ID">
  2. <Generator class ="AssignCopiedId"/>
  3. </Id>

Xxxclass. java code

  1. Public class XXXClass implements AssignedIdModel {
  2. /** Id */
  3. Private String id;
  4. Public void setId (String id ){
  5. This. id = id;
  6. }
  7. Public String getId (){
  8. Return id;
  9. }
  10. @ Override
  11. Public String getAssignedId (){
  12. Return assignedId;
  13. }
  14. Public void setAssignedId (String assignedId ){
  15. This. assignedId = assignedId;
  16. }
  17. }

Assignedidmodel. java code

  1. Public interface AssignedIdModel {
  2. Public String getAssignedId ();
  3. }

Assigncopiedid. java code

  1. Import org. hibernate. engine. SessionImplementor;
  2. Import org. hibernate. id. UUIDHexGenerator;
  3. Public class AssignCopiedId extends UUIDHexGenerator {
  4. Public Serializable generate (SessionImplementor session, Object obj ){
  5. If (obj instanceof AssignedIdModel
  6. & (AssignedIdModel) obj). getAssignedId ()! = Null
  7. & (AssignedIdModel) obj). getAssignedId (). trim (). length ()>0)){
  8. Return (AssignedIdModel) obj). getAssignedId ();
  9. } Else {
  10. Return super. generate (session, obj );
  11. }
  12. }
  13. }

Conclusion: compared with the two methods, the method 1 is relatively simple and can meet the general situation. It is used in many projects. Method 2 is used when method 1 cannot be processed, for example, when copying table records from other system databases.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.