JPA @OneToMany 和复合 PK

发布于 2024-08-30 12:59:33 字数 4317 浏览 5 评论 0原文

我正在从事 JPA 项目。我需要在具有三个主键的类上使用 @OneToMany 映射。您可以在此之后找到错误和类。

    javax.persistence.PersistenceException: No Persistence provider for EntityManager named JTA_pacePersistence: Provider named oracle.toplink.essentials.PersistenceProvider threw unexpected exception at create EntityManagerFactory: 
javax.persistence.PersistenceException
javax.persistence.PersistenceException: Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.EntityManagerSetupException
Exception Description: predeploy for PersistenceUnit [JTA_pacePersistence] failed.
Internal Exception: Exception [TOPLINK-7220] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [private java.util.Set isd.pacepersistence.common.Action.permissions] from the entity class [class isd.pacepersistence.common.Action] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referenceColumnName elements must be specified in each such @JoinColumn.
    at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:643)
    at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:196)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:110)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
    at isd.pacepersistence.common.DataMapper.(Unknown Source)
    at isd.pacepersistence.server.MainServlet.getDebugCase(Unknown Source)
    at isd.pacepersistence.server.MainServlet.doGet(Unknown Source)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:718)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
    at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:411)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:290)
    at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:271)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:202)

这是我的类的源代码:

Action :

@Entity
@Table(name="action")
public class Action {

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int num;

  @ManyToOne(cascade= { CascadeType.PERSIST, CascadeType.MERGE,
      CascadeType.REFRESH })
  @JoinColumn(name="domain_num")
  private Domain domain;

  private String name;
  private String description;

  @OneToMany
  @JoinTable(name="permission", joinColumns= { @JoinColumn(name="action_num", referencedColumnName="action_num", nullable=false, updatable=false) }, inverseJoinColumns= { @JoinColumn(name="num") })
  private Set<Permission> permissions;

  public Action() {
  }

Permission :

@SuppressWarnings("serial")
@Entity
@Table(name="permission")
public class Permission implements Serializable {

  @EmbeddedId
  private PermissionPK primaryKey;

  @ManyToOne
  @JoinColumn(name="action_num", insertable=false, updatable=false)
  private Action action;

  @ManyToOne
  @JoinColumn(name="entity_num", insertable=false, updatable=false)
  private isd.pacepersistence.common.Entity entity;

  @ManyToOne
  @JoinColumn(name="class_num", insertable=false, updatable=false)
  private Clazz clazz;

  private String kondition;

  public Permission() {
  }

PermissionPK :

@SuppressWarnings("serial")
@Entity
@Table(name="permission")
public class Permission implements Serializable {

  @EmbeddedId
  private PermissionPK primaryKey;

  @ManyToOne
  @JoinColumn(name="action_num", insertable=false, updatable=false)
  private Action action;

  @ManyToOne
  @JoinColumn(name="entity_num", insertable=false, updatable=false)
  private isd.pacepersistence.common.Entity entity;

  @ManyToOne
  @JoinColumn(name="class_num", insertable=false, updatable=false)
  private Clazz clazz;

  private String kondition;

  public Permission() {
  }

I am working on a JPA project. I need to use a @OneToMany mapping on a class that has three primary keys. You can find the errors and the classes after this.

    javax.persistence.PersistenceException: No Persistence provider for EntityManager named JTA_pacePersistence: Provider named oracle.toplink.essentials.PersistenceProvider threw unexpected exception at create EntityManagerFactory: 
javax.persistence.PersistenceException
javax.persistence.PersistenceException: Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.EntityManagerSetupException
Exception Description: predeploy for PersistenceUnit [JTA_pacePersistence] failed.
Internal Exception: Exception [TOPLINK-7220] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [private java.util.Set isd.pacepersistence.common.Action.permissions] from the entity class [class isd.pacepersistence.common.Action] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referenceColumnName elements must be specified in each such @JoinColumn.
    at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:643)
    at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:196)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:110)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
    at isd.pacepersistence.common.DataMapper.(Unknown Source)
    at isd.pacepersistence.server.MainServlet.getDebugCase(Unknown Source)
    at isd.pacepersistence.server.MainServlet.doGet(Unknown Source)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:718)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
    at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:411)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:290)
    at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:271)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:202)

Here is the source code of my classes :

Action :

@Entity
@Table(name="action")
public class Action {

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int num;

  @ManyToOne(cascade= { CascadeType.PERSIST, CascadeType.MERGE,
      CascadeType.REFRESH })
  @JoinColumn(name="domain_num")
  private Domain domain;

  private String name;
  private String description;

