Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Hibernate One to Many Mapping

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 521
    Comment on it

    One to many mapping

    One-to-many mapping means a mapping in which each record in one table is linked to multiple records in another table. Like a parent record in one table can have several child records in another table.The parent is not required to have child records i.e allows zero child records, a single child record or multiple child records. The important thing is that the child cannot have more than one parent record.

    This can be explained with a simple example, lets have a look on this:

    There are two persistent classes Customer.java & Order.java

    Customer.java

    public class Customer implements java.io.Serializable {
        private long customerId;
        private String firstName;
        private String lastName;
        private Set orders = new HashSet();
    
        public Customer() {
        };
    Order.java
    public class Order {
        private Long orderId;
        private Long quantity;
        private String orderDescription;
        private Date orderDate;
    
        private Customer customer;
    

    There are two mapping files for the persistent classes namely, customer.hbm.xml & order.hbm.xml

    Mapping file is used to define customer table attributes.

    customer.hbm.xml

    <hibernate-mapping>
     <class name="com.hibernateExample.Customer" table="customer">
    <id name="customerId" type="java.lang.Long">
                <column name="customerId">
                <generator class="identity">
            </generator></column></id>
            <property name="firstName" type="string">
                <column name="firstName" length="20" not-null="true">
            </column></property>
            <property name="lastName" type="string">
                <column name="lastName" length="20" not-null="true">
            </column></property>
            <set name="orders" table="order" inverse="true" lazy="true" fetch="select">
                <key><column name="customerId" not-null="true"></column></key>
                <one-to-many class="com.hibernateExample.Order">
            </one-to-many></set>
        </class>
    </hibernate-mapping>
    

    This mapping file is used to define order table attribute

    order.hbm.xml

    <hibernate-mapping>
        <class name="com.hibernateExample.Order" table="order">
            <id name="orderId" type="java.lang.Long">
                <column name="orderId">
                <generator class="identity">               
            </generator></column></id>
            <many-to-one name="customer" class="com.hibernateExample.Customer" fetch="select">
                <column name="customerId" not-null="true"></column>    
            </many-to-one>
            <property name="quantity" type="java.lang.Long">
                <column name="quantity" length="10" not-null="true"></column>
            </property>
            <property name="orderDescription" type="string">
                <column name="orderDescription" length="50" not-null="true"></column>
            </property>
            <property name="orderDate" type="date">
                <column name="orderDate" length="10" not-null="true"></column>
            </property>
        </class>
    </hibernate-mapping>
    

    A configuration file named hibernate.cfg.xml contains information about the database and mapping file.

    <hibernate-configuration>
        <session-factory>
            <property name="connection.url">jdbc:mysql://localhost:3306/hibernateExample</property>
            <property name="connection.username">root</property>
            <property name="connection.password"></property>
            <property name="connection.driver&#95;class">com.mysql.jdbc.Driver</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    
            <mapping resource="com/hibernateExample/customer.hbm.xml"></mapping>
            <mapping resource="com/hibernateExample/order.hbm.xml"></mapping>       
        </session-factory>
    </hibernate-configuration>
    

    Now lets create App.java class, which is going to create two tables in database as, customer & order and are linked with one-to-many relationship.

    public class App {
        public static void main(String[] args) {
        ...................................
                .................................
                 ................................
    
         Customer customer = new Customer();
            customer.setFirstName("Amit");
            customer.setLastName("Singh");
    
            session.save(customer);
    
            Set orders = new HashSet();
    
            Order dellLaptops = new Order();
            dellLaptops.setCustomer(customer);
            dellLaptops.setOrderDate(new Date());
            dellLaptops.setQuantity(30l);
            dellLaptops.setOrderDescription("30 Dell latitude D830 laptops");
    
            Order sonyLaptops = new Order();
            sonyLaptops.setCustomer(customer);
            sonyLaptops.setOrderDate(new Date());
            sonyLaptops.setQuantity(40l);
            sonyLaptops.setOrderDescription("40 sony viao laptops");
    
            orders.add(dellLaptops);
            orders.add(sonyLaptops);
            customer.setOrders(orders);
    
                    .................................
                    ..............................
                    .........................
    
        }
    }
    

    Hope this help someone!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: