无法使用hibernate一对一映射插入数据

时间:2014-02-23 23:27:06

标签: java hibernate one-to-one

我有两张桌子:

ptUSER(userID, name)
ptProteinData(userID, total, goal)

我尝试使用Hibernate一对一的关系来实现这一点。

这两个表的实现如下:

ptUser table (User.java)的实施:

@Entity
@Table(name = "ptuser")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "userID", unique = true, nullable = false)
    int userID;

    @Column(name = "name", unique = true, nullable = false, length = 45)
    String name;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "ptuser", cascade = CascadeType.ALL)
    ProteinData proteinData;

    // getters and setters

}

ptProteinData table (ProteinData.java)的实施:

@Entity
@Table(name = "ptproteindata")
public class ProteinData {
    @Id
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "ptuser"))
    int userID;
    int total;
    int goal;

    @OneToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    User user;

    //getter & setter
}

DAO图层实现如下所示:

public class UserDAO {

    public void addUserWithProteinData(String name, int goal, int total) {

        Session session = HibernateUtilities.getSessionFactory().openSession();
        session.beginTransaction();

        User user = new User();
        user.setName(name);

        ProteinData proteinData = new ProteinData();
        proteinData.setGoal(goal);
        proteinData.setTotal(total);

        user.setProteinData(proteinData);
        proteinData.setUser(user);

        session.saveOrUpdate(user);

        session.getTransaction().commit();
        session.close();
    }

}

我尝试使用以下代码插入数据:

UserDAO uDAO = new UserDAO();
uDAO.addUserWithProteinData("abc", 100, 10);

但我无法使用上述代码插入数据。你能告诉我我在做错的地方吗?当我尝试执行此操作时,我收到以下错误:

Exception in thread "main" 
java.lang.NullPointerException
    at mydomain.UserDAO.addUserWithProteinData(UserDAO.java:43)
    at mydomain.UserDAO.main(UserDAO.java:65)

2 个答案:

答案 0 :(得分:0)

将第一行添加到代码中,因为您需要先保存proteinData,然后在user对象中设置。最后,您可以保存user

session.saveOrUpdate(proteinData); // you need to save 'proteinData' first

user.setProteinData(proteinData);
proteinData.setUser(user);
session.saveOrUpdate(user);

同时修改这些

//在用户类

@Entity
@Table(name = "ptuser")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "userID")
    int userID;

    @Column(name = "name", unique = true, nullable = false, length = 45)
    String name;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "ptuser", cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    ProteinData proteinData;

}

//在ProteinData上

@Entity
@Table(name = "ptproteindata")
public class ProteinData {
    @Id
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator", strategy = "foreign", parameters =  @Parameter(name = "property", value = "ptuser"))
    int userID;
    int total;
    int goal;

    @OneToOne(fetch = FetchType.LAZY, mappedBy="ProteinData")
    @PrimaryKeyJoinColumn
    User user;

}

答案 1 :(得分:0)

添加 (可选=假)  @JoinColumn(name =“userID”,unique = true,nullable = false,updatable = false)

第二个表ProteinData

的主键是什么