2013-02-09 453 views
1

我剛剛學習hibernate,我試圖查詢表以找到具有特定id的人。當我打電話給criteria.unique結果,沒有錯誤被拋出,但它返回一個空值,任何人都可以幫助我弄清楚最後會發生什麼?Criteria.uniqueResult返回null,但不會引發錯誤

@Entity 
@Table(name="employees") 
public class Employee { 
private long emp_no; 
private Date birth_date; 
private String first_name; 
private String last_name; 
private char gender; 
private Date hire_date; 

private Set<DepartmentEmp> department_emp = new HashSet<DepartmentEmp>(); 
private Set<DepartmentManager> department_manager = new HashSet<DepartmentManager>(); 
private Set<Projects> projects; 
private ContactInfo contactInfo; 


public Employee(){}; 
public Employee(long empt_no, Date birth_date, String first_name, String last_name, 
     char gender, Date hire_date){ 
     this.emp_no = emp_no; 
     this.birth_date = birth_date; 
     this.first_name = first_name; 
     this.last_name = last_name; 
     this.gender = gender; 
     this.hire_date = hire_date; 
} 

@Id 
@Column(name="emp_no") 
    public long getEmp_no() { return emp_no; } 
    public void setEmp_no(long emp_no) {this.emp_no=emp_no;} 


@Column(name="birth_date") 
    public Date getBirth_date() { return birth_date; } 
    public void setBirth_date(Date birth_date) {this.birth_date = birth_date;} 


@Column(name="first_name") 
    public String getFirst_name() { return first_name; } 
    public void setFirst_name(String name) { this.first_name = name; } 



@Column(name="last_name") 
    public String getLast_name() { return last_name; } 
    public void setLast_name(String name) { this.last_name = name; } 


@Column(name="gender") 
    public char getGender() { return gender; } 
    public void setGender(char gender) {this.gender = gender;} 


    @Column(name="hire_date") 
    public Date getHire_date() { return hire_date; } 
    public void setHire_date(Date hire_date) {this.hire_date = hire_date;} 

    @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY) 
    @JoinColumn(name="emp_no") 
    public ContactInfo getContactInfo() { return contactInfo; } 
    public void setContactInfo(ContactInfo info) { this.contactInfo = info; } 

    @OneToMany(mappedBy="employee", targetEntity=DepartmentEmp.class, 
    cascade=CascadeType.ALL) 
    @Fetch(value = FetchMode.SELECT) 
    public Set<DepartmentEmp> getDepartment_emp() { return department_emp; } 
    public void setDepartment_emp(Set<DepartmentEmp> department_emp) { this.department_emp = department_emp; } 



    @OneToMany(mappedBy="employee", targetEntity=DepartmentManager.class, 
    cascade=CascadeType.ALL) 
    @Fetch(value = FetchMode.SELECT) 

    public Set<DepartmentManager> getDepartment_manager() { return department_manager; } 
    public void setDepartment_manager(Set<DepartmentManager> department_manager) { this.department_manager = department_manager; } 

    @ManyToMany 
     @JoinTable(name="employee_projects", 
      joinColumns={@JoinColumn(name="employee_emp_no")}, 
      inverseJoinColumns={@JoinColumn(name="projects_proj_num")}) 
    @Fetch(value = FetchMode.SELECT) 
     public Set<Projects> getProjects() { return projects; } 
     public void setProjects(Set<Projects> projects) { this.projects = projects; } 

    public void print() { 
     System.out.printf("%d: %s %s %s %c %s\n", emp_no, birth_date.toString(), first_name, last_name, gender, hire_date.toString()); 
    } 

    public static void list() 
    { 
     Session session = HibernateContext.getSession(); 
     Criteria criteria = session.createCriteria(Employee.class); 
     criteria.addOrder(Order.asc("emp_no")); 

     List <Employee> employees = criteria.list(); 
     System.out.println("All Employees:"); 
     for (Employee employee : employees) { 
      employee.print(); 
     } 
     session.close(); 
    } 

    //TODO finish 4 methods 

    public static Employee find(long emp_no){ 
     Employee prototype = new Employee(); 
     prototype.setEmp_no(emp_no); 
     Example ex = Example.create(prototype); 

     Session sess = HibernateContext.getSession(); 
     Criteria criteria = sess.createCriteria(Employee.class); 
     criteria.add(ex); 

     Employee e = (Employee) criteria.uniqueResult(); 
     return e; 
    } 
    public static Employee find(String first, String last){ 
     Employee prototype = new Employee(); 
     prototype.setFirst_name(first); 
     prototype.setLast_name(last); 
     Example ex = Example.create(prototype); 

     Session sess = HibernateContext.getSession(); 
     Criteria criteria = sess.createCriteria(Employee.class); 
     criteria.add(ex); 

     Employee e = (Employee) criteria.uniqueResult(); 

     sess.close(); 
     return e; 
    } 
    public static void main(String [] args){ 
      Class klasses [] = {Employee.class, DepartmentEmp.class, DepartmentManager.class, ContactInfo.class, 
           Salaries.class, Titles.class, Department.class, Projects.class}; 
      HibernateContext.addClasses(klasses); 
      Employee.find((long)10001).print(); 
    } 
    } 

