Hibernate One to Many Mapping -XML

In this post, I am going to give an  example of hibernate ONE TO MANY mapping using XML.
Two entity class are used Author and Book.One Author can write many books i.e. One to Many Relation.I am using MySQL database and have created schema name "hibernate" before following below steps.



Author.java

package com.skuera.entity;



import java.util.Set; 


/**

 * @author skuera
 *
 */
public class Author {

private int authId;
private String authName;
private Set<Book> books;
public int getAuthId() {
return authId;
}
public void setAuthId(int authId) {
this.authId = authId;
}
public String getAuthName() {
return authName;
}
public void setAuthName(String authName) {
this.authName = authName;
}
public Set<Book> getBooks() {
return books;
}
public void setBooks(Set<Book> books) {
this.books = books;
}
@Override
public boolean equals(Object obj) {
Author auth=(Author)obj;
if(this.authId==auth.authId){
return true;
}else{
return false;
}
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return authId;
}


}


Book.java

package com.skuera.entity;


/**

 * @author skuera
 *
 */
public class Book {

private int bookId;
private String bookName;
private double bookPrice;
private Author author;

public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public double getBookPrice() {
return bookPrice;
}
public void setBookPrice(double bookPrice) {
this.bookPrice = bookPrice;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}



}

AddRecordDao.java

package com.skuera.dao;



import java.util.HashSet;

import java.util.Set;

import org.hibernate.Session;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.skuera.entity.Author;

import com.skuera.entity.Book;



/**

 * @author skuera
 *
 */
public class AddRecordDao {

private static SessionFactory sf=null;
static{
Configuration cfg=new Configuration().configure();
sf=cfg.buildSessionFactory();
}
public static void insertAuthor() {
Session sess=sf.openSession();
Transaction tx=sess.beginTransaction();
Author author=new Author();
author.setAuthId(1003);
author.setAuthName("Arun");

Book b1=new Book();
b1.setBookId(2005);
b1.setBookName("Spring programming");
b1.setBookPrice(1700);
b1.setAuthor(author);

Book b2=new Book();
b2.setBookId(2006);
b2.setBookName("hibernate Programming");
b2.setBookPrice(3900);
b2.setAuthor(author);

Set<Book> books=new  HashSet<Book>();
books.add(b1);
books.add(b2);
author.setBooks(books);
sess.save(author);

tx.commit();
sess.close();
System.out.println("Rows Inserted..................");


}

}

Client.java

package com.skuera.client;

import com.skuera.dao.AddRecordDao;



/**

 * @author skuera
 *
 */
public class Client {


public static void main(String[] args) {
AddRecordDao author=new AddRecordDao();
author.insertAuthor();
}
}

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">Welcome123</property>
     
      <property name="hibernate.show_sql">true</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
       <property name="hibernate.hbm2ddl.auto">update</property>
     
     
      <!-- Mapping files -->
      <mapping resource="author-book.hbm.xml"/>
     
</session-factory>
</hibernate-configuration>


author-book.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
<hibernate-mapping>
<class name="com.skuera.entity.Author" table ="Author" dynamic-update="true" dynamic-insert="true">
<id name ="authId" column="auth_id" type="int">
<generator class ="assigned"/>
</id>
<property name="authName" column="auth_name" length="50"/>
<set name="books" cascade="all" inverse="true">
<key column="aid"/>
<one-to-many class="com.skuera.entity.Book"/>
</set>
</class>
<class name="com.skuera.entity.Book" table="book"
dynamic-update="true" dynamic-insert="true">
<id name="bookId" column="book_id" type="int">
<generator class="assigned"/>
</id>
<property name="bookName" column="book_name" length="50"/>
<property name="bookPrice" column="book_price" type="double"/>
<many-to-one name="author" column="aid" class="com.skuera.entity.Author"/>
</class>
</hibernate-mapping>

Result:
Two table author and book will be created and record inserted.

Give you suggestion in comments.If you like the post Share with your friends on social network.



Hibernate One to Many Mapping -XML Hibernate One to Many Mapping -XML Reviewed by JavaInstance on 11:21:00 AM Rating: 5

No comments:

Powered by Blogger.