I have a database (in postgres) and I’m working with spring 3 and hibernate 3, the problem is when try to delete or get a row from database, here is the class that I’m using, ohh another thing after the query, a don’t have any mistake, the only thing es that class User is 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;
}
}
by the way, methods, SaveUser and ListUser work really good.
this is the model class
@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;
//with getters and setters
this is what hibernate show in console when
User user = (User)sessionFactory.getCurrentSession().load(User.class, id) is executed
Hibernate: select user0_.id as id0_0_, user0_.password as password0_0_, user0_.user_name as user3_0_0_ from mrbean user0_ where user0_.id=?
I would use “get” instead of “load” because load() creates a proxy of User (see Hibernate ref doc )
so i would try : User user = (User) sessionFactory.getCurrentSession().get(User.class, id);