hibernate 生成器本机类不会增加我的 user_id 列

发布于 2024-12-17 11:23:33 字数 1711 浏览 0 评论 0原文

我正在使用以下休眠映射文件

<class name="com.abdus.hibernate.UserTable" table="tbl_users">
    <meta attribute="class-description">
        This class contains the user details.
    </meta>
    <id name="userId" type="long" column="userId">
        <generator class="native" />
    </id>
    <property name="firstName" type="string" column="firstName" not-null="true" />
    <property name="lastName" type="string" column="lastName" not-null="true" />
    <property name="emailId" type="string" column="emailId" not-null="true" />
    <property name="password" type="string" column="password" not-null="true" />
</class>

这是我插入一条新记录的代码

public Long add(UserDomain userDomain) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    Long userId = null;
    try {
        transaction = session.beginTransaction();
        UserTable userTable = new UserTable();
        userTable.setFirstName(userDomain.getFirstName());
        userTable.setLastName(userDomain.getLastName());
        userTable.setEmailId(userDomain.getEmailId());
        userTable.setPassword(userDomain.getPassword());
        userId = (Long) session.save(userTable);
        System.out.println("userId returned is " + userId);
        transaction.commit();
        userTable.toString();
    } catch (Exception e) {
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
    return userId;
}

但是我看到的是每次插入一条记录时,它总是插入一个 user_id 为 1 的代码。这意味着数据库中总是只有一条 user_id 为 1 的记录。为什么 hibernate 不递增每次插入记录时user_id的值?

i am using the following hibernate mapping file

<class name="com.abdus.hibernate.UserTable" table="tbl_users">
    <meta attribute="class-description">
        This class contains the user details.
    </meta>
    <id name="userId" type="long" column="userId">
        <generator class="native" />
    </id>
    <property name="firstName" type="string" column="firstName" not-null="true" />
    <property name="lastName" type="string" column="lastName" not-null="true" />
    <property name="emailId" type="string" column="emailId" not-null="true" />
    <property name="password" type="string" column="password" not-null="true" />
</class>

And here is my code to insert a new record

public Long add(UserDomain userDomain) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    Long userId = null;
    try {
        transaction = session.beginTransaction();
        UserTable userTable = new UserTable();
        userTable.setFirstName(userDomain.getFirstName());
        userTable.setLastName(userDomain.getLastName());
        userTable.setEmailId(userDomain.getEmailId());
        userTable.setPassword(userDomain.getPassword());
        userId = (Long) session.save(userTable);
        System.out.println("userId returned is " + userId);
        transaction.commit();
        userTable.toString();
    } catch (Exception e) {
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
    return userId;
}

But what i see is every time i insert a record, it always gets inserted a user_id as 1. Meaning there is always only one record in the DB with user_id 1. Why does hibernate not increment the value of user_id every time i insert a record?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小霸王臭丫头 2024-12-24 11:23:33

尝试

<generator class="increment"/>

我希望它能解决问题。

try

<generator class="increment"/>

I hope it solves the problem.

怀念你的温柔 2024-12-24 11:23:33

我以前从未使用过这个,但在规范中,本机选项解释如下。

它根据数据库选择身份、序列或 hilo。

这对我来说有点模糊。

我所理解的是取决于数据库,它会选择不同的ID,并且数据库物理上必须具有对象,例如身份或序列。

对我来说,我使用的是 Oracle,所以我具体使用

 <generator class="sequence">
            <param name="sequence">SEQUENCE_NAME</param>
 </generator>

如果您使用一个特定的数据库,请使用更具体的类。
希望这会有所帮助。

I never used this before but in the spec,Native option explains lie below.

It picks identity,sequence,or hilo,depending on the database.

It is somehow vague to me.

What I understnad is depending on the database, it would pick different ids and the database physically has to have the object,for example, identity or sequence.

For me, I am using Oracle so I sepecifically using

 <generator class="sequence">
            <param name="sequence">SEQUENCE_NAME</param>
 </generator>

If you are using one sepecific DB, please use more specific class.
Hope this would help.

Hello爱情风 2024-12-24 11:23:33
Finally i resolved this. I had this in the hibernate.cfg.xml file <property name="hbm2ddl.auto">create</property>

This was causing the schema to be created every time. So i did two things to resolve my issue
- remove the following <property name="hbm2ddl.auto">create</property>
- create the table with auto-increment
Finally i resolved this. I had this in the hibernate.cfg.xml file <property name="hbm2ddl.auto">create</property>

This was causing the schema to be created every time. So i did two things to resolve my issue
- remove the following <property name="hbm2ddl.auto">create</property>
- create the table with auto-increment
橘寄 2024-12-24 11:23:33
<generator class="native"/> 

连接到 Sybase 数据库时生成错误。

为此,我们尝试使用

<generator class="increment"/> 
<generator class="native"/> 

generated errors while connecting to Sybase Database.

For that purpose, we tried using

<generator class="increment"/> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文