W3Schools Learner's Blog

w3Schools programming knowledge summary website

div

12/20/2017

Hibernate id generation strategy|The difference between native, assigned and UUID

Hibernate id generation strategy and the difference between native, assigned and UUID
1:the assigned primary key strategy needs to manual set up the ID  when the data is inserted, because it does not automatically generate ID.
  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC 
  3.    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4.    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6.    <class name="com.baidu.entity.Student" table="student">
  7.       <id name="id" column="id">
  8.           <generator class="assigned"></generator>
  9.       </id>
  10.       <property name="name" column="name"></property>
  11.       <property name="gender" column="gender"></property>
  12.       <property name="age" column="age"></property>
  13.    </class>
  14. </hibernate-mapping>

2:the native primary key strategy will automatically generate ID when the data is inserted, the entity class attribute is Number type.
  1. <id name="id" column="id">
  2.   <generator class="native"></generator>
  3. </id>
2:The uuid.hex primary key strategy automatically generates a 16 base UUID primary key, and the ID is not required when the data is inserted into the MySQL database, the entity class attribute is String type.
  1. <id name="id" type="java.lang.String">
  2. <column name="id" length="32" />
  3. <generator class="uuid.hex" />
  4. </id>

No comments:

Post a Comment

Note: only a member of this blog may post a comment.