Mapping a class more than once in Hibernate by R4R Team

Mapping a class more than once: In hibernate every thing is possible to provide more than one mapping for a particular persistent class. We found in this case we need to specify an entity name to disambiguate between instances of the two mapped entites. In hibernate By default entity name is the same as the class name and Hibernate specify the entity name when working with persistent objects, it done when we writing queries, or mapping association to the named entity.

For Example:

<class name="Camera" table="Cameras" entity-name="CurrentCamera"> ... 

<set name="history" inverse="true" order-by="effectiveEndDate desc"> 

<key column="currentCameraId"/> 

<one-to-many entity-name="HistoricalCamera"/> 



<class name="Camera" table="CameraHistory" entity-name="HistoricalCamera"> ... 

<many-to-one name="currentCamera" column="currentCameraId" entity-name="CurrentCamera"/> 


Here we defined the Associations using entity-name instead of class.

Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!