Kos*_*asC 2 java hibernate spring-mvc
我已经将Set嵌套到我的实体中。这是我的代码:
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 + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
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 + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
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 + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在控制器中获取所有MyClassA对象时,出现以下异常:
2016-09-28 10:10:46调试DefaultHandlerExceptionResolver:134-从处理程序[公共org.springframework.http.ResponseEntity> com.example.controller.MyController.listAllMyClassA()]中解析异常:java.lang.NullPointerException 2016-09 -28 10:10:46 DEBUG DispatcherServlet:989-无法在org.hibernate.event.spi.AbstractCollectionEvent处完成请求org.hibernate.engine.internal.StatefulPersistenceContext.getLoadedCollectionOwnerOrNull(StatefulPersistenceContext.java:756)的java.lang.NullPointerException org.hibernate.event.spi.InitializeCollectionEvent。(InitializeCollectionEvent.java:36)处的.getLoadedOwnerOrNull(AbstractCollectionEvent.java:75)org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1931)处的org.hibernate.collection 。内部。org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:261)处的org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:555)处的AbstractPersistentCollection $ 4.doWork(AbstractPersistentCollection.java:559) org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:316)上的org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:143)在com.example.model.MyClassB.hashCode(MyClassB。的java.util.HashMap.hash(HashMap.java:338)的java:79)的java.util.HashSet.add(HashSet.java:219)的java.util.HashMap.put(HashMap.java:611)的Java java.util.AbstractCollection.addAll(AbstractCollection。org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:344)处org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:251)处org.hibernate.engine org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:211)的org.hibernate.loader.Loader.Loader.endCollectionLoad(Loader.java)的.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:238) :1157),位于org.hibernate.loader.Loader.doQuery(Loader.org)的org.hibernate.loader.Loader.processResultSet(Loader.java:973),位于org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1126)。 java:921)上的org.hibernate.loader.Loader。org.hibernate.loader.Loader.doList(Loader.java:2554)的org.hibernate.loader.Loader.doList(Loader.java:2540)的org.hibernate.loader.doList(Loader.java:2540)的org.hibernate.loader.Loader的doQueryAndInitializeNonLazyCollections(Loader.java:355) org.hibernate.loader.Loader.list(Loader.java:2365)的.listIgnoreQueryCache(Loader.java:2370)org.hibernate的org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)的.listIgnoreQueryCache(Loader.java:2370) com.example.dao.MyClassADaoImpl.findAllGroupQuestions(MyClassADaoImpl.java:42)上org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)上的internal.SessionImpl.list(SessionImpl.java:1718) .service.MyClassAImpl.findAllGroupQuestions(MyClassAImpl.java:41)位于sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法),位于sun.reflect。org.springframework.aop.support上java.lang.reflect.Method.invoke(Method.java:497)上的sun.reflect.DelegatingMethodAccessorImpl.invoke(NlegantMethodAccessorImpl.java:43)上的NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)的org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation。 org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99)在org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)在org.springframework.aop.framework.JdkDynamicAopProxy(org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)在org.springframework.aop.framework.JdkDynamicAopProxy .com.sun.proxy上的.invoke(JdkDynamicAopProxy.java:207).gr.citystore.helios.controller.QuestionaireController.listAllGroupQuestions(QuestionaireController.java:212)上的com.sun.proxy。$ Proxy63.findAllGroupQuestions(未知源),在sun.reflect.NativeMethodAccessorImpl。调用sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)上的invoke0(本机方法),调用java.lang.reflect.Method.invoke(Method。org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)处org.springframework.web.method.support.InvocableHandlerMethod.java:221)org.springframework.web.method.support.InvocableHandlerMethod.java org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)上的.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) org.org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)处的.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)上的springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet。 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)处org.springframework.web.servlet.FrameworkServlet处javax.servlet.http.HttpServlet.service(HttpServlet.java:622)处org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)的javax.servlet.http.HttpServlet.service(HttpServlet.java:729)的.service(FrameworkServlet.java:842) catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206),位于org.apache.catalina.core的org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52),位于org.apache.catalina.core的ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:316)上的.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor) .java:126),位于org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90),位于org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330) org.springframework.security.web.access。org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)处的ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114): 122)在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)在org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)在org.springframework.security org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java处的.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330):168)在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)在org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:48)在org.springframework.security org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter上的.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)在org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:205) (FilterChainProxy.java:330)在org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain。org处org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:96)处的doFilter(FilterChainProxy.java:330)org org.org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)上的.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)在org.springframework.web.filter.OncePerRequestFilter org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)的org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91的.doFilter(OncePerRequestFilter.java:107) )在org。org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)上的springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)在org.springframework.web上。 org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java)处的filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)在org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java: 213),位于org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344),位于org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344),位于org.springframework.web.filter。org上的org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)的org.org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)的DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261) org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)上的.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase .java:502)位于org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)位于org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)位于org.apache.catalina。 valve.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)在org.apache.catalina.connector.CoyoteAdapter.service(CocoteAdapter.java:521)在org.apache.coyote.http11.AbstractHttp11Processor org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:674)的.process(AbstractHttp11Processor.java:1096)org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.doRun(AprEndpoint.java:2500处的。 )在org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.run(AprEndpoint.java:2489)在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)在java.util.concurrent.ThreadPoolExecutor $ org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable上的Worker.run(ThreadPoolExecutor.java:617)。在java.lang.Thread.run(Thread.java:745)上运行(TaskThread.java:61)
MyClassADaoImpl:
@SuppressWarnings("unchecked")
public List<MyClassA> findAllMyClassAs() {
Criteria criteria = createEntityCriteria();
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
return (List<MyClassA>) criteria.list();
}
Run Code Online (Sandbox Code Playgroud)
最后,我的服务实现MyClassAImpl:
public List<MyClassA> findAllMyClassAs() {
return dao.findAllMyClassAs();
}
Run Code Online (Sandbox Code Playgroud)
我的Hibernate版本是4.3.11.Final。如果您需要其他任何信息,请告诉我!谢谢
查看您的stacktrace,我相信此问题的根源在您的hashCode方法中:
com.example.model.MyClassB.hashCode(MyClassB.java:79)在
尝试从您的hashCode方法中删除sets属性。我认为这对于计算类的哈希值不是必需的。在大多数情况下,您使用业务密钥(即标识对象的唯一属性)。
注意安全!:)
| 归档时间: |
|
| 查看次数: |
4016 次 |
| 最近记录: |