使用一个实体映射多个集合

时间:2018-04-12 15:34:08

标签: java hibernate annotations mapping

我有一个下面列出的四个实体类。 映射要求如下 - 1 - 客户可能有多个地址 2-Customer可能有多个唯一的联系号码。 3-Customer可能拥有多个独特的电子邮件ID。 在运行应用程序时,我收到多个集合的MultipleBagFetchException becoz或者可能是错误映射的bcoz。,任何人都可以帮助我... 感谢

首先

public class Customer{
@Id
@GeneratedValue(generator = "uuid")
@Access(AccessType.PROPERTY)
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Type(type = "uuid-char")
@Column(columnDefinition = "VARCHAR(36)", name = DatabaseConstants.ID)
private UUID id;

private String firstName;
private String middleName;
private String lastName;

private Date dob;
@OneToOne(cascade = CascadeType.ALL)
private Gender gender;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer")
@JoinColumn(name="department_id")
@Embedded
private List<Address> address;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer")
@Embedded
private Set<Email> email;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer")
@Embedded
private Set<Mobile> mobile;
}

二 -

public class Address {
@Id
@GeneratedValue(generator = "uuid")
@Access(AccessType.PROPERTY)
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Type(type = "uuid-char")
@Column(columnDefinition = "VARCHAR(36)", name = DatabaseConstants.ID)
private UUID id;
private String country;
private String state;
private String city;
private String streetAddress;
private String zip;
private String addressType;
@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
private Customer customer;
}

第三 -

public class Email {
@Id
@GeneratedValue(generator = "uuid")
@Access(AccessType.PROPERTY)
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Type(type = "uuid-char")
@Column(columnDefinition = "VARCHAR(36)", name = DatabaseConstants.ID)
private UUID id;
@javax.validation.constraints.Email
private String emailId;
@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
private Customer customer;
private boolean primary;
}

四 -

public class Mobile {
@Id
@GeneratedValue(generator = "uuid")
@Access(AccessType.PROPERTY)
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Type(type = "uuid-char")
@Column(columnDefinition = "VARCHAR(36)", name = DatabaseConstants.ID)
private UUID id;

private String mobileNumber;
private boolean primary;
@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
private Customer customer;
}

1 个答案:

答案 0 :(得分:0)

这些

上不需要@Embedded属性
private List<Address> address;
private Set<Email> email;
private Set<Mobile> mobile;

只有在要嵌入复合值时才需要@Embedded。这里有一个简单的实体之间的一对多关系。删除类成员上的@Embedded和子类的@Embeddable(如果存在),如果不存在则删除@Entity。