当前位置: 首页 > 知识库问答 >
问题:

Hibernate java.lang.NullPointerException

常彭薄
2023-03-14
@Entity
@Table(name = "aaa")
public class MyClassA {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    @NotEmpty
    @Size(min = 3, max = 255)
    @Column(name = "name", nullable = false)
    private String name;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<MyClassB> mycollection = new HashSet<MyClassB>();

    public MyClassA() {    
    }

    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 Set<MyClassB> getMyClassB() {
        return mycollection;
    }

    public void setMyClassB(Set<MyClassB> mycollection) {
        this.mycollection = mycollection;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((mycollection == null) ? 0 : mycollection.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MyClassA other = (MyClassA) obj;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (mycollection == null) {
            if (other.mycollection != null)
                return false;
        } else if (!mycollection.equals(other.mycollection))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "MyClassA [id=" + id + ", name=" + name + ", mycollection=" + mycollection + "]";
    }       
}

MyClassB:

@Entity
@Table(name = "bbb")
public class MyClassB {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    @NotEmpty
    @Size(min = 3, max = 255)
    @Column(name = "name", nullable = false)
    private String name;

    @NotEmpty
    @Size(min = 3, max = 255)
    @Column(name = "elementname", nullable = false)
    private String elementname;                

    @NotEmpty
    @Size(min = 3, max = 255)
    @Column(name = "type", nullable = false)
    private String type;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<MyClassC> mycollection = new HashSet<MyClassC>();

    public MyClassB() {    
    }

    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 String getElementname() {
        return elementname;
    }

    public void setElementname(String elementname) {
        this.elementname = elementname;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Set<MyClassC> getMyCollection() {
        return mycollection;
    }

    public void setMyCollection(Set<MyClassC> mycollection) {
        this.mycollection = mycollection;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((elementname == null) ? 0 : elementname.hashCode());
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
Line 79---> result = prime * result + ((mycollection == null) ? 0 : mycollection.hashCode());
        result = prime * result + ((type == null) ? 0 : type.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MyClassB other = (MyClassB) obj;
        if (elementname == null) {
            if (other.elementname != null)
                return false;
        } else if (!elementname.equals(other.elementname))
            return false;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (mycollection == null) {
            if (other.mycollection != null)
                return false;
        } else if (!mycollection.equals(other.mycollection))
            return false;
        if (type == null) {
            if (other.type != null)
                return false;
        } else if (!type.equals(other.type))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "MyClassB [id=" + id + ", name=" + name + ", elementname=" + elementname + ", type=" + type + ", mycollection=" + mycollection + "]";
    }       
}

myclassc:

@Entity
@Table(name = "ccc")
public class MyClassC {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    @NotEmpty
    @Size(min = 3, max = 255)
    @Column(name = "name", nullable = false)
    private String name;

    @NotEmpty
    @Size(min = 3, max = 255)
    @Column(name = "value", nullable = false)
    private String value;

    public MyClassC(){          
    }

    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 String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((value == null) ? 0 : value.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MyClassC other = (MyClassC) obj;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (value == null) {
            if (other.value != null)
                return false;
        } else if (!value.equals(other.value))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "MyClassC [id=" + id + ", name=" + name + ", value=" + value + "]";
    }

}

当我试图获取控制器中的所有MyClassA对象时,我得到以下异常:

@SuppressWarnings("unchecked")
public List<MyClassA> findAllMyClassAs() {
    Criteria criteria = createEntityCriteria();
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    return (List<MyClassA>) criteria.list();
}

最后,我的服务实现MyClassaImpl:

public List<MyClassA> findAllMyClassAs() {
        return dao.findAllMyClassAs();
    }

我的冬眠版本是4.3.11。最终版本。如果您需要任何其他信息,请告诉我!谢谢

共有1个答案

华谭三
2023-03-14

看看您的stacktrace,我相信这个问题的源头在您的hashCode方法中:

com.example.model.myclassb.HashCode(myclassb.java:79)在

尝试从hashCode方法中删除sets属性。我不认为这将是计算类的散列所必需的。在大多数情况下,您使用业务键(即标识对象的唯一属性)。

https://docs.jboss.org/hibernate/stable/core.old/referen/en/html/persistent-classes-equalshashcode.html

注意安全!:)

 类似资料:

相关问答

相关文章

相关阅读