JavaBeat

  • Home
  • 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)
  • Privacy
  • Contact Us

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

[code lang=”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;
}

[/code]

Book.java

[code lang=”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;
}
}
[/code]

JavaBeatHibernateExample

[code lang=”java”]
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();
}
}
[/code]

also read:

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

Author.hbm.xml

[code lang=”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>
[/code]

hibernate.cfg.xml

[code lang=”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>
[/code]

Filed Under: Hibernate Tagged With: 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.

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.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved