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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28612610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:53:28  来源:igfitidea点击:

How to map an auto increment field in hibernate?

javamysqlxmlhibernatenetbeans

提问by Lemon Juice

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 Hibernatemapping class of my database table Family. We create the database separately using MySQL Work bench and then generate the mapping classes. We auto generated the mapping files using netbeans as mentioned in "Generating Hibernate Mapping Files and Java Classes"section of netbeans tutorial.

它是Hibernate我的数据库表的映射类Family。我们使用 MySQL Work bench 单独创建数据库,然后生成映射类。我们使用 netbeans 自动生成映射文件,如netbeans 教程的“生成 Hibernate 映射文件和 Java 类”部分所述。

Now we have a problem. That is, we changed the primary key(idFamily) of our table Familyto an auto generatedfield inside MySQL. Now, how can we change the above hibernate code so it identifies the idFamilyas an auto generated one?

现在我们有一个问题。也就是说,我们将表的primary key( idFamily)更改为MySQL 中的Family一个auto generated字段。现在,我们如何更改上面的休眠代码,使其识别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?

另一个问题是,手动编辑一个映射类而不通过工具重新生成所有映射会“破坏”系统吗?例如,喜欢搞乱关系?

回答by Niamath

In Annotation It work for me as

在注释中它对我有用

@GeneratedValue(strategy= GenerationType.IDENTITY)

for you hope it works

因为你希望它有效

<generated-value strategy="IDENTITY" /> 

回答by aryn.galadar

You're looking for an identitycolumn. That indicates that the column value is auto-generated as an identity for the row by the RDBMS.

您正在寻找标识列。这表示 RDBMS 自动生成列值作为行的标识。

<generator class="identity" />

<generator class="身份" />

See the these Hibernate docsfor more information. According to it:

有关更多信息,请参阅这些Hibernate 文档。根据它:

Identity supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.

Identity 支持 DB2、MySQL、MS SQL Server、Sybase 和 HypersonicSQL 中的标识列。返回的标识符的类型为 long、short 或 int。

回答by Dipen Adroja

Just replace your generator class to increment it will treat it as autoincrement

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

<generator class="increment"/>

回答by Giri Vrc

<?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>