package com.cn.mydao;import com.cn.hibernate.hibernateUtil;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;/**
* Data access object (DAO) for domain model class User.
* @see com.cn.mydao.User
* @author MyEclipse - Hibernate Tools
*/
public class UserDAO extends hibernateUtil { private static final Log log = LogFactory.getLog(UserDAO.class);
private Session session = null ;
// 在构造方法之中实例化session对象
public UserDAO()
{
// 找到Hibernate配置
Configuration config = new Configuration().configure() ;
// 从配置中取出SessionFactory
SessionFactory factory = config.buildSessionFactory() ;
// 从SessionFactory中取出一个Session
this.session = factory.openSession() ;
}
// 所有的操作都是通过session进行的
// 向数据库中增加数据
public void update(User uInstance){
log.debug("update User instance");
try{
//getSession().beginTransaction();
//tx.begin();
getSession().update(uInstance);
getSession().beginTransaction().commit();
log.debug("update successful");
}catch(RuntimeException re){
log.error("update failed",re);
throw re;
}
finally{
this.closeSession();
}
}
public void save(User transientInstance) {
log.debug("saving User instance");
try {
getSession().save(transientInstance);
getSession().beginTransaction().commit();
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
finally{
closeSession();
}
}
public void delete(User persistentInstance) {
log.debug("deleting User instance");
try {
getSession().delete(persistentInstance);
getSession().beginTransaction().commit();
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
finally{
closeSession();
}
}
public User findById( java.lang.Integer id) {
log.debug("getting User instance with id: " + id);
try {
User instance = (User) getSession().get("com.cn.mydao.User", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
finally{
closeSession();
}
}
public List findByExample(User instance) {
log.debug("finding User instance by example");
try {
List results = getSession()
.createCriteria("com.cn.mydao.User")
.add(Example.create(instance))
.list();
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}finally{
closeSession();
}
}
public List findByProperty(String propertyName) {
// log.debug("finding User instance with property: " + propertyName
// + ", value: " + value);
try {
List l = null ;
String hql = "FROM User as s WHERE s.useppp='"+propertyName+"'" ;
Query q = this.session.createQuery(hql) ;
l = q.list() ;
return l ;
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
finally{
closeSession();
}
} public User merge(User detachedInstance) {
log.debug("merging User instance");
try {
User result = (User) getSession()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
finally{
closeSession();
}
} public void attachDirty(User instance) {
log.debug("attaching dirty User instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
finally{
closeSession();
}
}
public void attachClean(User instance) {
log.debug("attaching clean User instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
finally{
closeSession();
}
}
}一到刚才标记的地方就执行不下去,String hql = "FROM User as s WHERE s.useppp='"+propertyName+"'" ;[/color]
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;/**
* Data access object (DAO) for domain model class User.
* @see com.cn.mydao.User
* @author MyEclipse - Hibernate Tools
*/
public class UserDAO extends hibernateUtil { private static final Log log = LogFactory.getLog(UserDAO.class);
private Session session = null ;
// 在构造方法之中实例化session对象
public UserDAO()
{
// 找到Hibernate配置
Configuration config = new Configuration().configure() ;
// 从配置中取出SessionFactory
SessionFactory factory = config.buildSessionFactory() ;
// 从SessionFactory中取出一个Session
this.session = factory.openSession() ;
}
// 所有的操作都是通过session进行的
// 向数据库中增加数据
public void update(User uInstance){
log.debug("update User instance");
try{
//getSession().beginTransaction();
//tx.begin();
getSession().update(uInstance);
getSession().beginTransaction().commit();
log.debug("update successful");
}catch(RuntimeException re){
log.error("update failed",re);
throw re;
}
finally{
this.closeSession();
}
}
public void save(User transientInstance) {
log.debug("saving User instance");
try {
getSession().save(transientInstance);
getSession().beginTransaction().commit();
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
finally{
closeSession();
}
}
public void delete(User persistentInstance) {
log.debug("deleting User instance");
try {
getSession().delete(persistentInstance);
getSession().beginTransaction().commit();
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
finally{
closeSession();
}
}
public User findById( java.lang.Integer id) {
log.debug("getting User instance with id: " + id);
try {
User instance = (User) getSession().get("com.cn.mydao.User", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
finally{
closeSession();
}
}
public List findByExample(User instance) {
log.debug("finding User instance by example");
try {
List results = getSession()
.createCriteria("com.cn.mydao.User")
.add(Example.create(instance))
.list();
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}finally{
closeSession();
}
}
public List findByProperty(String propertyName) {
// log.debug("finding User instance with property: " + propertyName
// + ", value: " + value);
try {
List l = null ;
String hql = "FROM User as s WHERE s.useppp='"+propertyName+"'" ;
Query q = this.session.createQuery(hql) ;
l = q.list() ;
return l ;
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
finally{
closeSession();
}
} public User merge(User detachedInstance) {
log.debug("merging User instance");
try {
User result = (User) getSession()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
finally{
closeSession();
}
} public void attachDirty(User instance) {
log.debug("attaching dirty User instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
finally{
closeSession();
}
}
public void attachClean(User instance) {
log.debug("attaching clean User instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
finally{
closeSession();
}
}
}一到刚才标记的地方就执行不下去,String hql = "FROM User as s WHERE s.useppp='"+propertyName+"'" ;[/color]
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货