• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Collection Mapping in Hibernate : one-to-many

July 26, 2008 //  by Krishna Srinivasan//  Leave a 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:

  • Introduction to Hibernate
  • Hibernate Interview Questions
  • Interceptors in Hibernate
  • Hibernate Books

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>

Category: HibernateTag: hibernate mappings

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.

Previous Post: « Comparison operators in Hibernate
Next Post: JPA and NetBeans »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

EJB 3.0 Timer Services

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact