当前位置: 代码迷 >> J2EE >> ssh2整合后调用CRUD方法,findByUserid没有关问题,可findByUsername报错,没找出原因,希望能得到解答
  详细解决方案

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

热度:76   发布时间:2016-04-22 01:53:08.0
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());    }               //执行失败,报错。}