使用@AttributeOverride对@Embeddable项进行JPA @ElementCollection

时间:2018-05-18 14:08:22

标签: java jpa mariadb eclipselink jpa-2.0

@Embeddable
public class Address {
  @Column(name="str")
  private String street;

  @Column(name="nr")
  private int number;

  // getters/setters
}

并且地址@Embeddable在User entiy中的两个位置使用:Address User.mainAddress和List User.otherAddresses:

@Entity
public class User {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private long id;

  @AttributeOverrides({
    @AttributeOverride(
      name="street", 
      column=@Column(name="main_user_address_street")),
    @AttributeOverride(
      name="number", 
      column=@Column(name="main_user_address_number"))
  })
  private Address mainAddress;

  @ElementCollection
  @CollectionTable(
    name="user_addresses", 
    joinColumns=@JoinColumn(name="user_id"))
  @AttributeOverrides({
    @AttributeOverride(
      name="street", 
      column=@Column(name="other_user_address_street")),
    @AttributeOverride(
       name="number", 
       column=@Column(name="other_user_address_number"))
  })
  private List<Address> otherAddresses;

  // getters/setters
}

这会创建表格如下:

user(ID, main_user_address_street, main_user_address_number)
user_addresses(nr, str, user_id)

问题:

对于User.otherAddresses的@AttributeOverrides似乎被忽略了,我原以为user_addresses表的列结构如下:

user_addresses(other_user_address_number, other_user_address_street, user_id)

我错过了吗?这是预期的结果吗?

依赖关系:

  • org.eclipse.persistence / javax.persistence / 2.2.0
  • org.eclipse.persistence /的EclipseLink / 2.7.1
  • org.mariadb.jdbc / MariaDB的-Java的客户机/ 2.2.4

  • java版:1.8

0 个答案:

没有答案