您的位置:首页 > 其它

hibernate ID生成策略配置

2016-03-01 14:17 405 查看
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
identity
,
sequence
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数据库。

@SequenceGenerator(name=”teacherSEQ”,sequenceName=”teacherSEQ_DB”)加在类前



@GeneratedValue(strategy=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

)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: