I am trying to use Hibernate with MySQL. Here is some more information about my software:
IDE: InteliJ IDEA Spring MVC TomCat Server Maven Hibernate
I've got a Family entity, and a User entity. Each user is going to be part of a 'family unit', so I need to be able to retrieve the family id of each user to show a family tree of sorts. However, no matter what I do, I can't get the familyId to return anything besides 0 or null. Here's the code I have so far:
Home Controller
@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;
}
FamilyEntity
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>
The issue is that Hibernate generated my FamilyEntity.hbm.xml incorrectly. Where it had:
<id name="familyid">
<column name="familyid" sql-type="int(10) unsigned zerofill"/>
</id>
It should have had:
<id name="familyid" type="int" column="familyid">
<generator class="native" />
</id>
in the second example, use int instead of int(10). column should be the column name, the same value you see in "name='' " in the first example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With