首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

ssh2整合后调用CRUD方法,findByUserid没有关问题,可findByUsername报错,没找出原因,希望能得到解答

2012-03-30 
ssh2整合后调用CRUD方法,findByUserid没问题,可findByUsername报错,没找出原因,希望能得到解答。1. CMSUser

ssh2整合后调用CRUD方法,findByUserid没问题,可findByUsername报错,没找出原因,希望能得到解答。
1. CMSUser对象定义

Java code
public class CMSUser {    private Integer userid;    private String username;    private String password;        public CMSUser(){}    public CMSUser(String username, String password){        this.username = username;        this.password = password;    }        public Integer getUserid() {        return userid;    }        ....//以下省略get,set方法.

2. CMSUser.hbm.xml文件
XML code
<?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 package="com.ailincms.bean">    <class name="CMSUser">        <id name="userid" type="java.lang.Integer">            <generator class="native" />        </id>        <property name="username" length="20" not-null="true" unique="true"/>        <property name="password" length="20" not-null="true" />    </class></hibernate-mapping>

3. CMSUserService接口 //省略代码
4. CMSUserServiceBean方法实现
Java code
@Service(value="cMSUserServiceBean") @Transactionalpublic class CMSUserServiceBean implements CMSUserService {    @Resource SessionFactory factory;    public void createCMSUser(CMSUser cmsuser) {        factory.getCurrentSession().persist(cmsuser);    }    public void updateCMSUser(CMSUser cmsuser) {        factory.getCurrentSession().merge(cmsuser);    }    public void deleteByUsername(String... usernames) {        for(String username : usernames){            factory.getCurrentSession().delete(factory.getCurrentSession().load(CMSUser.class, username));        }    }    public void deleteByUserid(Integer userid) {        factory.getCurrentSession().delete(factory.getCurrentSession().get(CMSUser.class, userid));    }        @Transactional(propagation=Propagation.NOT_SUPPORTED)    public CMSUser findByUsername(String username) {//return (CMSUser)factory.getCurrentSession().delete(factory.getCurrentSession().get(CMSUser.class, userid));        return (CMSUser)factory.getCurrentSession().createQuery("from CMSUser as user where user.username='"+username+"'");//两种方法都试了,都报错。    }        @Transactional(propagation=Propagation.NOT_SUPPORTED)    public CMSUser findByUserid(Integer userid) {        return (CMSUser)factory.getCurrentSession().get(CMSUser.class, userid);    }        @SuppressWarnings("unchecked")    @Transactional(propagation=Propagation.NOT_SUPPORTED)    public List<CMSUser> getAllCMSUser() {        return factory.getCurrentSession().createQuery("from CMSUser").list();    }    }

5. Junit测试
Java code
public class CMSUser_CRUD {        private static CMSUserService userService;    @BeforeClass    public static void setUpBeforeClass() throws Exception {        ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");        userService  = (CMSUserService)act.getBean("cMSUserServiceBean");    }    @Test    public void createCMSUser() {        userService.createCMSUser(new CMSUser("xql","linlin520"));    //执行成功,未报错。    }        @Test    public void findByUserid(){        CMSUser cmsUser = userService.findByUserid(1);        ////执行成功,未报错。        System.out.println(cmsUser.getPassword());    }        @Test    public void findByUsername(){        CMSUser cmsUser = (CMSUser) userService.findByUsername("xql");         System.out.println(cmsUser.getPassword());    }               //执行失败,报错。} 



报错信息如下:
java.lang.ClassCastException: org.hibernate.impl.QueryImpl cannot be cast to com.ailincms.bean.CMSUser
at com.ailincms.service.impl.CMSUserServiceBean.findByUsername(CMSUserServiceBean.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy10.findByUsername(Unknown Source)
at com.ailincms.Junit.CMSUser_CRUD.findByUsername(CMSUser_CRUD.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)




[解决办法]
org.hibernate.impl.QueryImpl cannot be cast to com.ailincms.bean.CMSUser
QueryImpl 不能转换为CMSUser类型,factory.getCurrentSession().createQuery("from CMSUser as user where user.username='"+username+"'")(这里还有后续方法);楼主你查看下吧,我好像记得是.list()的方法,然后这个查询也不是很好,中文名称有可能是相同的,那样你就不能确定其唯一性。

热点排行