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.
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.baidu.entity.Student" table="student">
- <id name="id" column="id">
- <generator class="assigned"></generator>
- </id>
- <property name="name" column="name"></property>
- <property name="gender" column="gender"></property>
- <property name="age" column="age"></property>
- </class>
- </hibernate-mapping>
2:the native primary key strategy will automatically generate ID when the data is inserted, the entity class attribute is Number type.
- <id name="id" column="id">
- <generator class="native"></generator>
- </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.
- <id name="id" type="java.lang.String">
- <column name="id" length="32" />
- <generator class="uuid.hex" />
- </id>
No comments:
Post a Comment
Note: only a member of this blog may post a comment.