I have been searched this problem for a long time, but the answers didn't help.
The results are the same, maybe the reasons are different.
The model associated with database by using Table annotation.
The model as follows:
@Entity
@Table(name = "ts_user")
public class User {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", phone='" + phone + '\'' +
'}';
}
}
The DAO as follows:
@Repository
public class HibernateUserDAO implements UserDAO {
private SessionFactory sessionFactory;
@Autowired
public HibernateUserDAO(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session currentSession() {
return sessionFactory.getCurrentSession();
}
@SuppressWarnings("unchecked")
@Override
public List<User> list() {
Session session = this.sessionFactory.openSession();
List<User> users = session.createQuery("from ts_user").list();
session.close();
return users;
}
}
xml as follows:
答案 0 :(得分:0)
您在这里使用的是表名而不是entityName。应在此处使用实体名称,即类的名称。
@Override
public List<User> list() {
Session session = this.sessionFactory.openSession();
List<User> users = session.createQuery("select u from User u").list();
session.close();
return users;
}