在beans.xml配置文件(applicationContext.xml)中将
<property name="mappingResources">
<list>
<value>cw/test/bean/Employee.hbm.xml</value>
</list>
</property>
的映射关系删除,改成:
<property name="annotatedClasses">
<list><value>cw.test.bean.Employee</value></list>
</property>
其中cw.test.bean.Employee为实体类
具体的实体类改成如下形式,加入注解:
@Entity
@Table(name="employee")
public class Employee implements java.io.Serializable{
@Id
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@Enumerated(EnumType.STRING)
private Gender gender = Gender.MAN;
public Employee(){}
public Employee(String username,String password){
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
}
需要注意的是如果某个字段是用的枚举类型,那么需要用到Enumerated注解,并且在实体类上要实现java.io.Serializable类。