I have 2 tables:
Person:
person_id: PK
attribute2:String
Address:
person_id:pFK
field1:String
....
I like 2 represent them in 2 entities (PersonEntity, AddressEnity) and there should be a one to one relation between them, stored in the PersonEnity (thats where the person_id is generated with a sequence generator).
The entites look like follows:
PersonEnity:Code:
@Column(name = "person_id")
@Id
@SequenceGenerator(name = "personId", sequenceName = "seqPersonID", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "personId")
private Integer id
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "person")
private AddressEntity address;
AddressEnity:Code:
@Id
@GenericGenerator(name = "addressGenerator", strategy = "foreign", parameters = { @Parameter(name = "property", value = "person") })
@GeneratedValue(generator = "addressGenerator")
private Integer id;
@Id
@OneToOne(optional = false)
@PrimaryKeyJoinColumn
private PersonEntity person;
As you see because of the GenericGenerator i needed to create a bidirectional one to one relation.
With this code I get following ERROR:
Unknown mappedBy in: ch.test.PersonEntity.address, referenced property unknown: ch.test.AddressEntity.person
How does I have to create this relation correctly?
The solution I found in the hibernat doc was: (see also
http://docs.jboss.org/hibernate/stable/core/reference/en/html/assoc-bidirectional.html#assoc-bidirectional-121):
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<one-to-one name="address"/>
</class>
<class name="Address">
<id name="id" column="personId">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person"
constrained="true"/>
</class>
But how can I do that with annotations?