【发布时间】: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>
【问题讨论】:
标签: java mysql spring hibernate maven