Hibernate / Java:检索主键始终返回零

时间:2017-05-23 12:13:01

标签: java mysql spring hibernate maven

我正在尝试将Hibernate与MySQL一起使用。以下是有关我的软件的更多信息:

IDE:InteliJ IDEA Spring MVC TomCat服务器 Maven的 冬眠

我有一个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;
}

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>

2 个答案:

答案 0 :(得分:3)

问题是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)。列应该是列名,您在&#34; name =&#39;&#39;中看到的值相同。 &#34;在第一个例子中。

答案 1 :(得分:1)

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();

我希望它有所帮助)