我有一个数据库(在postgres中),我正在使用spring 3和hibernate 3,问题是当尝试从数据库中删除或获取一行时,这是我正在使用的类,哦查询后的另一件事,一个没有任何错误,唯一的事情就是那个类User是null
@Repository("userDao")
public class UserDao implements IUserDao {
@Autowired
private SessionFactory sessionFactory;
public void SaveUser(User user) {
sessionFactory.getCurrentSession().save(user);
}
@SuppressWarnings("unchecked")
public List<User> ListUsers() {
List<User> users = (List<User>) sessionFactory.getCurrentSession().createCriteria(User.class).list();
return users;
}
public User GetUserById(Integer id) {
User user = (User) sessionFactory.getCurrentSession().load(User.class, id);
return user;
}
}
顺便说一句,方法、SaveUser 和 ListUser 工作得非常好。
这是模型类
@Entity
@Table(name = "mrbean")
public class User {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
@Column (name = "id")
private Integer id;
@NotNull (message = "not null")
@NotEmpty (message = "empty")
@Column (name = "user_name", length = 20, nullable = false, unique = true)
private String userName;
@NotNull
@NotEmpty (message = "empty password")
@Size (min = 7, max = 20, message = "La contraseña debe tener entre 7 y 20 caracteres")
@Column (name = "password", length = 20, nullable = false)
private String password;
//带有getter和setter
这是执行 User user = (User)sessionFactory.getCurrentSession().load(User.class, id) 时休眠在控制台中显示的内容
Hibernate:从 mrbean user0_ 中选择 user0_.id 作为 id0_0_,user0_.password 作为 password0_0_,user0_.user_name 作为 user3_0_0_ where user0_.id=?