Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 实体之间存在多个关系的问题_Java_Jpa_Jakarta Ee_Ejb 3.0 - Fatal编程技术网

Java 实体之间存在多个关系的问题

Java 实体之间存在多个关系的问题,java,jpa,jakarta-ee,ejb-3.0,Java,Jpa,Jakarta Ee,Ejb 3.0,我对实体之间的许多关系有问题。添加效果很好,但我无法删除公社 这是我的全部财产: public class Tuteur implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String nom;

我对实体之间的许多关系有问题。添加效果很好,但我无法删除公社

这是我的全部财产:

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;
...}

由于外键限制,当公社链接到一个或多个Tuteur记录时,无法删除该公社。您必须使用其中一个级联选项删除依赖记录(尽管在您的情况下,这对于多对多关系是不常见的),或者必须编写中断关系的逻辑

所以我猜在您的例子中,正确的解决方案是首先清除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 update语句

编辑:我重新读取了堆栈跟踪,其中包含以下根本原因:

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
声明来实现这一点。除非手动声明联接表有特定的原因,否则无论如何,这是一种方法。请记住,EJB3.0中的许多改进都与常规控制有关,以减少锅炉板代码和配置要求。

何时出现异常?你说的“note rmeove Commune”是什么意思?GRAVE:javax.ejb.ejb异常:事务中止,我无法删除Comune,我能说什么呢?非常感谢,我所做的只是删除了@JoinTable,它工作得很好,再次感谢你
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)])