在hibernate中实体的setter中的空指针异常

时间:2014-12-02 11:13:00

标签: java hibernate mapping setter one-to-one

我正在尝试映射User和ProteinData Entity之间的一对一关系,但在我的用户实体的setter中获取空指针异常。

我的主要服务代码 Program.java

public class Program {

    public static void main(String a[]){


        Session session=HibernateUtility.getSessionFactory().openSession();

        session.beginTransaction();
        User user=new User();
        user.addHistory(new UserHistory(new Date(),"set name to akku"));
        user.setName("akku");
        user.getProteinData().setTotal(234);



        user.addHistory(new UserHistory(new Date(),"set goal to 234"));
        session.save(user);
        session.getTransaction().commit();

        session.beginTransaction();




        User loadedeUser=(User)session.get(User.class,1);

        System.out.println(loadedeUser.getName());

        System.out.println(loadedeUser.getProteinData().getGoal());

        for(UserHistory history:loadedeUser.getHistory()){
            //System.out.println("key is: "+ history.getKey());
            System.out.println(history.getEntryTime().toString()+" "+history.getEntry());
        }

        loadedeUser.getProteinData().setTotal(loadedeUser.getProteinData().getTotal()+50);
        loadedeUser.addHistory(new UserHistory(new Date(),"added 32 proteins"));

        session.getTransaction().commit();

        session.close();
        HibernateUtility.getSessionFactory().close();
    }

}

用户实体

public class User {

    private int id;
    private String name;


    private ProteinData proteinData;

    private List<UserHistory> history=new ArrayList<UserHistory>();

    public User(){

        setProteinData(new ProteinData());
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public ProteinData getProteinData() {
        return proteinData;
    }


  public void setProteinData(ProteinData proteinData) {
        this.proteinData = proteinData;
        try{

        proteinData.setUser(this);  //Null Pointer Exception here

        }catch(Exception e){
            e.printStackTrace();
        }

    }

    public List<UserHistory> getHistory() {
        return history;
    }
    public void setHistory(List<UserHistory> history) {
        this.history = history;
    }

    public void addHistory(UserHistory historyItem){

        historyItem.setUser(this);

        history.add(historyItem);
    }

}

ProteinData类

 public class ProteinData {

        private int id;
        private User user;
        private int total;
        private int goal;

        public int getTotal() {
            return total;
        }
        public void setTotal(int total) {
            this.total = total;
        }
        public int getGoal() {
            return goal;
        }
        public void setGoal(int goal) {
            this.goal = goal;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public User getUser() {
            return user;
        }
        public void setUser(User user) {
            this.user = user;
        }
    }

我创建了一个setter,用于在User中设置proteinData变量的值,但在通过我的setter传递proteinData.addUser(this)时,我得到一个空指针异常

我该如何解决这个问题?

由于

2 个答案:

答案 0 :(得分:1)

在实体生命周期中,Hibernate会调用getter和setter,因此不能保证不会使用null值调用此setter。要避免此错误,只需检查参数是否为空

  public void setProteinData(ProteinData proteinData) {
      this.proteinData = proteinData;
      if (proteinData != null) {
          proteinData.setUser(this);
      }
  }

答案 1 :(得分:0)

因为在调用setter方法之前必须初始化蛋白质数据对象。希望这有帮助