我已经这样做了。请参考这个,它在那里也会很好地工作:
@Entity
@Table(name = "member_information")
public class MemberInformation implements Serilizable{
@OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE},optional = false)
private MemberInformationDetails memberInformationDetailsId;
@LazyCollection(LazyCollectionOption.TRUE)
@OneToMany(mappedBy = "memberInformationId", cascade = CascadeType.ALL)
private Collection<AccountOpening> accountOpeningCollection;
}
用于@OneToOne(fetch = FetchType.LAZY....)
一对一映射和集合使用@LazyCollection(LazyCollectionOption.TRUE)
。
@Entity
@Table(name = "member_info_details")
public class MemberInformationDetails implements Serializable{
@OneToOne(mappedBy = "memberInformationDetailsId")
private MemberInformation memberInformationId;
.......//getter and setters
}
@Entity
@Table(name = "account_opening")
public class AccountOpening implements Serializable {
@JoinColumn(name = "member_information_id", referencedColumnName = "id", nullable = false)
@ManyToOne(optional = false)
private MemberInformation memberInformationId;
..........//getters and setters
}
当您要访问集合时,请先合并并获取对象:
@Stateless
public class MemberInformationFacade{
..............
public MemberInformation getMemberWithMemberDetails(MemberInformation m) {
m = getEntityManager().merge(m);
try {
m.getMemberInformationDetailsId().getId();
m.getMemberInformationDetailsId().accountOpeningCollection().size();
} catch (Exception e) {
e.printStackTrace();
}
return m;
}
}