hibernate 生成器本机类不会增加我的 user_id 列
我正在使用以下休眠映射文件
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试
我希望它能解决问题。
try
I hope it solves the problem.
我以前从未使用过这个,但在规范中,本机选项解释如下。
这对我来说有点模糊。
我所理解的是取决于数据库,它会选择不同的ID,并且数据库物理上必须具有对象,例如身份或序列。
对我来说,我使用的是 Oracle,所以我具体使用
如果您使用一个特定的数据库,请使用更具体的类。
希望这会有所帮助。
I never used this before but in the spec,Native option explains lie below.
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
If you are using one sepecific DB, please use more specific class.
Hope this would help.
连接到 Sybase 数据库时生成错误。
为此,我们尝试使用
generated errors while connecting to Sybase Database.
For that purpose, we tried using