hibernate4 使用getCurrentSession().createQuery报错,求助

Roy_Sashulin
重庆医济智联科技有限公司官方账号
2014-07-18 09:51:38
public List findByProperty(String propertyName, Object value) {
log.debug("finding FmUser instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from FmUser as model where model."
+ propertyName + "= ?";
Query queryObject = getCurrentSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}


调试到“getCurrentSession().createQuery”报一个错误:
Source not found for FmUserDAO$$FastClassByCGLIB$$deecd5cd.invoke(int, Object, Object[]) line: not available
提示找不到FmUser,但我在com.fundclien.entity.FmUser是存在的,它的代码是:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

/**
* FmUser entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "FM_USER", schema = "GF", uniqueConstraints = @UniqueConstraint(columnNames = "VC_USER_CODE"))
public class FmUser implements java.io.Serializable {
@Id
@GeneratedValue
@Column(name = "L_USER_ID", unique = true, nullable = false, precision = 10, scale = 0)
public Long getLUserId() {
return this.LUserId;
}

public void setLUserId(Long LUserId) {
this.LUserId = LUserId;
}

@Column(name = "VC_USER_CODE", unique = true, nullable = false, length = 16)
public String getVcUserCode() {
return this.vcUserCode;
}
}


下面是applicationContext.xml配置:

<?xml version="1.0" encoding="UTF-8"?>

<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx">

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url"
value="jdbc:oracle:thin:@127.0.0.1:1521:orcl">
</property>
<property name="username" value="gf"></property>
<property name="password" value="gf"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.fundclien.entity.FmUser</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

<bean
id="fmUserDao" class="com.fundclien.dao.FmUserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="login" class="com.fundclient.actions.loginAction">
<property name="fmUserDao" ref="fmUserDao" />
</bean>

</beans>


求前辈帮我看看是怎么回事啊?我调用FmUser instance = (FmUser) getCurrentSession().get("com.fundclien.entity.FmUser", id);则可以取得。
...全文
349 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Roy_Sashulin 2014-07-18
  • 打赏
  • 举报
回复
引用 8 楼 suciver 的回复:
action com.fundclient.actions.loginA这个action没有result的返回页面,楼主检查下这个action的配置
:-) 我写错了,错误消息是No result defined for action com.fundclient.actions.loginAction and result error
suciver 2014-07-18
  • 打赏
  • 举报
回复
action com.fundclient.actions.loginA这个action没有result的返回页面,楼主检查下这个action的配置
Roy_Sashulin 2014-07-18
  • 打赏
  • 举报
回复
引用 5 楼 whos2002110 的回复:
你这样试一下:

String queryString = "from FmUser as model where model."
                    + propertyName + "=:value";
            Query queryObject = getCurrentSession().createQuery(queryString);
            queryObject.setParameter("value", value);
不行也,还是会报错,这是不是hibernate4的bug啊,或者是不是需要配置什么参数来技能呀
Roy_Sashulin 2014-07-18
  • 打赏
  • 举报
回复
还有一个问题: 我在loginAction的execute函数执行完成后,在跳转页面时报了个错误: HTTP Status 404 - No result defined for action com.fundclient.actions.loginA

public String   execute() throws Exception{  
    	boolean res = fmUserDao.validUser(userName, passWord);
        if(res){  
               return SUCCESS;  
          }else{  
               return ERROR;  
            }  
    }
structs2.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<constant name="struts.objectFactory" value="spring"></constant>  
	<package name="mypack" extends="struts-default">
	    <action name="index">
            <result>/index.jsp</result>
        </action>
        <action name="login" class="com.fundclient.actions.LoginAction">  
        	<result name="success">/index.jsp</result>
            <result name="error">/login.jsp</result>  
        </action>  
     </package>  
</struts>    

whos2002110 2014-07-18
  • 打赏
  • 举报
回复
你这样试一下:

String queryString = "from FmUser as model where model."
                    + propertyName + "=:value";
            Query queryObject = getCurrentSession().createQuery(queryString);
            queryObject.setParameter("value", value);
Roy_Sashulin 2014-07-18
  • 打赏
  • 举报
回复
感谢 lmj623565791 我已经按你指教的改了,果然好用啊。 to 版主大人 fangmingshijie findByProperty(String propertyName, Object value) 是自动生成的,value是propertyName的值。
  • 打赏
  • 举报
回复
queryObject.setParameter(0, value); 这个value应该是FmUser类型,通过返回去get 对应的propertyName。
鸿洋_ 2014-07-18
  • 打赏
  • 举报
回复
如果你喜欢注解,把seesionFactory中的属性 <property name="annotatedClasses"> <list> <value>com.fundclien.entity.FmUser</value> </list> </property> 改成 <property name="packagesToScan"> <value>com.fundclien.entity</value> </property> 你也不嫌累,一个一个加~
Roy_Sashulin 2014-07-18
  • 打赏
  • 举报
回复
我修改为字符串拼接就可以,如果用参数需要做什么配置吗? String queryString = "from FmUser as model where model." + propertyName + "= '"+value+"'"; 修改成这样就可以。
Roy_Sashulin 2014-07-18
  • 打赏
  • 举报
回复
1.参数绑定这个还没有解决。 2.访问已经解决了 <action name="login" class="com.fundclient.actions.loginAction" method="execute"> 改成 <action name="loginAction" class="com.fundclient.actions.loginAction" method="execute"> 就好了。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