在网上做了一些研究之后,我发现hibernate有一个bug> Hibernate让我们消失了一些例外或某事的原因;那是什么让我无法解决异常。
所以我有一个对象,我尝试使用hibernate merge()方法进行更新。 (坚持可以正常工作)
我之前在项目中遇到过这个问题几次,并且我的所有解决方案都无法解决之前的问题。
我也发现了很多类似的问题,但所有答案都与noRollBackFor=Exception.class
中的@Transactional
属性有关。
这是我想要阻止在我的代码中执行的操作,加上它在这种情况下不起作用。
我正在使用的数据库:MySQL
对象我尝试更新:
@Entity
@Table(name = "APPLICATIONS", uniqueConstraints = @UniqueConstraint(columnNames = { "NAME", "CUSTOMER_ID" }) )
public class ApplicationInfo extends DomainObject {
private static final long serialVersionUID = -5112065902722692463L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@NotNull
@NotBlank
private String name;
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "CUSTOMER_ID", nullable = false)
private Customer customer;
@Column(name = "GROUP_ID")
private String groupId;
@NotNull
@NotBlank
@Column(name = "ARTIFACT_ID")
private String artifactId;
@NotNull
@NotBlank
@Column(name = "TYPE")
private String type;
@Column(name = "DEPLOYMENT_GROUP_ID")
private String deploymentGroupId;
@NotNull
@Column(name = "DEPLOYMENT_ARTIFACT_ID")
private String deploymentArtifactId;
@Column(name = "APP_NODES")
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "application")
@LazyCollection(LazyCollectionOption.FALSE)
private Set<ApplicationNode> applicationNodes;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "application")
@OrderBy("sortKey ASC")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Stage> stages;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "application")
@LazyCollection(LazyCollectionOption.FALSE)
private List<ApplicationProperty> properties;
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "NEXUS_INFO_ID", nullable = true)
private NexusInfo nexusInfo;
// Getters and setters
save methoid:
@Override
@Transactional(propagation = Propagation.REQUIRED)
public T save(T t) {
try {
if (t.getId() == null) {
getEntityManager().persist(t);
} else {
t = getEntityManager().merge(t);
}
return t;
} catch (Exception e) {
log.error(e);
}
return t;
}
如果我有什么问题,请纠正我/如果您需要更多信息请问我。
domainObject的:
public abstract class DomainObject implements Serializable, Comparable<DomainObject> {
private static final long serialVersionUID = -2606953921071135629L;
public abstract Long getId();
public abstract void setId(Long id);
@Override
public int hashCode() {
if (getId() != null) {
return getId().hashCode();
} else {
return this.getClass().getCanonicalName().hashCode();
}
}
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof DomainObject) {
DomainObject dObj = (DomainObject) obj;
if (this.getId() != null) {
return this.getId().equals(dObj.getId());
}
}
return false;
}
public int compareTo(DomainObject obj) {
if (obj != null) {
if (this.getId() != null && obj.getId() != null) {
return this.getId().compareTo(obj.getId());
}
}
return 0;
}
}
例外情况: 你可以忽略它引用我的applicationcontroller,它只调用我的安全方法
2016-01-29 13:16:03 ERROR ApplicationsController:58 - java.lang.IllegalStateException: Multiple representations of the same entity [nl.ciber.deploy.metadata.model.ApplicationInfo#1] are being merged. Detached: [ApplicationInfo[id=1,name=MDS,customer=nl.ciber.deploy.metadata.model.Customer@603bdce,groupId=-,artifactId=-,type=-,deploymentGroupId=-,deploymentArtifactId=-,applicationNodes=[],stages=[],properties=[],nexusInfo=<null>]]; Detached: [ApplicationInfo[id=1,name=MDS,customer=nl.ciber.deploy.metadata.model.Customer@603bdce,groupId=-,artifactId=-,type=-,deploymentGroupId=-,deploymentArtifactId=-,applicationNodes=<null>,stages=<null>,properties=<null>,nexusInfo=<null>]]
jan 29, 2016 1:16:03 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [spring] in context with path [/mds] threw exception [Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly] with root cause
javax.persistence.RollbackException: Transaction marked as rollbackOnly
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:72)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:521)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy44.saveApplication(Unknown Source)
at nl.ciber.deploy.metadata.controller.ApplicationsController.editapp(ApplicationsController.java:150)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
提前感谢您的时间。