Hibernate/Java:检索主键总是返回零
Posted
技术标签:
【中文标题】Hibernate/Java:检索主键总是返回零【英文标题】:Hibernate/Java: retrieving primary key always returns zero 【发布时间】:2017-10-23 09:19:54 【问题描述】:我正在尝试将 Hibernate 与 mysql 一起使用。以下是有关我的软件的更多信息:
IDE:InteliJ IDEA 春季MVC 汤姆猫服务器 马文 休眠
我有一个 Family 实体和一个 User 实体。每个用户都将成为“家庭单位”的一部分,因此我需要能够检索每个用户的家庭 ID 以显示各种家谱。但是,无论我做什么,我都无法让 familyId 返回 0 或 null 之外的任何内容。这是我到目前为止的代码:
家庭控制器
@RequestMapping (value = "/dashboard/admin/new", method = RequestMethod.POST)
public String newAdmin(@RequestParam("famName") String famName,
@RequestParam("fName") String fName,
@RequestParam("lName") String lName,
@RequestParam("email") String email,
@RequestParam("password") String password,
Model model)
FamiliesEntity family = newFamily(famName);
UsersEntity user = newAdmin(fName,lName,email,password,family.getFamilyid());
model.addAttribute("family", family);
model.addAttribute("user", user);
return "adminDashboard";
@RequestMapping ("/register/user")
public String registerUser()
return "newUser";
private FamiliesEntity newFamily(String famName)
Configuration configurationObject = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configurationObject.buildSessionFactory();
Session adminSession = sessionFactory.openSession();
Transaction familyTransaction = adminSession.beginTransaction();
FamiliesEntity newFamily = new FamiliesEntity();
newFamily.setName(famName);
// returns 0, should return 0
System.out.println("before save :" + newFamily.getFamilyid());
int testvalue = (Integer)adminSession.save(newFamily);
// both return 0, should return 30-something
System.out.println("after save :" + newFamily.getFamilyid());
System.out.println("after save :" + testvalue);
familyTransaction.commit();
// both return 0, should return 30-something
System.out.println("after commit :" + newFamily.getFamilyid());
System.out.println("after commit :" + testvalue);
return newFamily;
家庭实体
package com.grandcircus.spring.models;
import javax.persistence.*;
/**
* Class description
*
* @author Sarah Guarino
* @version 1.0
*/
@Entity
@Table(name = "families", schema = "checkin", catalog = "")
public class FamiliesEntity
@Id
@GeneratedValue
private int familyid;
private String name;
@Column(name = "familyid", nullable = false)
public int getFamilyid()
return familyid;
public void setFamilyid(int familyid)
this.familyid = familyid;
@Basic
@Column(name = "name", nullable = false, length = 45)
public String getName()
return name;
public void setName(String name)
this.name = name;
@Override
public boolean equals(Object o)
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FamiliesEntity that = (FamiliesEntity) o;
if (familyid != that.familyid) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return true;
@Override
public int hashCode()
int result = familyid;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
FamilyEntity.hbm.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.grandcircus.spring.models.FamiliesEntity" table="families" schema="checkin">
<id name="familyid">
<column name="familyid" sql-type="int(10) unsigned zerofill"/>
</id>
<property name="name">
<column name="name" sql-type="varchar(45)" length="45"/>
</property>
</class>
</hibernate-mapping>
【问题讨论】:
【参考方案1】:问题是 Hibernate 错误地生成了我的 FamilyEntity.hbm.xml。它在哪里:
<id name="familyid">
<column name="familyid" sql-type="int(10) unsigned zerofill"/>
</id>
应该有:
<id name="familyid" type="int" column="familyid">
<generator class="native" />
</id>
在第二个示例中,使用 int 而不是 int(10)。 column 应该是列名,与您在第一个示例中的“name=''”中看到的值相同。
【讨论】:
Hibernate 可以通过字段和 getter 方法注释来访问您的数据。
Hibernate 会根据 @Id 注解的位置来选择访问方法,你不能混合使用它们。如果使用@Id 注释字段,则方法上的注释将被忽略,反之亦然。
因此,您应该在字段上方或 getter 方法上方设置注释。
移动
@Id
@GeneratedValue
去getter
@Id
@GeneratedValue
@Column(name = "familyid", nullable = false)
public int getFamilyid()
return familyid;
实际上你不需要 FamilyEntity.hbm.xml
使用
SessionFactory factory = new AnnotationConfiguration().
configure().
//addPackage("com.xyz") //add package if used.
addAnnotatedClass(FamiliesEntity.class).
buildSessionFactory();
希望对你有帮助)
【讨论】:
它没有解决问题,但它仍然是很好的信息。谢谢!我希望我可以投票给你,但我还不能。以上是关于Hibernate/Java:检索主键总是返回零的主要内容,如果未能解决你的问题,请参考以下文章
Mybatis 在 insert 之后想获取自增的主键 id,但是总是返回1