JSF 2.0 with hibernate 3.0 tutorial, Online Movie Reviews web-app part 2

JSF 2.0 with hibernate 3.0 tutorial, Online Movie Reviews web-app part 2

in last post, you have seen all model classes, hibernate configuration and mappings.

in this post I will show you Data Access Objects (DAO components: data access layer)

for each Model class, I have created a separate DAO class:

com.mahendra.dao.MovieDAO


package com.mahendra.dao;
import java.util.List;
import org.hibernate.*;
import com.mahendra.models.Movies;
import com.mahendra.utils.HibernateUtils;

public class MovieDAO {

SessionFactory fact = null;

public MovieDAO() {
fact = HibernateUtils.getSessionFactory();
}

public void create(Movies m) {

Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
session.save(m);
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}

}

public Movies getMovie(String title) {
Session session = fact.getCurrentSession();
Transaction t = null;
Movies m = null;
try {
t = session.beginTransaction();
m = (Movies) session.createQuery("from Movies where title=:title")
.setString("title", title).list().get(0);
t.commit();
} catch (HibernateException he) {

if (t != null)
t.rollback();
he.printStackTrace();
}

return m;
}

public Movies getMovie(int id) {
Session session = fact.getCurrentSession();
Transaction t = null;
Movies m = null;
try {
t = session.beginTransaction();
m = (Movies) session.get(Movies.class, id);
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}

return m;
}

public List<Movies> getAllMovies() {
List<Movies> list = null;
Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
list = session.createQuery("from Movies").list();
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}

return list;
}

}

com.mahendra.dao.ReviewsDAO:

package com.mahendra.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.*;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.transform.Transformers;
import com.mahendra.models.Movies;
import com.mahendra.models.Reviews;
import com.mahendra.utils.HibernateUtils;
import com.mahendra.utils.MovieReviewCount;

public class ReviewsDAO {

private SessionFactory fact;

public ReviewsDAO() {
fact = HibernateUtils.getSessionFactory();
}

public void create(Reviews review) {
Session session = fact.getCurrentSession();

Transaction t = null;
try {
t = session.beginTransaction();
session.save(review);
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}
}

@SuppressWarnings("unchecked")

public List<Reviews> getReviewsForMovie(int id) {
List<Reviews> list = null;
Session session = fact.getCurrentSession();
Transaction t = null;

try {
t = session.beginTransaction();
list = session
   .createQuery("from Reviews where movie.movieId=:movieid")
   .setInteger("movieid", id).list();
t.commit();

} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}

return list;
}

public Reviews getReview(int id) {
Session session = fact.getCurrentSession();
Reviews review = null;
Transaction t = null;
try {
t = session.beginTransaction();
review = (Reviews) session.get(Reviews.class, id);
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}

return review;
}

public List<Reviews> getReviewsByUser(int userid) {
List<Reviews> list = null;
Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
list = session.createQuery("from Reviews where userid = :userid")
.setInteger("userid", userid).list();
t.commit();
} catch (HibernateException he) {

if (t != null)
t.rollback();
he.printStackTrace();
}

return list;
}

public List<Reviews> getAll() {
List<Reviews> list = null;
Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
list = session.createQuery("from Reviews").list();
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();
}

return list;
}

public Movies getTopper() {
Movies m = null;
Session session = fact.getCurrentSession();

Transaction t = null;
try {
t = session.beginTransaction();
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();

}

return m;
}

public List<MovieReviewCount> getReviewCount() {

List<MovieReviewCount> list = new ArrayList<MovieReviewCount>();
Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
SQLQuery q = session
.createSQLQuery("select m.title as title,count(r.message) as count, avg(r.ratings) as average  from app.movies m join app.reviews"
+ " r on (m.movie_id=r.movie_id) group by m.title"
+ " order by count(r.message) desc");
q.addScalar("title")
.addScalar("count")
.addScalar("average")
.setResultTransformer(
Transformers.aliasToBean(MovieReviewCount.class));
list = q.list();
t.commit();

} catch (HibernateException he) {
if (t != null)
t.rollback();
System.out.println("Error: ");
he.printStackTrace();
}

return list;
}

}

com.mahendra.dao.UserDAO:


package com.mahendra.dao;

import java.util.List;
import org.hibernate.*;
import com.mahendra.models.Users;
import com.mahendra.utils.HibernateUtils;

public class UserDAO {

SessionFactory fact = null;

public UserDAO() {
fact = HibernateUtils.getSessionFactory();
}

public void create(String username, String password) {

Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
Users user = new Users();
user.setUsername(username);
user.setPassword(password);
Object obj = session.save(user);
System.out.println(obj);
t.commit();
System.out.println("record!");
} catch (HibernateException he) {

if (t != null)
t.rollback();
he.printStackTrace();
}
}

public List<Users> getAllUsers() {

Session session = fact.getCurrentSession();
Transaction t = null;
List<Users> users = null;
try {
t = session.beginTransaction();
users = (List<Users>) session.createQuery("from Users").list();
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
System.out.println("Error: " + he.getMessage());
}

return users;

}

public void delete(int userid) {
Session session = fact.getCurrentSession();
Transaction t = null;
try {
t = session.beginTransaction();
Users user = (Users) session.get(Users.class, userid);
if (user == null)
throw new HibernateException("Record not found!");
session.delete(user);
t.commit();
} catch (HibernateException he) {
if (t != null)
t.rollback();
System.out.println("Error: " + he.getMessage());
}

}

public Users getUser(String username) {

Session session = fact.getCurrentSession();
Transaction t = null;
Users user = null;
try {
t = session.beginTransaction();
Query q = session.createQuery("from Users where username = :uname");
q.setString("uname", username);
List<Users> list = q.list();
if (list == null || list.isEmpty())
throw new RuntimeException("user '" + username
+ "' doesnot exists!");
user = list.get(0);
t.commit();

} catch (HibernateException he) {
if (t != null)
t.rollback();
he.printStackTrace();

}

return user;
}

public void changePassword(int userid, String newpass) {

Session session = fact.getCurrentSession();
Transaction t = null;
try {

t = session.beginTransaction();
Users user = (Users) session.get(Users.class, userid);
if (user == null)
throw new HibernateException("Record not found!");

user.setPassword(newpass);
session.update(user);
t.commit();

} catch (HibernateException he) {
if (t != null)
t.rollback();

System.out.println("Error: " + he.getMessage());
}

}

public Users getUser(int userid) {

Session session = fact.getCurrentSession();
Transaction t = null;
Users user = null;
try {
t = session.beginTransaction();
user = (Users) session.get(Users.class, userid);
if (user == null)
throw new HibernateException("Record not found!");
t.commit();
} catch (HibernateException he) {

if (t != null)
t.rollback();
he.printStackTrace();
}

return user;
}

}

next post… JSF configaration & navigation

Advertisements

One comment on “JSF 2.0 with hibernate 3.0 tutorial, Online Movie Reviews web-app part 2

  1. Pingback: JSF sample app: part 3 | mahendra's Unlimited Blogs

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