简体   繁体   中英

Select from two table from hibernate?

I have two tables in db employee and department as:

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;

And my mapping classes are as below: 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;
            }              
}

I want to select from two tables with join as EMPLOYEE.DEPTID = DEPARTMENT.DEPTID

I dont want to write query. Here is how I m doing it in test class

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();

But I m getting following exception

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

I m using Hibernate 4.2

Can you please help me as what I m missing in this.

As suggested by @jpprade

Make the following change

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

Thanks NG

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM