@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.mariadb.jdbc / MariaDB的-Java的客户机/ 2.2.4
java版:1.8