简体   繁体   中英

Hibernate mapping with two tables

I am trying to learn Hibernate and I could create some simple CRUD operation using a Single Class and Single Table. I am just reading the Hibernate Doc and some online tutorial.

But I have a problem on how to define this relationship with two tables involved. I basically have an Employee table with this structure.

CREATE TABLE EMPLOYEE
(
 EMP_ID VARCHAR(10) NOT NULL,
 EMP_FIRST_NAME VARCHAR(30) NOT NULL,
 EMP_LAST_NAME VARCHAR(30) NOT NULL,
 STATUS_ID INT NOT NULL,
 PRIMARY KEY (EMP_ID)
);

The STATUS_ID field references another table. STATUS_DESC can either be 'PERMANENT', 'CONTRACTUAL', 'ON-DEMAND'

CREATE TABLE EMP_STATUS
(
 STATUS_ID VARCHAR(10) NOT NULL,
 STATUS_DESC VARCHAR(100) ,
 PRIMARY KEY (STATUS_ID)
);

I am thinking of having an Entity class like this. Now my goal is to return list of Employee object with status, but I don't know how to go about on doing this.

@Entity
public class Employee{
 //other private instance
 private EmployeeStatus empStatus;
 //getters and setters.
}

public class EmployeeStatus{
 private int statusID;
 private String statusDesc;
 //getters and setters
}

You want to know how to map it? ManyToOne?

Employee.java

@Entity
public class Employee{
   //other private instance

   @JoinColumn(name = "empStatus", referencedColumnName = "yourColName")
   @ManyToOne(optional = false)
   private EmployeeStatus empStatus;

   //getters and setters.
}

Dont forget to change "referencedColumnName" value...

EmployeeStatus.java

@Entity
public class EmployeeStatus{
   @Id //this is your pk?
   private int statusID;
   private String statusDesc;

   @OneToMany(cascade = CascadeType.ALL, mappedBy = "empStatus", fetch = FetchType.LAZY) //or EAGER
   private List<Employee> empList;

   //getters and setters
}

To create a relationship between two tables you need to decide:

Is the relationship bi-directional? That is, do the statuses know the employees or not? If no then it is uni-directional. In that case you can add the annotation on the Employee class like this:

@ManyToOne
@JoinColumn(name = "status")
private EmployeeStatus empStatus;

And there is a few other options that you may add.

You can do what you are doing, but I would suggest, if the status can only be one of three values, create an Enum with the three values. No need for a separate table.

The downside for this is you need to create a hibernate custom type (the code is on the wiki) to support persisting enums.

A simpler answer is to not use a secondary table, and just save the status as a String on the domain object. You can put business logic on your model to ensure the String is in the list of acceptable values.

If you really want to use a relationship between two entities, then check out the hibernate docs on many-to-one relationships.

You can use HQL to query the entities. Like so

Query q = s.createQuery("from Employee as e where e.empStatus = :status");
q.setParameter("status", status);
List emps= q.list();

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