2016-05-13 103 views
2

我在DB員工和部門兩個表:從兩個表中選擇休眠?

CREATE TABLE test.employee (
    EMPID int(10) unsigned NOT NULL DEFAULT '1', 
    Name varchar(45) NOT NULL DEFAULT '1', 
    DEPTID int(10) unsigned NOT NULL DEFAULT '1', 
    PRIMARY KEY (EMPID), 
    KEY FK_employee_1 (DEPTID), 
    CONSTRAINT FK_employee_1 FOREIGN KEY (DEPTID) REFERENCES department (DEPTID) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

CREATE TABLE test.department (
    DEPTID int(10) unsigned NOT NULL AUTO_INCREMENT, 
    Name varchar(45) NOT NULL, 
    PRIMARY KEY (DEPTID) 
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; 

我的映射類是如下: Employee2.java

@Entity 
@Table(name="EMPLOYEE") 
public class Employee2 { 

      @Id @GeneratedValue 
      @Column(name="EMPID") 
      private String ID; 

      @Column(name="Name") 
      private String Name; 

      @Column(name="DEPTID") 
      private String DepartmentID; 

      public Employee2(String iD, String name, String departmentID){ 
          ID = iD; 
          Name = name; 
          DepartmentID = departmentID;         
      } 

      public Employee2(){ 

      } 

      public String getID() { 
          return ID; 
      } 

      public void setID(String iD) { 
          ID = iD; 
      } 

      public String getName() { 
          return Name; 
      } 

      public void setName(String name) { 
          Name = name; 
      } 

      public String getDepartmentID() { 
          return DepartmentID; 
      } 

      public void setDepartmentID(String departmentID) { 
          DepartmentID = departmentID; 
      } 

      @OneToOne 
      @JoinColumn(table = "DEPARTMENT", name = "DEPTID", referencedColumnName="DEPTID") 

      private Department2 ec; 

      public Department2 getEc() { 
          return ec; 
      } 

      public void setEc(Department2 ec) { 
          this.ec = ec; 
      }    
} 

Department2.java

@Entity 
@Table(name="DEPARTMENT") 
public class Department2 { 

      @Id @GeneratedValue 
      @Column(name="DEPTID") 

      private String ID; 

      @Column(name="Name") 
      private String Name; 

      public Department2(String iD, String name) { 
          ID = iD; 
          Name = name; 
      } 

      public Department2(){ 

      } 

      public String getID() { 
          return ID; 
      } 

      public void setID(String iD) { 
          ID = iD; 
      } 

      public String getName() { 
          return Name; 
      } 

      public void setName(String name) { 
          Name = name; 
      }    
} 

我想從兩個標籤中選擇加入爲EMPLOYEE.DEPTID = DEPTIMENT.DEPTID

我不想寫查詢。 這裏是我米如何在測試類

tx = session.beginTransaction(); 
Criteria criteria = session.createCriteria(Employee2.class, "employee").             
createCriteria("employee.ec", JoinType.INNER_JOIN); 
List<Equipment2> rows = criteria.list(); 
System.out.println(rows.size()); 
tx.commit(); 

這樣做,但我米獲得以下異常

Failed to create sessionFactory object.org.hibernate.AnnotationException: Cannot find the expected secondary table: no DEPARTMENT available for com.cts.sm.Employee2 
Exception in thread "main" java.lang.ExceptionInInitializerError 

我米使用Hibernate 4.2

你能幫幫我,什麼我爲失蹤在這。

+0

您可以檢查每個部門ID,有在DEPARTMENT表或不連續? –

+0

檢查你的HBM文件,如果所有的映射都是正確的 –

+0

@KP我使用類實體進行映射... – Roy

回答

1

正如@jpprade建議

做出如下變化

@ManyToOne 
    @JoinColumn(name ="DEPTID", updatable = false, insertable = false) 
    private Department2 ec; 
//getter setter 

感謝 N G -

+1

上面的答案是適合我的,但我想這是Hibernate的限制。兩個連接表的列名應該完全相同以便連接發生。 – Roy