我正在尝试在数据库中持有一个实体类,为此我尝试了几个不同的类实现,但仍然面临错误(但不同的错误).我的实体类现在是这样的:
@Entity
@Table(name="pagina")
public class Pagina extends Entidade {
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer Id;
@Column(name = "nome",unique=true)
private String nome;
@Column(name = "titulo")
private String titulo;
@Column(name = "descricao")
private String descricao;
@OneToOne( cascade = {CascadeType.ALL},fetch = FetchType.EAGER )
@JoinColumn(name="parent_page")
@Cascade(org.hibernate.annotations.CascadeType.ALL)
private Pagina pagina;
@OneToOne( cascade = {CascadeType.ALL},fetch = FetchType.EAGER )
@JoinColumn(name="parent_product")
@Cascade(org.hibernate.annotations.CascadeType.ALL)
private Produto produto;
}
在我的Dao类中,我为我的方法persist(…)尝试了这两个实现:
@Transactional
public boolean persist(E transientInstance) {
try {
sessionFactory.getCurrentSession().persist(transientInstance);
return true;
} catch (RuntimeException e) {
e.printStackTrace();
return false;
}
}
和
@Transactional
public boolean persist(E transientInstance) {
try {
sessionFactory.getCurrentSession().save(transientInstance);
return true;
} catch (RuntimeException e) {
e.printStackTrace();
return false;
}
}
使用这两个选项,如果我尝试保存一个没有属性Pagina或Produto的实体,它就会存储在数据库中而没有任何问题.但是,如果我尝试使用此属性提交数据,我会遇到问题.
使用第一个选项,异常org.hibernate.PersistentObjectException:传递给persist的分离实体被触发,而使用第二个选项,数据存储在数据库中,但是有错误,因为没有值的额外行存储在数据库,我插入的数据行指向该行.
谁知道这里发生了什么?因为我不知道.
PS:
我有这个视图在该表中插入数据:
Box" name="Pagina.id" value="2">two
Box" name="Pagina.id" value="1">one
由这种方法处理:
在控制器类中
@RequestMapping(value="cadastra",method=RequestMethod.POST)
@ResponseBody
public String cadastra(@modelattribute("object") E object,BindingResult result) {
if(serv.cadastra(object))
return "yes";
else
return "not";
}
在服务类中
@PreAuthorize("hasPermission(#user,'cadastra_'+#this.this.name)")
@Transactional
public boolean cadastra(E e) {
return dao.persist(e);
}
UPDATE
stacktrace与我尝试插入实体时发生的错误:
org.hibernate.PersistentObjectException: detached entity passed to persist: com.spring.loja.model.pagina.persistence.model.Pagina
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:139)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:801)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:794)
at org.hibernate.engine.spi.CascadingActions$7.cascade(CascadingActions.java:314)
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118)
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:432)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
at com.spring.loja.config.generic.persistence.Dao.persist(Dao.java:33)
at com.spring.loja.config.generic.persistence.Dao$$FastClassBySpringCGLIB$$ddbbe880.invoke(logout.logoutFilter.doFilter(logoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
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.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.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Jul 13,2014 9:07:48 PM org.hibernate.AssertionFailure Failed; nested exception is org.hibernate.AssertionFailure: null id in com.spring.loja.model.pagina.persistence.model.Pagina entry (don't flush the Session after an exception occurs)] with root cause
org.hibernate.AssertionFailure: null id in com.spring.loja.model.pagina.persistence.model.Pagina entry (don't flush the Session after an exception occurs)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:77)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:192)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:152)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:231)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:102)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:555)
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:478)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at com.spring.loja.model.pagina.service.PaginaService$$EnhancerBySpringCGLIB$$71a378e.cadastra(logout.logoutFilter.doFilter(logoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
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.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.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
这个错误可以像这样解释(ouf,如何在不描述Hibernate / JPA的整个对象生命周期的情况下这样做?):
使用JPA / Hibernate,您可以将类定义为Entitys.这意味着该类的对象可以由Hibernate管理,因此可以存储在数据库中.
@Entity
public class Entity { /** ... */ }
当你创建一个实体(新实体())类的新对象时,Hibernate对它一无所知.它不存储在数据库中,并且不受任何方式的Hibernate控制.如果我们想要发生这些事情,我们需要坚持我们的对象(em.persist(entity)).当我们这样做时,对象状态存储在数据库中,而且,对象本身由Hibernate管理,这意味着Hibernate将跟踪对象的状态.此外,如果您已指定Hibernate生成id,则会为该对象分配一个id.
当您通过Hibernate从数据库加载对象时,此对象也将由Hibernate管理.新持久化或加载的对象都将具有id,并且都将由Hibernate管理.但是,对象和Hibernate之间的连接可以被打破!发生这种情况时,对象会分离.分离的对象实际上只是实体类的一个对象,它具有id设置,但不受Hibernate管理.
我们在分离对象时所拥有的选项与我们对托管对象或新对象的选项不同.与托管对象不同,不允许将分离的对象传递给persist-method,因为它们已经具有id,这意味着它们必须已经被保留!当想要获取分离的对象并使其受管时,合并方法就是要使用的方法. (http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#merge(T))
这将合并分离对象的状态与对象id对应的数据库状态.
调用与新对象(没有id)的合并将为其分配一个id并保存它,但要注意它不会对象传递作为参数做任何事情,而是返回托管对象.
public Entity saveDetached(final Entity entity) {
return em.merge(entity);
}
在您的情况下,解决方案将取决于几个因素,但如果引用的Pagina将始终是数据库中已存在的那个,我将从中删除持久化操作的级联,并继续使用em.persist()(合并有它的弱点..)如果它可能是新的有时您还需要添加一些逻辑来确定是否持久化(在持久拥有对象之前). (我也认为从数据库中加载Pagina和Produto并将它们分配给新的Pagina可能会有效.)
TL; DR
将分离的对象重新引入持久性上下文时,请使用EntityManager的merge-method,而不是persist-method.