日期:2014-05-19  浏览次数:20805 次

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") @Transactional
public 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(