我是靠谱客的博主 淡定自行车,最近开发中收集的这篇文章主要介绍关于hibernate的一对多ORM映射关系中一方Set中空指针异常解决方案,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


实体类:

@Entity
public class Owners {
    private Integer ownersId;
    private String ownersName;
    private String ownersAddress;
    private String ownersCity;
    private Set<Pets> petsSet;--------->要new 一个HashSet<Pets>出来

    public Owners() {
    }

    public Owners(String ownersName) {
        this.ownersName = ownersName;
    }

    @Id
    @Column(name = "ownersId", nullable = false)
    public Integer getOwnersId() {
        return ownersId;
    }

    public void setOwnersId(Integer ownersId) {
        this.ownersId = ownersId;
    }

    @Basic
    @Column(name = "ownersName", nullable = false, length = 20)
    public String getOwnersName() {
        return ownersName;
    }

    public void setOwnersName(String ownersName) {
        this.ownersName = ownersName;
    }

    @Basic
    @Column(name = "ownersAddress", nullable = false, length = 100)
    public String getOwnersAddress() {
        return ownersAddress;
    }

    public void setOwnersAddress(String ownersAddress) {
        this.ownersAddress = ownersAddress;
    }

    @Basic
    @Column(name = "ownersCity", nullable = false, length = 40)
    public String getOwnersCity() {
        return ownersCity;
    }

    public void setOwnersCity(String ownersCity) {
        this.ownersCity = ownersCity;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Owners owners = (Owners) o;

        if (ownersId != null ? !ownersId.equals(owners.ownersId) : owners.ownersId != null) return false;
        if (ownersName != null ? !ownersName.equals(owners.ownersName) : owners.ownersName != null) return false;
        if (ownersAddress != null ? !ownersAddress.equals(owners.ownersAddress) : owners.ownersAddress != null)
            return false;
        if (ownersCity != null ? !ownersCity.equals(owners.ownersCity) : owners.ownersCity != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = ownersId != null ? ownersId.hashCode() : 0;
        result = 31 * result + (ownersName != null ? ownersName.hashCode() : 0);
        result = 31 * result + (ownersAddress != null ? ownersAddress.hashCode() : 0);
        result = 31 * result + (ownersCity != null ? ownersCity.hashCode() : 0);
        return result;
    }

    @OneToMany(mappedBy = "owners")
    public Set<Pets> getPetsSet() {
        return petsSet;
    }

    public void setPetsSet(Set<Pets> petsSet) {
        this.petsSet = petsSet;
    }
}

测试类

@Test
 public void  petsAdd(){
     Session session= HibernateUtil.currentSession();
     Transaction tx=session.beginTransaction();
     PetsDao petsDao=new PetsDaoImpl();

     Pets pets=new Pets();
     Timestamp birth=Timestamp.valueOf("2017-1-13 12:00:00");
     pets.setPetsName("加菲");
     pets.setPetsBirthdate(birth);


    Pets pets1=new Pets();
    Timestamp birth1=Timestamp.valueOf("2016-1-13 12:00:00");
    pets1.setPetsName("灰熊");
    pets1.setPetsBirthdate(birth1);




     /*Types types=new Types();
     types.setTypesName("猫");
     types.getPetsSet().add(pets);*/


     Owners owners=new Owners();
     owners.setOwnersName("John");
     owners.getPetsSet().add(pets);
     owners.getPetsSet().add(pets1);


    pets.setOwners(owners);
    session.save(owners);
    tx.commit();
    HibernateUtil.closeSession();
 }


java.lang.NullPointerException

at com.lrq.test.TestPets.petsAdd(TestPets.java:53)


解决方案:将HashSet<Pets>中new 出 HashSet<Pets>




最后

以上就是淡定自行车为你收集整理的关于hibernate的一对多ORM映射关系中一方Set中空指针异常解决方案的全部内容,希望文章能够帮你解决关于hibernate的一对多ORM映射关系中一方Set中空指针异常解决方案所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部