hibernate ID生成策略配置

1.Student.hbm.xml配置

<hibernate-mapping package="com.wxh.hibernate.model">
	<class name="Student" >
		<id name="id">
		<generator class="uuid"></generator>
		</id>
		<property name="age"></property>
		<property name="name"></property>
	</class>
</hibernate-mapping>

uuid

uses a 128-bit UUID algorithm to generate identifiers of type string that are unique within a network (the IP address is used). The UUID is encoded as a string of 32 hexadecimal digits in length.


id的类型要为String类型,最终在mysql中产生的id为varchar类型。


<hibernate-mapping package="com.wxh.hibernate.model">
	<class name="Student" >
		<id name="id">
		<generator class="uuid"></generator>
		</id>
		<property name="age"></property>
		<property name="name"></property>
	</class>
</hibernate-mapping>

native

selects identitysequence or hilo depending upon the capabilities of the underlying database.


2.Annotation配置_IDENTITY_SEQUENCE

@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public int getId() {
		return id;
	}


Sequence生成器,使用oracle数据库。

@SequenceGeneratorname=”teacherSEQ”,sequenceName=”teacherSEQ_DB”)加在类前

 

@GeneratedValuestrategy=GenerationType.SEQUENCE,generator=”teacherSEQ”)加在方法前


3.TableGenerator(跨数据库平台)

@javax.persistence.TableGenerator(

name="Teacher_GEN",

table="GENERATOR_TABLE",

pkColumnName="pk_key",

valueColumnName="pk_value",

pkColumnValue="Teacher",

allocationSize=1

)


你可能感兴趣的:(Hibernate)