我正在使用 SQLQuery 从使用休眠的数据库中选择一些数据。尝试选择 * 时它可以工作,但只需要几列时它会返回:无效的列名搜索了有关此问题的其他主题,但没有帮助;这是我的代码:
User.java 声明变量的地方:
公共类用户实现可序列化{
private static final long serialVersionUID = -1798070786993154676L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_Sequence")
@Column(name = "ID", unique = true, nullable = false)
@SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ")
private Integer id;
@Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100)
private String firstname;
@Column(name = "LAST_NAME", unique = false, nullable = false, length = 100)
private String lastname;
@Column(name = "DATE_OB", unique = false, nullable = false, length = 100)
private String birthdate;
@Column(name = "DATE_OW", unique = false, nullable = false, length = 100)
private String startdate;
@Column(name = "DEPARTMENT", unique = false, nullable = false, length = 100)
private String department;
@Column(name = "POSITION", unique = false, nullable = false, length = 100)
private String position;
//带有getter和setter
尝试选择的部分:
public static List<User> getAllUser()
{
List<User> result = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
String sql = "select first_name from employee";
SQLQuery query = session.createSQLQuery (sql).addEntity(User.class);
result = query.list();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return result;
}
我想我不需要创建单独的休眠映射文件,因为它已经在 User 类中完成了。请帮助找出可能是什么原因。