【问题标题】:hibernate generator native class does not increment my user_id column休眠生成器本机类不会增加我的 user_id 列
【发布时间】:2012-01-04 00:38:45
【问题描述】:

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

<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 的记录。为什么休眠每次我都不会增加 user_id 的值插入记录?

【问题讨论】:

    标签: hibernate class generator native


    【解决方案1】:
    <generator class="native"/> 
    

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

    为此,我们尝试使用

    <generator class="increment"/> 
    

    【讨论】:

      【解决方案2】:
      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
      

      【讨论】:

        【解决方案3】:

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

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

        对我来说有点模糊。

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

        对我来说,我使用的是 Oracle,所以我特别使用

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

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

        【讨论】:

        • I am using MYSQL. I changed my table to create this way CREATE TABLE tbl_users(用户 ID 整数 NOT NULL AUTO_INCREMENT,名字 varcHAR(100) NOT NULL,姓氏 varcHAR(100) NOT NULL,emailid varcHAR(100) NOT NULL,密码 varcHAR(50) , PRIMARY KEY (userid) );` 并在映射文件&lt;generator class="increment" /&gt; 中写入以下内容,这在同一个JVM 中确实会增加,但是当服务器重新启动时,它会再次重置。 `
        • 身份选项怎么样。 stackoverflow.com/questions/1838520/…
        • 我也使用了身份。同样的问题“这确实在同一个 JVM 中增加,但是当服务器重新启动时,它再次被重置。”
        【解决方案4】:

        试试

        <generator class="increment"/>
        

        希望能解决问题。

        【讨论】:

        • 如果我在同一个 jvm 中重复插入,这将有效。如果我重新启动服务器,则增量值再次重置为 1。我注意到不仅如此,如果我在单个 JVM 中插入了 3 条记录,当我重新启动服务器并插入一条记录时,之前的所有 3 条记录都消失了,只有新记录存在。 hibernate很奇怪
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-06
        • 2011-12-26
        • 2015-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多