Hibernate Query @onetomany and @manytoone

Standard

This tutorials is following from my previous post .

If you are not seeing yet my previous , please have a look. @ Previous  Hibernate Post

My Hibernate Repository , iam adding two more method to getAuthor and getBooks .

As Author class have one to many relationship with Books , and Books class have many to one relationship with Author .

So From author class i can get All books related to that author .

As hibernate doing Lazy loading , if i load author it will not load associated objects, in this case its Books object.

To overcome Lazy loading , iam fetching with other object called books .

So My getAuthors function like this .


public List<Author>  getAuthors()
{
return   hibernateTemplate.find("From Author a inner join fetch a.booksCollection b");
}

So Same way iam loading getBooks to load Author object related to that books .


public List<Books>  getBooks()
{
return   hibernateTemplate.find("From Books b inner join fetch b.parentId a");
}

Finally the demo class to just print out All authors and related books , And books and related authors .


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ed.ws.hiber;

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*
* @author home
*/
public class AppSelect {
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
HibernateDao dao = (HibernateDao)context.getBean("userDao");
List<Author> authors = dao.getAuthors();
for(Author author : authors)

{
System.out.println("Author :" +  author.getName() + " " + author.getRole() + " " +  author.getId() );
if (author.getBooksCollection() != null)
{
for (Books book : author.getBooksCollection())
{
System.err.println("Books " + book.getBookName() + " " + book.getBooksId());
}
}

}
List<Books> books = dao.getBooks();
for (Books book : books)
{
System.out.println("Book info " + book.getBookName() + book.getBooksId());
if (book.getParentId() != null)
{
System.out.println("Author  info " + book.getParentId().getId() + " " + book.getParentId().getName());
}
}

}
}

Please give me comments , if you like the tutorials .

About these ads

2 thoughts on “Hibernate Query @onetomany and @manytoone

  1. Pingback: NamedQuery with hibernate jpa with spring « Sjohn's Blog

  2. Pingback: Hibernate Spring @manytomany with JPA « Sjohn's Blog

  3. Pingback: Convert hibernate template into sessionFactory « Sjohn's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s