91-9990449935 0120-4256464 |
One to One Mapping in Hibernate by many-to-one exampleWe can perform one to one mapping in hibernate by two ways:
Here, we are going to perform one to one mapping by many-to-one element. In such case, a foreign key is created in the primary table. In this example, one employee can have one address and one address belongs to one employee only. Here, we are using bidirectional association. Let's look at the persistent classes. 1) Persistent classes for one to one mappingThere are two persistent classes Employee.java and Address.java. Employee class contains Address class reference and vice versa. Employee.javaAddress.java2) Mapping files for the persistent classesThe two mapping files are employee.hbm.xml and address.hbm.xml. employee.hbm.xmlIn this mapping file we are using many-to-one element with unique="true" attribute to make the one to one mapping. address.hbm.xmlThis is the simple mapping file for the Address class. 3) Configuration fileThis file contains information about the database and mapping file. hibernate.cfg.xml4) User classes to store and fetch the dataStore.javaFetch.java |