How to map an auto increment field in hibernate?

17,214

Solution 1

In Annotation It work for me as

@GeneratedValue(strategy= GenerationType.IDENTITY)

for you hope it works

<generated-value strategy="IDENTITY" /> 

Solution 2

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.

<generator class="identity" />

See the these Hibernate docs for more information. According to it:

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

Solution 3

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

<generator class="increment"/>

Solution 4

<?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>
Share:
17,214
PeakGen
Author by

PeakGen

CTO

Updated on June 04, 2022

Comments

  • PeakGen
    PeakGen almost 2 years

    Please have a look at the below XML code

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

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

    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?