Retrieve Object Hibernate, Save Serializable Object Step 19
In my last step I showed you how to retrieve objects and set up the Hibernate DAO.
In this post, I will show you how to save your Hibernate Object, and retrieve a specific object using the id.
If you were able to set everything else up in Hibernate, these steps should be pretty easy.
These three methods you should add to your UserBookDAO.java Object.
Method to Retrieve Objects based on the Id and Method to Save Hibernate Objects
public Book getOneBook(long id)
{
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
Book book=null;
try {
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
book=(Book)sessionFactory.getCurrentSession().get(Book.class,id);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
} catch (RuntimeException re) {
System.out.println("Error In Hibernate getBook" re.toString());
throw re;
}
return book;
}
public Users getOneUser(long id)
{
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
Users user=null;
try {
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
user=(Users)sessionFactory.getCurrentSession().get(Users.class,id);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
} catch (RuntimeException re) {
System.out.println("Error In Hibernate getBook" re.toString());
throw re;
}
return user;
}
public void save(Serializable transientInstance) {
try {
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
sessionFactory.getCurrentSession().save(transientInstance);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
} catch (RuntimeException re) {
throw re;
}
}
{
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
Book book=null;
try {
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
book=(Book)sessionFactory.getCurrentSession().get(Book.class,id);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
} catch (RuntimeException re) {
System.out.println("Error In Hibernate getBook" re.toString());
throw re;
}
return book;
}
public Users getOneUser(long id)
{
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
Users user=null;
try {
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
user=(Users)sessionFactory.getCurrentSession().get(Users.class,id);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
} catch (RuntimeException re) {
System.out.println("Error In Hibernate getBook" re.toString());
throw re;
}
return user;
}
public void save(Serializable transientInstance) {
try {
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
sessionFactory.getCurrentSession().save(transientInstance);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
} catch (RuntimeException re) {
throw re;
}
}
The first two methods retrieves the objects using the id. This will allow you to retrieve a particular User or a particular Book based on the id. The other method will allow you to save the objects. The save method allows you to save a serializable object. Hibernate will know which table to save the data too based on your hibernate mappings.
You will also need to add these methods to your UserBookServiceImpl.java class.
public Book getOneBook(long id)
{
return dao.getOneBook(id);
}
public Users getOneUser(long id)
{
return dao.getOneUser(id);
}
public void save(Serializable transientInstance)
{
dao.save(transientInstance);
}
You will need to add these to your UserBookService.java class.
public Book getOneBook(long id);
public Users getOneUser(long id);
public void save(Serializable transientInstance);
This code can be added to your action class to test to make sure the methods work.
This code has the id's hardcoded. Therefore, make sure that the id in this code matches the ids in your database.
Book book=service.getOneBook(1);
System.out.println("Book with Id: "+ book.getBookName());
Users user=service.getOneUser(1);
System.out.println("User with Id: " +user.getFirstName() +" "+ user.getLastName());
Users newuser=new Users();
newuser.setFirstName("New User");
newuser.setLastName("New Users Last Name");
service.save(newuser);
************************************************
The eclipse console should printout the names of the individual user and individual book you called based on the id. A new user will be added to the database as well. You can check your database to make sure the new user was added. Make sure you pass in a valid id into the method or you will get a null pointer exception.
You might receive an error that says you have a duplicate key when saving the User to the database. That is because you need to change your hbm.xml mappings to reflect a key generator that will increment your key id for you. In all of your mappings, you should change your generator to class="increment" from the class="assigned". This will allow hibernate to insert a key into the database for you and increment the id for each object that is saved. This way your primary key will still be unique.

0 comments:
Post a Comment