HibernateUtil.java
package com.hibernate.util;
import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
final public class HibernateUtil {
private static SessionFactory sessionFactory;
private HibernateUtil(){}
static{
Configuration cfg=new Configuration();
cfg.configure();
sessionFactory=cfg.buildSessionFactory();
}
/*
* 获取Hibernate SessionFactory
* @return
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/*
* 获取Hibernate Session
* @return
*/
public static Session getSession(){
return sessionFactory.openSession();
}
/*
* 根据Class和id查询查询出一个对象
* @param clazz
* @param id
* @return
*/
public static Object getObject(Class clazz,Serializable id){
Object object=null;
Session s=null;
try {
s=getSession();
object=s.get(clazz, id);
} catch (HibernateException e) {
System.err.println("Hibernate Get Object Error:\n"+e.getMessage());
object=null;
} finally{
if (s!=null) {
s.close();
}
}
return object;
}
/*
* 根据hql语句查询出一个对象
* @param hql
* @return
*/
public static Object getObject(String hql){
Object object=null;
Session s=null;
try {
s=getSession();
Query query=s.createQuery(hql);
List list=query.list();
if (list!=null && list.size()>0) {
object=list.get(0);
}
} catch (Exception e) {
System.err.println("Hibernate Get Object Error:\n"+e.getMessage());
object=null;
}finally{
if (s!=null) {
s.close();
}
}
return object;
}
/*
* 查询符合该hql语句的所有对象
* @param hql
* @return
*/
public static List getList(String hql){
List list=null;
Session s=null;
try {
s=getSession();
Query query=s.createQuery(hql);
list=query.list();
} catch (Exception e) {
System.err.println("Hibernate Get List Object Error:\n"+e.getMessage());
list=null;
}finally{
if (s!=null) {
s.close();
}
}
return list;
}
/*
* 增加对象,在数据库里面增加对象
* @param obj
* @return
*/
public static int addObject(Object obj){
int status=-1;
Session s=null;
Transaction ts=null;
try {
s=getSession();
ts=s.beginTransaction();
s.save(obj);
ts.commit();
status=1;
} catch (HibernateException e) {
if (ts!=null) {
ts.rollback();
}
System.err.println("Hibernate Insert Object Error:"+e.getMessage());
status=-1;
} finally{
if (s!=null) {
s.close();
}
}
return status;
}
/*
* 根据对象内容删除对象
* @param obj
* @return
*/
public static int deleteObject(Object obj){
int status=-1;
Session s=null;
Transaction ts=null;
try {
s=getSession();
ts=s.beginTransaction();
s.delete(obj);
ts.commit();
status=1;
} catch (HibernateException e) {
if (ts!=null) {
ts.rollback();
}
System.err.println("Hibernate Delete Object Error:\n"+e.getMessage());
status=0;
} finally{
if (s!=null) {
s.close();
}
}
return status;
}
/*
* 根据类和id删除对象
* @param clazz
* @param id
* @return
*/
public static int deleteObject(Class clazz,Serializable id){
int status=-1;
Session s=null;
Transaction ts=null;
try {
s=getSession();
Object obj=s.get(clazz, id);
if(obj!=null){
ts=s.beginTransaction();
s.delete(obj);
ts.commit();
status=1;
}else{
status=0;
}
} catch (HibernateException e) {
if (ts!=null) {
ts.rollback();
}
System.err.println("Hibernate Delete Object Error:\n"+e.getMessage());
status=-1;
} finally{
if (s!=null) {
s.close();
}
}
return status;
}
/*
* 根据对象内容修改对象
* @param obj
* @return
*/
public static int updateObject(Object obj){
int status=-1;
Session s=null;
Transaction ts=null;
try {
s=getSession();
ts=s.beginTransaction();
s.update(obj);
ts.commit();
status=1;
} catch (HibernateException e) {
if (ts!=null) {
ts.rollback();
}
System.err.println("Hibernate Update Object Error:\n"+e.getMessage());
status=-1;
} finally{
if (s!=null) {
s.close();
}
}
return status;
}
}
调用方法:
package com.hibernate.domain;
import java.util.Date;
import com.hibernate.util.HibernateUtil;
public class Base {
public static void main(String[] args){
User user=new User();
user.setName("word");
user.setBirthday(new Date());
HibernateUtil.addObject(user);
User u=getUser(user.getId());
System.out.println(u);
//System.out.println(user.getId());
/*
user=new User();
user.setName("name");
user.setBirthday(new Date());
HibernateExecute.addObject(user);
System.out.println(user.getId());
*/
System.out.println("end");
}
public static User getUser(int id){
Class cls=User.class;
User user=(User)HibernateUtil.getObject(cls, id);
return user;
}
}
package com.hibernate.util;
import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
final public class HibernateUtil {
private static SessionFactory sessionFactory;
private HibernateUtil(){}
static{
Configuration cfg=new Configuration();
cfg.configure();
sessionFactory=cfg.buildSessionFactory();
}
/*
* 获取Hibernate SessionFactory
* @return
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/*
* 获取Hibernate Session
* @return
*/
public static Session getSession(){
return sessionFactory.openSession();
}
/*
* 根据Class和id查询查询出一个对象
* @param clazz
* @param id
* @return
*/
public static Object getObject(Class clazz,Serializable id){
Object object=null;
Session s=null;
try {
s=getSession();
object=s.get(clazz, id);
} catch (HibernateException e) {
System.err.println("Hibernate Get Object Error:\n"+e.getMessage());
object=null;
} finally{
if (s!=null) {
s.close();
}
}
return object;
}
/*
* 根据hql语句查询出一个对象
* @param hql
* @return
*/
public static Object getObject(String hql){
Object object=null;
Session s=null;
try {
s=getSession();
Query query=s.createQuery(hql);
List list=query.list();
if (list!=null && list.size()>0) {
object=list.get(0);
}
} catch (Exception e) {
System.err.println("Hibernate Get Object Error:\n"+e.getMessage());
object=null;
}finally{
if (s!=null) {
s.close();
}
}
return object;
}
/*
* 查询符合该hql语句的所有对象
* @param hql
* @return
*/
public static List getList(String hql){
List list=null;
Session s=null;
try {
s=getSession();
Query query=s.createQuery(hql);
list=query.list();
} catch (Exception e) {
System.err.println("Hibernate Get List Object Error:\n"+e.getMessage());
list=null;
}finally{
if (s!=null) {
s.close();
}
}
return list;
}
/*
* 增加对象,在数据库里面增加对象
* @param obj
* @return
*/
public static int addObject(Object obj){
int status=-1;
Session s=null;
Transaction ts=null;
try {
s=getSession();
ts=s.beginTransaction();
s.save(obj);
ts.commit();
status=1;
} catch (HibernateException e) {
if (ts!=null) {
ts.rollback();
}
System.err.println("Hibernate Insert Object Error:"+e.getMessage());
status=-1;
} finally{
if (s!=null) {
s.close();
}
}
return status;
}
/*
* 根据对象内容删除对象
* @param obj
* @return
*/
public static int deleteObject(Object obj){
int status=-1;
Session s=null;
Transaction ts=null;
try {
s=getSession();
ts=s.beginTransaction();
s.delete(obj);
ts.commit();
status=1;
} catch (HibernateException e) {
if (ts!=null) {
ts.rollback();
}
System.err.println("Hibernate Delete Object Error:\n"+e.getMessage());
status=0;
} finally{
if (s!=null) {
s.close();
}
}
return status;
}
/*
* 根据类和id删除对象
* @param clazz
* @param id
* @return
*/
public static int deleteObject(Class clazz,Serializable id){
int status=-1;
Session s=null;
Transaction ts=null;
try {
s=getSession();
Object obj=s.get(clazz, id);
if(obj!=null){
ts=s.beginTransaction();
s.delete(obj);
ts.commit();
status=1;
}else{
status=0;
}
} catch (HibernateException e) {
if (ts!=null) {
ts.rollback();
}
System.err.println("Hibernate Delete Object Error:\n"+e.getMessage());
status=-1;
} finally{
if (s!=null) {
s.close();
}
}
return status;
}
/*
* 根据对象内容修改对象
* @param obj
* @return
*/
public static int updateObject(Object obj){
int status=-1;
Session s=null;
Transaction ts=null;
try {
s=getSession();
ts=s.beginTransaction();
s.update(obj);
ts.commit();
status=1;
} catch (HibernateException e) {
if (ts!=null) {
ts.rollback();
}
System.err.println("Hibernate Update Object Error:\n"+e.getMessage());
status=-1;
} finally{
if (s!=null) {
s.close();
}
}
return status;
}
}
调用方法:
package com.hibernate.domain;
import java.util.Date;
import com.hibernate.util.HibernateUtil;
public class Base {
public static void main(String[] args){
User user=new User();
user.setName("word");
user.setBirthday(new Date());
HibernateUtil.addObject(user);
User u=getUser(user.getId());
System.out.println(u);
//System.out.println(user.getId());
/*
user=new User();
user.setName("name");
user.setBirthday(new Date());
HibernateExecute.addObject(user);
System.out.println(user.getId());
*/
System.out.println("end");
}
public static User getUser(int id){
Class cls=User.class;
User user=(User)HibernateUtil.getObject(cls, id);
return user;
}
}