Collection Mapping in Hibernate : one-to-many

SHARE & COMMENT :

one-to-many class mapping

This article explains how to map two classes in collection mapping. In this example one Author object has the list of Book objects. Both the classes are mapped and will be inserted together into the database. Here this example uses Bag as the collection type. Other collection types available are Set,List,Array. Look into the example programs for more detail.

Author.java


package javabeat.net.hibernate;

/**
 * source : www.javabeat.net
 */
import java.util.List;

public class Author {
    private long id;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<book> getBooks() {
        return books;
    }

    public void setBooks(List<book> books) {
        this.books = books;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    private List<book> books;
}

Book.java

package javabeat.net.hibernate;

/**
 * source : www.javabeat.net
 */
public class Book {
    private long id;
    private String name;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

JavaBeatHibernateExample

package javabeat.net.hibernate;


import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 * source : www.javabeat.net
 */
public class JavaBeatHibernateExample {
    public static void main(String args[]) {
        Configuration configuration = new Configuration();
        // configuring hibernate
        SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        tx.begin();
        Book book1 = new Book();
        book1.setName("Java Book");

        List<book> list = new ArrayList<book>();
        list.add(book1);

        Author author = new Author();
        author.setName("First Author");
        author.setBooks(list);
        session.save(author);
        tx.commit();;
        session.flush();
        session.close();
    }
}

also read:

Author.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="javabeat.net.hibernate.Author" table="Author">
        <id name="id" column="id" type="java.lang.Long" unsaved-value="0">
           <generator class="increment"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>
        <bag name="books" cascade="all" >
            <key column="id"/>
            <one-to-many class="javabeat.net.hibernate.Book"/>
        </bag>
    </class>
    <class name="javabeat.net.hibernate.Book" table="Book">
        <id name="id" column="id" type="java.lang.Long" unsaved-value="0" >
           <generator class="increment"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>
    </class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:derby://localhost:1527/SampleDB
        </property>
        <property name="connection.username">root
        </property>
        <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver
        </property>
        <property name="dialect">org.hibernate.dialect.DerbyDialect
        </property>
        <property name="connection.password">root
        </property>
        <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory
        </property>
        <property name="current_session_context_class">thread
        </property>
        <property name="hibernate.show_sql">true
        </property>
        <mapping resource="javabeat/net/hibernate/Author.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better