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

Tus*_*tra 5 java jpa eclipselink embeddable

在我当前的实现中,我为每个db表都有单独的实体类.我正在使用JPA和eclipselink-2.5.2.这对我来说很好,但在某些时候,当数据很大时,它会滞后.这就是我决定开始使用@ Embedded,@ Embeddable和@EmbeddedId的原因.在这样做时,我收到的错误对我来说非常奇怪.这是完整的堆栈跟踪:https://gist.githubusercontent.com/tjDudhatra/b955812e0d1a71cf97f1/raw/11ea458869e24baae744530417ac99bc877ed514/gistfile1.txt

具体,让我给你一个确切的场景,在这种情况下,我得到了例外.考虑这个有三个类的Code块.一个注释为@Entity,其他两个注释为@Embeddable.我知道在一个类中我们不能定义@Id和@EmbeddedId而我没有这样做,那么在部署服务器时,我得到的异常只表示:

[class org.apache.{SomeClass}]同时包含@EmbdeddedId(在属性[id]上)和@Id(在属性[]上.两种ID类型都不能在同一实体上指定.

@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;

  ....
}
Run Code Online (Sandbox Code Playgroud)

如果需要更多详细信息和任何形式的帮助将非常感谢,请告知我们.

谢谢,

Tus*_*tra 3

所以在这里我发现了这个问题。我做了一些分析,对 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;

  ....
}
Run Code Online (Sandbox Code Playgroud)