  @OneToMany
  @JoinTable(name="permission", joinColumns= { @JoinColumn(name="action_num", referencedColumnName="action_num", nullable=false, updatable=false) }, inverseJoinColumns= { @JoinColumn(name="num") })
  private Set<Permission> permissions;

  public Action() {
  }

Permission :

@SuppressWarnings("serial")
@Entity
@Table(name="permission")
public class Permission implements Serializable {

  @EmbeddedId
  private PermissionPK primaryKey;

  @ManyToOne
  @JoinColumn(name="action_num", insertable=false, updatable=false)
  private Action action;

  @ManyToOne
  @JoinColumn(name="entity_num", insertable=false, updatable=false)
  private isd.pacepersistence.common.Entity entity;

  @ManyToOne
  @JoinColumn(name="class_num", insertable=false, updatable=false)
  private Clazz clazz;

  private String kondition;

  public Permission() {
  }

PermissionPK :

@SuppressWarnings("serial")
@Entity
@Table(name="permission")
public class Permission implements Serializable {

  @EmbeddedId
  private PermissionPK primaryKey;

  @ManyToOne
  @JoinColumn(name="action_num", insertable=false, updatable=false)
  private Action action;

  @ManyToOne
  @JoinColumn(name="entity_num", insertable=false, updatable=false)
  private isd.pacepersistence.common.Entity entity;

  @ManyToOne
  @JoinColumn(name="class_num", insertable=false, updatable=false)
  private Clazz clazz;

  private String kondition;

  public Permission() {
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万劫不复 2024-09-06 12:59:33

早上好,

经过一整天的搜索 JPA 和 @OneToMany 如何与复合 PK 配合使用之后,我确实找到了解决方案。为了使其工作,我使用了@OneToMany的参数mappedBY。正如您在代码示例中看到的,我将权限集与类权限的属性操作进行了映射。就是这样!当你知道的时候就很简单了!

FF

操作类别:

@Entity
@Table(name="action")
public class Action {

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int num;

  @ManyToOne(cascade= { CascadeType.PERSIST, CascadeType.MERGE,
      CascadeType.REFRESH })
  @JoinColumn(name="domain_num")
  private Domain domain;

  private String name;
  private String description;

  @OneToMany(mappedBy="action")
  private Set<Permission> permissions;

权限类别

@SuppressWarnings("serial")
@Entity
@Table(name="permission")
public class Permission implements Serializable {

  @EmbeddedId
  private PermissionPK primaryKey;

  @ManyToOne
  @JoinColumn(name="action_num", insertable=false, updatable=false)
  private Action action;

Good morning,

After a long day searching how JPA and @OneToMany works with composite PK, I did find a solution. In order to make it work, I used the parameter mappedBY of @OneToMany. As you can see in the code sample, I mapped the Set of Permission with the attribute action of the class Permission. And that's it! Simple when you know it!

FF

Action Class :

@Entity
@Table(name="action")
public class Action {

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int num;

  @ManyToOne(cascade= { CascadeType.PERSIST, CascadeType.MERGE,
      CascadeType.REFRESH })
  @JoinColumn(name="domain_num")
  private Domain domain;

  private String name;
  private String description;

  @OneToMany(mappedBy="action")
  private Set<Permission> permissions;

Permission Class

@SuppressWarnings("serial")
@Entity
@Table(name="permission")
public class Permission implements Serializable {

  @EmbeddedId
  private PermissionPK primaryKey;

  @ManyToOne
  @JoinColumn(name="action_num", insertable=false, updatable=false)
  private Action action;
无法言说的痛 2024-09-06 12:59:33

错误消息看起来很清楚:您需要将复合 PK 的三列声明为 @JoinColum 并且必须指定 namereferenceColumnName对于每个。我没有测试映射,但尝试这个:

@OneToMany
@JoinTable(name="permission", joinColumns= {
    @JoinColumn(name="col1", referencedColumnName="col1", nullable=false, updatable=false),
    @JoinColumn(name="col2", referencedColumnName="col2", ...),
    @JoinColumn(name="col3", referencedColumnName="col3", ...)
}, inverseJoinColumns= { @JoinColumn(name="num") })
private Set<Permission> permissions;

The error message seems pretty clear: you need to declare the three columns of your composite PK as @JoinColum and the name and referenceColumnName must be specified for each. I didn't test the mapping but try this:

@OneToMany
@JoinTable(name="permission", joinColumns= {
    @JoinColumn(name="col1", referencedColumnName="col1", nullable=false, updatable=false),
    @JoinColumn(name="col2", referencedColumnName="col2", ...),
    @JoinColumn(name="col3", referencedColumnName="col3", ...)
}, inverseJoinColumns= { @JoinColumn(name="num") })
private Set<Permission> permissions;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文