I have done so. Please refer to this, it will work just as well:
@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; }
Use @OneToOne(fetch = FetchType.LAZY....) to match one to one and to use the @LazyCollection(LazyCollectionOption.TRUE) collection.
@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 }
If you want to access the collection, merge it to and get an object:
@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; } }
source share