Sunday, August 21, 2016

HIBERNATE: ONE TO ONE MAPPING

A Vehicle entity is inside a UserDetails entity!

Here is the definition of vehicle entity:


This newly defined entity must be mentioned in the hibernate.cfg.xml file.




Now put the Vehicle object inside the UserDetails class definition:
Used annotations are @OneToOne (which is self explanatory) and @JoinColumn (is optional) which takes the attribute name to rename the otherwise generated foreign key column name.





The generated schema looks like the following:



Basically, both the entities have separate tables and just one column to connect them in the parent USER_DETAILS class. 

However, the order of SQL queries made up here is very interesting. First, both the entities are inserted, and then the association using foreign key is made using an UPDATE operation.


No comments:

Post a Comment