zoukankan      html  css  js  c++  java
  • 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>


  • 相关阅读:
    454 Authentication failed, please open smtp flag first!
    zabbix 调用的发邮件脚本
    Apache Shiro 标签方式授权
    Realm [realm.ShiroDbRealm@15408475] does not support authentication token
    简单的zabbix agent自动安装脚本
    创建IPC端口失败:拒绝访问
    如何对报表的参数控件赋值
    如何对报表的参数控件赋值
    Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
    mysql 执行计划走分区
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7050344.html
Copyright © 2011-2022 走看看