In this example we will see how to establish one to many relationship in hibernate. To explain this, lets take two classes
UserDetails and Vehicle. Each user can have one or more number of vehicles. Similar to One to one mapping example we can make use
of "@OneToMany" annotation given by hibernate for one to many mapping. The below code is an example of one to many mapping.
In this example one user can have many vehicles. Hence I have used @OneToMany in the UserDetails class. Instead of using OneToMany in UserDetails class we can use the annotation "@ManyToOne" in Vehicle class.
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> <property name="connection.username">hr</property> <property name="connection.password">hr</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <!-- Names the annotated entity class --> <mapping class="com.ram.dao.UserDetails"/> <mapping class="com.ram.dao.Vehicle"/> </session-factory> </hibernate-configuration>
File: com.ram.dao.Vehicle.java
package com.ram.dao; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Vehicle { @Id @GeneratedValue private int vehicleId; private String vehicleName; public int getVehicleId() { return vehicleId; } public void setVehicleId(int vehicleId) { this.vehicleId = vehicleId; } public String getVehicleName() { return vehicleName; } public void setVehicleName(String vehicleName) { this.vehicleName = vehicleName; } }
File: com.ram.dao.UserDetails.java
package com.ram.dao; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class UserDetails { @Id @GeneratedValue private int userId; @Column (name="USER_NAME") private String userName; @OneToMany private Collectionvehicle = new ArrayList (); public Collection getVehicle() { return vehicle; } public void setVehicle(Collection vehicle) { this.vehicle = vehicle; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
File: com.ram.hibernate.OneToManyExample.java
package com.ram.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.ram.dao.UserDetails; import com.ram.dao.Vehicle; public class OneToManyExample { public static void main(String[] args) { Vehicle vehicle1 = new Vehicle(); vehicle1.setVehicleName("Benz"); Vehicle vehicle2 = new Vehicle(); vehicle2.setVehicleName("BMW"); UserDetails userDetails = new UserDetails(); userDetails.setUserName("Ram"); userDetails.getVehicle().add(vehicle1); userDetails.getVehicle().add(vehicle2); SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); //session.persist(userDetails); session.save(userDetails); session.save(vehicle1); session.save(vehicle2); session.getTransaction().commit(); session.close(); } }
No comments:
Post a Comment