Hi,
In my JPA code I am trying to avoid using the annotations and just managing the same mapping inside orm.xml.
This has been working very nicely.
But I dont know how to implement in one scenario - one is to one relationship where in second table the primary key id is to be same as the id of the parent table through the foreign key.
How do I bring in hibernate extensions when using only orm.xml. Is there any thing available so that one does not need to use the annotations and yet the hibernate extensions can be used.
For example -
What do I do about org.hibernate.annotations.GenericGenerator in the xml?Skipping additional details - one can do one to one bi-directional using following annotations- How do you do this same configuration using only orm.xml also shown below:
Parent entity:
Code:
@javax.persistence.Id
@javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
@javax.persistence.Column(name = "id", unique = true, nullable = false)
public Integer getId()
{
return this.id;
}
@javax.persistence.OneToOne(fetch =javax.persistence.FetchType.LAZY, mappedBy = "user")
public Address getAddress()
{
return this.address;
}
One is to one child entity:
Code:
[b]@org.hibernate.annotations.GenericGenerator(name = "generator", strategy = "foreign", parameters = @org.hibernate.annotations.Parameter(name = "property", value = "user"))[/b]
@javax.persistence.Id
@javax.persistence.GeneratedValue(generator = "generator")
@javax.persistence.Column(name = "id", unique = true, nullable = false)
public int getId()
{
return this.id;
}
@javax.persistence.OneToOne(fetch = javax.persistence.FetchType.LAZY)
@javax.persistence.PrimaryKeyJoinColumn
public User getUser() {
return this.user;
}
How can I write this in orm.xml?
Code:
<entity class="User">
.......
<attributes>
<id name="id"><column name="id" unique="true" nullable="false"/><generated-value strategy="IDENTITY"/></id>
......
<one-to-one fetch="LAZY" name="address" mapped-by="user" ></one-to-one>
</attributes>
</entity>
<entity class="Address">
.....
<attributes>
<id name="id"><column name="id" unique="true" nullable="false"/>
<generated-value generator="generator"/>
</id>
....
<one-to-one name="user"><primary-key-join-column/></one-to-one>
.....
</attributes>
</entity>
What do I do about org.hibernate.annotations.GenericGenerator in the xml?