• 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

    )


    Student.hbm.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.wxh.pojo">
    	<!-- name相应类名称 -->
    	<class name="Student" >
    		<!-- name相应属性名字 -->
    		<id name="stuNo">
    			<!--
    			ID生成策略:针对不同的数据库对象使用不同的id生成方式
    			identity:mysql,mssqlservler(整型列)
    			sequence:oracle,db2(整型列)
    			uuid:一般用于字符串类型的主键
    			guid:同上(一般用于mysql,sqlserver)
    			native:经常使用配置(依据不同的数据库自己主动选择使用identity,sequence。hilo中的当中一个作为生成策略)			
    			  -->		
    			<generator class="native">
    			<!-- 设置自己定义序列的名字 -->
    				<param name="sequence">auto_sid</param>
    			</generator>
    		</id>
    		<property name="name" column="stuname" length="16" not-null="true" unique="false">
    		</property>
    		<property name="major" length="32"></property>
    		<property name="comingYear"></property>
    		<property name="type" length="16"></property>
    	</class>
    </hibernate-mapping>

    Teacher.hbm.xml

    <?xml version="1.0" encoding="UTF-8"?

    > <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wxh.pojo"> <!-- name相应类名称 --> <class name="Teacher" > <!-- name相应属性名字 --> <id name="tid"> <!-- ID生成策略:针对不同的数据库对象使用不同的id生成方式 identity:mysql,mssqlservler(整型列) sequence:oracle,db2(整型列) uuid:一般用于字符串类型的主键 guid:同上(一般用于mysql,sqlserver) native:经常使用配置(依据不同的数据库自己主动选择使用identity,sequence,hilo中的当中一个作为生成策略) --> <generator class="native"> <!-- 设置自己定义序列的名字 --> <param name="sequence">auto_tid</param> </generator> </id> <property name="tname" length="16" not-null="true" unique="false"> </property> </class> </hibernate-mapping>


  • 相关阅读:
    Codeforces 1082 毛毛虫图构造&最大权闭合子图
    BZOJ 1003 最短路dp
    BZOJ 1002 生成树计数&高精度
    BZOJ 1001 平面图转对偶图
    BZOJ 世界树
    一般图极大团个数,最大团顶点数
    第十五届四川省省赛 SCU
    第十五届四川省省赛 SCU
    第十五届四川省省赛 SCU
    BZOJ4671异或图
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7050344.html
Copyright © 2020-2023  润新知