public List findByExample(Username instance) { log.debug("finding Username instance by example"); try { List results = getSession() .createCriteria("com.shenzhenair.medet.bo.users.Username") .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; } }
我的方法是这样写的,看你的也差不多,只有检查配置那块儿的问题了,然后放在IDE中单步调试,查看变量的变化情况吧 public User getUser(Long id) { User user = (User) getHibernateTemplate().get(User.class, id); if (user == null) { throw new ObjectRetrievalFailureException(User.class, id); } return user; }
System.out.println(name.getDepartment());上面那句话是不是放在你的usernameDaoHibernate类中呢,然后你的配置文件又如何配的呢,贴出来吧
//do nothing
}
public void save(Username transientInstance) {
log.debug("saving Username instance");
try {
getHibernateTemplate().saveOrUpdate(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Username persistentInstance) {
log.debug("deleting Username instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Username findById( java.lang.String id) {
log.debug("getting Username instance with id: " + id);
try {
Username instance = (Username) getHibernateTemplate()
.get("com.shenzhenair.medet.bo.users.Username", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Username instance) {
log.debug("finding Username instance by example");
try {
List results = getSession()
.createCriteria("com.shenzhenair.medet.bo.users.Username")
.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;
}
}
public Username merge(Username detachedInstance) {
log.debug("merging Username instance");
try {
Username result = (Username) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} public void attachDirty(Username instance) {
log.debug("attaching dirty Username instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Username instance) {
log.debug("attaching clean Username instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public static UsernameDAO getFromApplicationContext(ApplicationContext ctx) {
return (UsernameDAO) ctx.getBean("UsernameDAO");
}
}
public User getUser(Long id) {
User user = (User) getHibernateTemplate().get(User.class, id);
if (user == null) {
throw new ObjectRetrievalFailureException(User.class, id);
}
return user;
}