0
我已經將java類名稱設置爲「Employee」並將db表名稱設置爲「EMPLOYEE」。該表有很多列,但我已經在員工類中聲明瞭兩個變量。以下是Employee類的代碼:在休眠時選擇查詢
package com.mmi.education;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="EMPLOYEES")
public class Employee {
private int id;
private String firstname;
private String lastname;
@Id
@Column(name="EMPLOYEE_ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="FIRST_NAME")
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
@Column(name="LAST_NAME")
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
在TestEmployee類,我寫了下面的代碼:
package com.mmi.education;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class TestEmployee {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config= new AnnotationConfiguration();
config.configure("hibernate.cfg.xml");
SessionFactory factory = config.buildSessionFactory();
Session session = factory.getCurrentSession();
//session.beginTransaction();
Query query = session.createQuery("FROM Employee");
List<Employee> employees = query.list();
for(Employee employee:employees){
System.out.println("ID=>"+employee.getId()+"\tFirstName=>"+employee.getFirstname()+"\tLastName=>"+employee.getLastname());
}
}
}
首先,我很困惑是否寫入「FROM僱員」或「FROM員工」。 其次,在POJO類(Employee)中聲明兩個變量是安全的,而表中有很多列。
最後,關於執行的代碼,我面臨着以下異常:
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: Employee is not mapped [FROM Employee]
我的數據庫安裝在本地(Oracle 10g中XE)
謝謝JBNizet。 –