HibernateTemplate.get()得到的结果总是空
我的DAO代码是这样的
package dao;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import wp.Worker;
public class WorkerDao extends HibernateDaoSupport implements ImpWorkerDao{
public Worker getWorker(String empno) {
HibernateTemplate ht = this.getHibernateTemplate();
try{
Worker wk = (Worker)ht.find("from t_worker where empno=?", empno);
return wk;
}
catch(Exception e){
e.printStackTrace();
return null;
}
}
public boolean insertWorker(Worker wk) {
HibernateTemplate ht = this.getHibernateTemplate();
try{
ht.save(wk);
return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
}
public boolean updateWorker(Worker wk) {
HibernateTemplate ht = this.getHibernateTemplate();
try{
ht.update(wk);
return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
}
}
下面是持久化类
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="wp.Worker" table="T_WORKER" lazy="true">
<id name="empno" column="EMPNO" >
<generator class="assigned" />
</id>
<property name="passwd" column="PASSWD" />
<property name="empname" column="EMPNAME" />
<property name="易做图" column="SEX" />
<property name="wage" column="WAGE" />
</class>
</hibernate-mapping>
然后是hibernate的配置文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:127.0.0.1:1521:ORCL
</property>
<property name="connection.username">sys as SYSDBA</property>
<property name="connection.password">brilliance</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="myeclipse.connection.profile">
MyEclipse Derby
</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="wp/Worker.hbm.xml" />
</session-factory>
</hibernate-configuration>
运行的时候没有任何的错误提示,也可以插入数据,但是就是查询的时候返回的结果为null --------------------编程问答-------------------- Worker wk = (Worker)ht.find("from t_worker where empno=?", empno); 改成实体类名
--------------------编程问答-------------------- HibernateTemplate.get();没看到get()呀...
ht.find("from t_worker where empno=?", empno);
那你这里就写错了!不是表名而是实体类名呀。
t_worker换成 Worker
--------------------编程问答-------------------- 我把代码改了一下看行不行,之前的代码是:
--------------------编程问答--------------------
--------------------编程问答-------------------- get里能传string的吗 我没试过 呵呵 传个id 试试吧 好久没用ssh --------------------编程问答--------------------
ht.get(Worker.class, empno);
empno是String类型吧,get()能传String类型的参数吗?
get()抛异常就有点奇怪了啊!get()加载不到数据会返回null --------------------编程问答-------------------- 你是怎么解决这个问题的,我今天也遇到了。 --------------------编程问答-------------------- get(Worker.class,Integer id) --------------------编程问答-------------------- 你的配置文件没配置,看看spring配置文件,配置一下 --------------------编程问答--------------------
<class name="wp.Worker" table="T_WORKER" lazy="true">
配置问题也不在这么,改成
Worker wk = (Worker)ht.find("from wp.Worker where empno=?", empno);
得到后先看结果集是否为空
补充:Java , Java EE