DAO类
package edu.fjtu.hibernate;import java.util.List;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;/**
* Data access object (DAO) for domain model class TabUser.
*
* @see .TabUser
* @author MyEclipse - Hibernate Tools
*/
public class TabUserDAO extends HibernateDaoSupport { private static final Log log = LogFactory.getLog(TabUserDAO.class); // property constants
public static final String USERNAME = "username"; public static final String PASSWORD = "password"; protected void initDao() {
// do nothing
} public void save(TabUser transientInstance) {
log.debug("saving TabUser instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
} public void delete(TabUser persistentInstance) {
log.debug("deleting TabUser instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
} public TabUser findById(java.lang.Long id) {
log.debug("getting TabUser instance with id: " + id);
try {
TabUser instance = (TabUser) getHibernateTemplate().get("TabUser",
id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
} public List findByExample(TabUser instance) {
log.debug("finding TabUser instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
} public List findByProperty(String propertyName, Object value) {
log.debug("finding TabUser instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from TabUser as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
} public List findByUsername(Object username) {
return findByProperty(USERNAME, username);
} public List findByPassword(Object password) {
return findByProperty(PASSWORD, password);
} public TabUser merge(TabUser detachedInstance) {
log.debug("merging TabUser instance");
try {
TabUser result = (TabUser) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} public void attachDirty(TabUser instance) {
log.debug("attaching dirty TabUser instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public void attachClean(TabUser instance) {
log.debug("attaching clean TabUser instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public static TabUserDAO getFromApplicationContext(ApplicationContext ctx) {
return (TabUserDAO) ctx.getBean("TabUserDAO");
} public static void main(String args[]) {
String[] paths = { "/WebRoot/WEB-INF/applicationContext.xml" };
ApplicationContext ctx = new FileSystemXmlApplicationContext(paths);
TabUserDAO dao = (TabUserDAO) ctx.getBean("TabUserDAO");
System.out.println(dao.findById(new Long(1))); }
}
package edu.fjtu.hibernate;import java.util.List;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;/**
* Data access object (DAO) for domain model class TabUser.
*
* @see .TabUser
* @author MyEclipse - Hibernate Tools
*/
public class TabUserDAO extends HibernateDaoSupport { private static final Log log = LogFactory.getLog(TabUserDAO.class); // property constants
public static final String USERNAME = "username"; public static final String PASSWORD = "password"; protected void initDao() {
// do nothing
} public void save(TabUser transientInstance) {
log.debug("saving TabUser instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
} public void delete(TabUser persistentInstance) {
log.debug("deleting TabUser instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
} public TabUser findById(java.lang.Long id) {
log.debug("getting TabUser instance with id: " + id);
try {
TabUser instance = (TabUser) getHibernateTemplate().get("TabUser",
id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
} public List findByExample(TabUser instance) {
log.debug("finding TabUser instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
} public List findByProperty(String propertyName, Object value) {
log.debug("finding TabUser instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from TabUser as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
} public List findByUsername(Object username) {
return findByProperty(USERNAME, username);
} public List findByPassword(Object password) {
return findByProperty(PASSWORD, password);
} public TabUser merge(TabUser detachedInstance) {
log.debug("merging TabUser instance");
try {
TabUser result = (TabUser) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} public void attachDirty(TabUser instance) {
log.debug("attaching dirty TabUser instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public void attachClean(TabUser instance) {
log.debug("attaching clean TabUser instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public static TabUserDAO getFromApplicationContext(ApplicationContext ctx) {
return (TabUserDAO) ctx.getBean("TabUserDAO");
} public static void main(String args[]) {
String[] paths = { "/WebRoot/WEB-INF/applicationContext.xml" };
ApplicationContext ctx = new FileSystemXmlApplicationContext(paths);
TabUserDAO dao = (TabUserDAO) ctx.getBean("TabUserDAO");
System.out.println(dao.findById(new Long(1))); }
}
解决方案 »
- weblogic.xml的DTD声明问题
- 线程同步 类静态属性同步问题
- struts2+spring+hibernate 三大框架怎么整合
- 反射equals方法与hibernate load
- 急!用struts hibernate 从sql server2005读取数据和在jsp页面中显示数据
- java程序定时运行cs模式
- 在网页中怎么调用win32程序? 急!
- 急等答案!
- 我用的springmvc框架,想用自己写的一个jdbc连接数据库,从而操作数据,可以用到注解来做事务么?
- tomcat 启动日志问题,求解
- 想参加点什么J2EE竞赛呢?
- (求助)在JBuilder 2006无发启动JSF写的程序。。(在线等)
org.hibernate.MappingException: Unknown entity: TabUser
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:569)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:62)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:621)
at org.springframework.orm.hibernate3.HibernateTemplate$2.doInHibernate(HibernateTemplate.java:471)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:465)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:459)
at edu.fjtu.hibernate.TabUserDAO.findById(TabUserDAO.java:56)
at edu.fjtu.hibernate.TabUserDAO.main(TabUserDAO.java:142)
Exception in thread "main"
我如果包TabUser user.hbm.xml DAO类等文件拷贝到SRC目录下 修改相应的配置文件就可以了 但是放在edu.fjtu.hibernate目录下就报异常了-,-