我对实体之间的ManyToMany关系有疑问。添加工作非常好,但我无法删除公社。
以下是我的邀请:
public class Tuteur implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nom;
@ManyToMany(mappedBy="tuteurs",fetch=FetchType.EAGER)
private List<Commune> communes;
...
}
public class Commune implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nomCommune;
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="Tuteur_Commune",joinColumns=@JoinColumn(name="Commune_Fk",
referencedColumnName="id")
,inverseJoinColumns=@JoinColumn(name="Tuteur_Fk",referencedColumnName="id"))
private List<Tuteur> tuteurs;
...}
我有以下错误消息:
javax.faces.el.EvaluationException: javax.ejb.EJBException: Transaction aborted
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:636)
Caused by: javax.ejb.EJBException: Transaction aborted
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5046)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4805)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2004)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1955)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:198)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:84)
at $Proxy218.supprimer(Unknown Source)
at SessionTest.__EJB31_Generated__TestSession__Intf____Bean__.supprimer(Unknown Source)
at test.supprimer(test.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 32 more
Caused by: javax.transaction.RollbackException: Transaction marked for rollback.
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:450)
at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:837)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5040)
... 48 more
Caused by: Exception [EclipseLink-45] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing mapping for field [Commune.id].
Descriptor: RelationalDescriptor(entites.Commune --> [DatabaseTable(Commune)])
at org.eclipse.persistence.exceptions.DescriptorException.missingMappingForField(DescriptorException.java:951)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.extractValueFromObjectForField(ObjectBuilder.java:1970)
at org.eclipse.persistence.mappings.ManyToManyMapping.prepareTranslationRow(ManyToManyMapping.java:931)
at org.eclipse.persistence.mappings.ManyToManyMapping.earlyPreDelete(ManyToManyMapping.java:135)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1374)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitToDatabase(RepeatableWriteUnitOfWork.java:547)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1508)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.issueSQLbeforeCompletion(UnitOfWorkImpl.java:3128)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.issueSQLbeforeCompletion(RepeatableWriteUnitOfWork.java:268)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.beforeCompletion(AbstractSynchronizationListener.java:157)
at org.eclipse.persistence.transaction.JTASynchronizationListener.beforeCompletion(JTASynchronizationListener.java:68)
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:412)
... 50 more
答案 0 :(得分:2)
由于外键约束,当Commune链接到一个或多个Tuteur记录时,您无法删除它。您必须使用其中一个Cascade选项来删除相关记录(尽管这对于您的情况中的多对多关系是不常见的),或者您必须编写破坏关系的逻辑。
所以我猜你的情况是,正确的解决办法是首先清除Commune实例和所有引用的Tuteur实例之间的关系,然后删除Commune实例。
您可以通过编程方式执行此操作,如下所示:
for (Tuteur tuteur in commune.tuteurs) {
tuteur.getCommunes().remove(commune);
entityManager.persist(tuteur);
}
commune.getTuteurs().clear();
entityManager.persist(commune);
entityManager.remove(commune);
或者使用几个JPQL更新语句。
编辑:我重新读取了堆栈跟踪,其中包含以下根本原因:
Caused by: Exception [EclipseLink-45] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Missing mapping for field [Commune.id].
Descriptor: RelationalDescriptor(entites.Commune --> [DatabaseTable(Commune)])
现在,根据我的经验,EclipseLink有时会引发误导或混淆的异常。
我查看了您的@JoinTable
声明,但我注意到在您的特定情况下,referenceColumnName
选项可以省略,因为您的外键是目标的@Id
列两个方向的表。我不认为这是问题的原因,但你可能还是想尝试一下。
尝试的另一个选择是简单地让JPA和EclipseLink定义连接表。你可以完全省略@JoinTable
声明。除非您有特定的理由手动声明连接表,否则无论如何都要这样做。请记住,EJB 3.0中的许多改进都与约定优于控制有关,以便减少锅炉板代码和配置要求。