Hibernate One to One Mapping
-
08-10-2019 - |
Pergunta
I have two class
Class Item
{
private Auction CorrespondingAuction;
}
Class Auction
{
private Item CurrentItem;
}
Can someone tell how to set this one to one mapping in XML, it must be bidirectional
Solução 2
In Auction XML FILE:
<many-to-one name="CurrentItem" class="com.BiddingSystem.Models.Item" fetch="join"
not-null="true" cascade="all" unique="true" lazy="false">
<column name="CURRENTITEM" />
</many-to-one>
In Item XML File:
<one-to-one name="auction" class="com.BiddingSystem.Models.Auction" property-ref="CurrentItem"/>
property-ref refers to the name of the variable corresponding to class item in the auction class
Outras dicas
Bookmark this cheat sheet.This shows simple examples of all the mappings supported by Hibernate.
You need to have PK in common:
Class Item {
@Id
Long id;
@OneToOne
private Auction CorrespondingAuction;
}
Class Auction {
@Id
Long id;
private Item CurrentItem;
}
The ID for Auction is taken from the ID already generated for Item
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow