Previously we have learned about Component Mapping in Hibernate using Annotations. In this example, we will learn about Hibernate Component Mapping using XML. In our example, we have two classes Employee and EmployeeAddress. The Employee can have an Address but Address cannot exist separately without Employee. Since the Employee and Address entities are strongly related, it is better to store them in a single table using component mapping( tag).
Creating table
Create EMPLOYEE Table, simply Copy and Paste the following SQL query in the query editor to get the table created.
First and foremost property is for specifying the JDBC Driver class, in my case it OracleDriver
oracle.jdbc.driver.OracleDriver
Give the connection URL for connecting the database and provide username and password for connecting the above database
jdbc:oracle:thin:@mydb:40051:devrootroot
Specify the connection pool size, this property limits the number of connections in the Hibernate connection pool.
Dialect Property makes the Hibernate generate the SQL for the corresponding database which is being used. In this example we are using Oracle database hence Oracle query will be generated. If you are using MySQL database then you need to change the dialect accordingly.
org.hibernate.dialect.Oracle10gDialect
The show_sql property will print the executed sql in the console when set to true.
If the property “hibernate.hbm2ddl.auto” is set to “create” This will drop and recreate the database schema on every execution. If it is set to “update” then the database schema will be updated every time rather than dropping and recreating.
Under the Mapping resource tag, we need to specify all the mapping resources for which we need the table to be created or updated.
ComponentMapping.java
package com.javainterviewpoint; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; public class ComponentMapping < public static void main(String args[]) < //Reading the hibernate configuration file Configuration configuration = new Configuration().configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder regBuilber = new StandardServiceRegistryBuilder(); regBuilber.applySettings(configuration.getProperties()); ServiceRegistry serviceRegistry = regBuilber.build(); //Create SessionFacctory SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); //Create Session from SessionFactory Session session = sessionFactory.openSession(); //Begin the transaction session.beginTransaction(); //Create a new EmployeeAddress object EmployeeAddress address = new EmployeeAddress(); address.setStreet("Tharamani"); address.setCity("Chennai"); address.setState("TamilNadu"); //Create a new Employee object Employee employee = new Employee(); //employee.setId(1); employee.setName("JavaInterviewPoint"); employee.setAddress(address); session.save(employee); //Commit the changes session.getTransaction().commit(); //Close the session session.close(); >>
Create the Configuration object and read the configuration file using the configure() method.
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
Get the SessionFactory object through the buildSessionFactory() method of the configuration object.
Create EmployeeAddress and Employee object and set values to its properties
//Create a new EmployeeAddress object EmployeeAddress address = new EmployeeAddress(); address.setStreet("Tharamani"); address.setCity("Chennai"); address.setState("TamilNadu"); //Create a new Employee object Employee employee = new Employee(); //employee.setId(1); employee.setName("JavaInterviewPoint"); employee.setAddress(address);
save() method of the session object will persist the Employee object
Finally get the transaction and commit the changes and close the session.
In hibernate, there is two way of mapping — first one is by using hibernate annotation and second one is by using hbm.xml.
When you use hbm.xml, just modify default hibernate SessionFactory class in hibernate-cfg.xml by passing your «hbm.xml» file path as an argument to resource method.
Example: Here we are taking an example of employee table.
Employee.hbm.xml file is your hbm.xml file in which table column and their types are mapped.
Employee.hbm.xml
hibernate-cfg.xml is configuration file of hibernate where you map hbm.xml