The embeddable class ContractInformation is used in an element collection of the Employee entity.
@Entity
Public class Employee {
@Id int empId;
@ElementaryCollection Set <ContractInformation> info;
. . .
}
Assume that the phone class is an entity and that address is an embedded class.
Which two of the code segments below can be used to model the state of ContractInformation?
(Choose two)
A.
@OneToMany Set <phone> phones;
B.
@Embeddable Address address;
C.
@ManyToOne phone phone;
D.
@ElementaryCollection <Phone> phones;
E.
@OneToOne Address address;
B C
Why “c” ?
A is incorrect
B And C
Mapping contains an embeddable “q62.ContractInformation” with a prohibited mapping “phone62s”, embeddables in element collections may only contain many-to-one or one-to-one mappings which must be on the “owning” side of the relationship and must not use join table
agree with you Test , answer is B And C
explanation why A is wrong :
if we have a class like this one , this class have wrong relation.
because :
an embeddable cannot use a mapping that requires a foreign key to point at the embeddable, such as a OneToMany or ManyToMany. Thisis because the embeddable doesn’t have a primary key for the foreign keys required by the mappings to point to. Make your embeddable a full entity instead.
@Embeddable public class ContactInformation {
@OneToMany
private Set phoneList;
@Embedded
private Address address;
……
}
so it should be @ManyToOne phone phone; Like C
see this link :
http://stackoverflow.com/questions/18848294/how-to-define-a-elementcollection-of-an-embedded-type-which-contains-onetomany