在 EclipseLink-2.5.2 中为 Embeddable class 配置 ID 类型时出现奇怪问题

Strange issue while configuring ID types for Embeddable class in EclipseLink-2.5.2

在我当前的实现中,每个数据库 table 都有单独的实体 classes。我将 JPA 与 eclipselink-2.5.2 一起使用。这对我来说很好,但在某些时候当数据很大时,它会滞后。这就是我决定开始使用@Embedded、@Embeddable 和@EmbeddedId 的原因。在执行此操作时,我遇到了一个对我来说很奇怪的错误。这是发布的完整堆栈跟踪:https://gist.githubusercontent.com/tjDudhatra/b955812e0d1a71cf97f1/raw/11ea458869e24baae744530417ac99bc877ed514/gistfile1.txt

具体来说,让我给你具体的情况,在这种情况下我会遇到异常。考虑这个包含三个 class 的代码块。一个被注释为@Entity,另外两个被注释为@Embeddable。 我知道在一个 class 中我们不能定义 @Id 和 @EmbeddedId 而我没有那样做,然后在部署服务器时,我得到的异常只是说:

[class org.apache.{SomeClass}] has both an @EmbdeddedId (on attribute [id]) and an @Id (on attribute []. Both ID types cannot be specified on the same entity.

@Entity
@Table(name="user")  
public class User {

  @ID
  public Long id;

  @Column(name="userCode")
  public String userCode;

  @ElementCollection
  @CollectionTable(name = "address", joinColumns = @JoinColumn(name = "user_id"))
  public List<Address> addressList;

  ....
}

@Embeddable  
public class Address {

  @EmbeddedId
  @Column(name = "id")
  public Long id;

  @Column(name="userId")
  public Long userId;

  @Column(name="address-line-1")
  public String addressLine1;

  @Column(name="address-line-2")
  public String addressLine2;

  @ElementCollection
  @CollectionTable(name = "phone", joinColumns = @JoinColumn(name = "user_id"))
  protected List<Phone> phoneList;

  ....
}

@Embeddable 
public class Phone {

  @EmbeddedId
  @Column(name = "id")
  public Long id;

  @Column(name="contact_no")
  public String contactNo;

  @Column(name="country_code")
  public int countryCode;

  @Column(name="address_id")
  public int addressId;

  ....
}

如果需要更多详细信息,请告诉我,我们将不胜感激。

谢谢,

一个 class 注释 @Embeddable 不能包含任何 @EmbeddedId.

@EmbeddedId是在另一个class中注解@Embeddableclass类型的字段,标记为主键。

示例:

@Entity
public class Project {
    @EmbeddedId ProjectId id;
    ...
}

@Embeddable
Class ProjectId {
    int departmentId;
    long projectId;
    ...
}

编辑 :在你的情况下,我认为你不希望 Phone,也不希望 Address 成为 @Embeddable。只需将它们标记为常规 @Entity.

所以我在这里找到了问题。我做了一些分析,对 embeddable 和 elementCollection 有了更多的了解。这是参考:https://en.wikibooks.org/wiki/Java_Persistence/ElementCollection

所以现在我相应地修改了代码并且现在工作正常。这就是我让它工作的方式:

@Entity
@Table(name="user")  
public class User {

  @ID
  public Long id;

  @Column(name="userCode")
  public String userCode;

  @OneToMany(fetch = FetchType.EAGER)
  @JoinColumn(name = "user_id")
  public List<Address> addressList;

  ....
}

@Entity
@Table(name="address")  
public class Address {

  @Id
  @Column(name = "id")
  public Long id;

  @Column(name="userId")
  public Long userId;

  @Column(name="address-line-1")
  public String addressLine1;

  @Column(name="address-line-2")
  public String addressLine2;

  @OneToMany(fetch = FetchType.EAGER)
  @JoinColumn(name = "address_id")
  protected List<Phone> phoneList;

  ....
}

@Entity
@Table(name="phone")
public class Phone {

  @Id
  @Column(name = "id")
  public Long id;

  @Column(name="contact_no")
  public String contactNo;

  @Column(name="country_code")
  public int countryCode;

  @Column(name="address_id")
  public int addressId;

  ....
}