Hibernate java.lang.NullPointerException
我已将嵌套集设置为我的实体。这是我的密码:Hibernate java.lang.NullPointerException,java,hibernate,spring-mvc,Java,Hibernate,Spring Mvc,我已将嵌套集设置为我的实体。这是我的密码: MyClassA: @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(na
MyClassA:
@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 + "]";
}
}
@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 + "]";
}
}
@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 + "]";
}
}
@SuppressWarnings("unchecked")
public List<MyClassA> findAllMyClassAs() {
Criteria criteria = createEntityCriteria();
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
return (List<MyClassA>) criteria.list();
}
MyClassC:
@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 + "]";
}
}
@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 + "]";
}
}
@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 + "]";
}
}
@SuppressWarnings("unchecked")
public List<MyClassA> findAllMyClassAs() {
Criteria criteria = createEntityCriteria();
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
return (List<MyClassA>) criteria.list();
}
当我试图在控制器中获取所有MyClassA对象时,我得到以下异常:
2016-09-28 10:10:46调试DefaultHandlerExceptionResolver:134-从处理程序[public org.springframework.http.ResponseEntity>com.example.controller.MyController.listAllMyClassA()]解析异常:java.lang.NullPointerException
2016-09-28 10:10:46调试调度员Servlet:989-无法完成请求
java.lang.NullPointerException
位于org.hibernate.engine.internal.StatefulPersistenceContext.getLoadedCollectionOwnerNull(StatefulPersistenceContext.java:756)
位于org.hibernate.event.spi.AbstractCollectionEvent.GetLoadedOwnerNull(AbstractCollectionEvent.java:75)
位于org.hibernate.event.spi.InitializeCollectionEvent。(InitializeCollectionEvent.java:36)
位于org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1931)
位于org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:559)
位于org.hibernate.collection.internal.AbstractPersistentCollection.WithTemporarySessionIneed(AbstractPersistentCollection.java:261)
位于org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:555)
位于org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:143)
位于org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:316)
位于com.example.model.MyClassB.hashCode(MyClassB.java:79)
位于java.util.HashMap.hash(HashMap.java:338)
位于java.util.HashMap.put(HashMap.java:611)
在java.util.HashSet.add处(HashSet.java:219)
位于java.util.AbstractCollection.addAll(AbstractCollection.java:344)
位于org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:344)
位于org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:251)
位于org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:238)
位于org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:211)
位于org.hibernate.loader.loader.endCollectionLoad(loader.java:1157)
位于org.hibernate.loader.loader.initializeEntitiesAndCollections(loader.java:1126)
位于org.hibernate.loader.loader.processResultSet(loader.java:973)
位于org.hibernate.loader.loader.doQuery(loader.java:921)
在org.hibernate.loader.loader.doQueryAndInitializeNonLazyCollections(loader.java:355)上
位于org.hibernate.loader.loader.doList(loader.java:2554)
位于org.hibernate.loader.loader.doList(loader.java:2540)
位于org.hibernate.loader.loader.listIgnoreQueryCache(loader.java:2370)
位于org.hibernate.loader.loader.list(loader.java:2365)
位于org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
位于org.hibernate.internal.SessionImpl.list(SessionImpl.java:1718)
位于org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)
位于com.example.dao.MyClassADaoImpl.findAllGroupQuestions(MyClassADaoImpl.java:42)
位于com.example.service.MyClassAImpl.findAllGroupQuestions(MyClassAImpl.java:41)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:497)
位于org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
位于org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
在org.springframework.aop.framework.ReflectiveMethodInvocation.procedue(ReflectiveMethodInvocation.java:157)上
位于org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
位于org.springframework.transaction.interceptor.TransactionSpectSupport.invokeWithinTransaction(TransactionSpectSupport.java:281)
位于org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
在org.springframework.aop.framework.ReflectiveMethodInvocation.procedue(ReflectiveMethodInvocation.java:179)上
位于org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
位于com.sun.proxy.$Proxy63.findAllGroupQuestions(未知来源)
位于gr.citystore.helios.controller.QuestionaireController.listAllGroupQuestions(QuestionaireController.java:212)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:497)
位于org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
位于org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
位于org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
位于org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
位于org.springframework.web.servlet.m