从休眠中的两个表中选择?

时间:2016-05-13 06:07:24

标签: java hibernate

我在db employee和department中有两个表:

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 = DEPARTMENT.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

你可以帮助我,因为我在这里缺少了。

1 个答案:

答案 0 :(得分:1)

正如@jpprade所建议的

进行以下更改

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

由于 N G