HibernateContext:

public class HibernateContext { 

public static AnnotationConfiguration config = null; 
public static SessionFactory factory = null; 

/** 
* sets configuration if null 
*/ 
private static void setConfiguration(){ 
    if(config == null){ 
     config = new AnnotationConfiguration(); 
     config.configure(); 
    } 
} 

/** 
* sets factory if null 
*/ 
private static void setFactory(){ 
    if(factory == null){ 
     setConfiguration(); 
     factory = config.buildSessionFactory(); 
    } 
} 

/** 
* Opens session from the factory 
*/ 
public static Session getSession(){ 
    setFactory(); 
    return factory.openSession(); 
} 

/** 
* Creates a schema from the configuration 
*/ 
public static void createSchema(){ 
    setConfiguration(); 
    (new SchemaExport(config)).create(true, true); 

} 

/** 
* Adds a new class object to the database 
*/ 
public static void addClass(Class c){ 
    setConfiguration(); 
    config.addAnnotatedClass(c); 
} 

/** 
* Adds a list of class objects to the database 
*/ 
public static void addClasses(Class classes[]){ 
    for(Class c : classes){ 
     addClass(c); 
    } 
} 

}

我已經中省略了其它類,因爲他們arent參與此查詢,但是當我運行在員工的主要功能,堆棧跟蹤只是說find.print();空指針異常線。我知道數據庫已經填充並且正在運行的Employee.list()只能找到,所以我正在努力弄清楚究竟是什麼錯誤。謝謝您的幫助!

+0

OKY,所以我意識到,默認的構造函數初始化字符性別作爲「」,而不是零,這使得它,所以它也搜尋查詢尋找性別,我固定通過將其切換爲字符串 – Wonger 2013-02-09 23:47:53

回答

0

隨着出堆棧跟蹤它很難找到什麼是錯,這裏是我猜測:

如果我們看到您打印方法;

public void print() { 
     System.out.printf("%d: %s %s %s %c %s\n", emp_no, birth_date.toString(), 
first_name, last_name, gender, 
hire_date.toString()); 
    } 

即使Employee對象返回正常。在print你得到的 hire_date值,如果你不給hire_dateemployee object並試圖調用hire_date.toString(),它會給null pointer exception

確保如果返回Employee對象有hire_date或不

+0

使用此ID爲10001檢查DB中的員工對象。如果他已經有hire_date,請忽略此答案。 – 2013-02-09 16:59:53

+0

當你回家的時候,我會把確切的堆棧跟蹤放在裏面,但是它所說的是我稱之爲打印的行上的空指針異常。在我的employee.find方法中,criteria.unique result()方法返回null,所以返回的employee對象中根本沒有值。員工ID確實存在,因爲我選擇了數據庫中的一個 – Wonger 2013-02-09 20:22:57

相關問題