简体   繁体   English

如何在休眠中映射自动增量字段?

[英]How to map an auto increment field in hibernate?

Please have a look at the below XML code 请看下面的XML代码

<?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">
<!-- Generated Feb 17, 2015 10:01:43 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="model.main.Family" table="family" catalog="****" optimistic-lock="version">
        <id name="idFamily" type="int">
            <column name="idFamily" />
            <generator class="assigned" />
        </id>
        <many-to-one name="employee" class="model.main.Employee" fetch="select">
            <column name="idEmployee" not-null="true" />
        </many-to-one>
        <property name="firstName" type="string">
            <column name="FirstName" length="45" />
        </property>
        <property name="middleName" type="string">
            <column name="MiddleName" length="45" />
        </property>
        <property name="lastName" type="string">
            <column name="LastName" length="45" />
        </property>
        <property name="dob" type="date">
            <column name="DOB" length="10" />
        </property>
        <property name="passportNumber" type="string">
            <column name="PassportNumber" length="45" not-null="true" />
        </property>
        <property name="dateLeft" type="date">
            <column name="DateLeft" length="10" />
        </property>
        <property name="lastUpdated" type="timestamp">
            <column name="LastUpdated" length="19" not-null="true" />
        </property>
        <set name="visas" table="visa" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="idFamily" />
            </key>
            <one-to-many class="model.main.Visa" />
        </set>
    </class>
</hibernate-mapping>

It is the Hibernate mapping class of my database table Family . 它是数据库表FamilyHibernate映射类。 We create the database separately using MySQL Work bench and then generate the mapping classes. 我们使用MySQL Work Bench单独创建数据库,然后生成映射类。 We auto generated the mapping files using netbeans as mentioned in "Generating Hibernate Mapping Files and Java Classes" section of netbeans tutorial . netbeans教程的 “生成Hibernate映射文件和Java类”一节所述,我们使用netbeans自动生成了映射文件。

Now we have a problem. 现在我们有一个问题。 That is, we changed the primary key ( idFamily ) of our table Family to an auto generated field inside MySQL. 也就是说,我们将表Familyprimary keyidFamily )更改为MySQL内部的auto generated字段。 Now, how can we change the above hibernate code so it identifies the idFamily as an auto generated one? 现在,我们如何更改上面的休眠代码,以便将idFamily标识为自动生成的代码?

The other question is, manually editing one mapping class without regenerating all the mappings via a tool can "break" the system? 另一个问题是,手动编辑一个映射类而不通过工具重新生成所有映射可以“破坏”系统吗? For an example, like messing up with relationships? 例如,喜欢搞砸关系吗?

In Annotation It work for me as 在注释中它对我有用

@GeneratedValue(strategy= GenerationType.IDENTITY)

for you hope it works 为你希望它有用

<generated-value strategy="IDENTITY" /> 

You're looking for an identity column. 您正在寻找一个身分栏。 That indicates that the column value is auto-generated as an identity for the row by the RDBMS. 这表示RDBMS自动将列值生成为该行的标识。

<generator class="identity" /> <generator class =“ identity” />

See the these Hibernate docs for more information. 有关更多信息,请参见这些Hibernate文档 According to it: 根据它:

Identity supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. 身份支持DB2,MySQL,MS SQL Server,Sybase和HypersonicSQL中的身份列。 The returned identifier is of type long, short or int. 返回的标识符的类型为long,short或int。

只需替换您的生成器类以使其递增,它将被视为自动递增

<generator class="increment"/>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.tech.spring4.model.User" table="Customer">
<id name="id" type="long">
 <column name="USERID"  unique="true"/>
             <generator class="increment"/>
</id>
<property name="username"><column name="username" length="30" not-null="true"></column></property>
<property name="email"><column name="email" length="100" not-null="true"></column></property>
<property name="address"><column name="address" length="100" not-null="true"></column></property>
</class>
</hibernate-mapping>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM