0

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
}
5
  • 1
    Its difficult to tell the exact question you are asking. How to do the mapping? Is the table design correct? etc.... Commented Jan 5, 2011 at 15:03
  • @hvgotcodes Thanks for the reply sir! This has been the design. For some reason, they want to have a master table of all the employee status. This is so that they could add another entry into it like 'RESIGNED','TERMINATED'. It would be easier to add other status in the future. Commented Jan 5, 2011 at 15:10
  • so is the question how to do the mapping? Commented Jan 5, 2011 at 15:11
  • @hvgotcodes Yes sir. I really dont have an idea on how to return a List of employee with Employee Status. In plain JDBC, I will just create a native SQL query that join the two tables then instantiate the status class per each record. I am thinking that hibernate can automate this. Commented Jan 5, 2011 at 15:15
  • then @sblundy has a good answer. Commented Jan 5, 2011 at 15:17

4 Answers 4

4

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
}
Sign up to request clarification or add additional context in comments.

10 Comments

@Renan Thanks sir! I will explore this code. Will post back after some reading and testing.
@Renan: If there are a lot of employees but only a few status (and the poster lists only 3), this could cause memory and performance problems even if it's lazy loaded. So I wouldn't have empList. Otherwise, your annotations make sense.
@sblundy: yes I know, it'll be slow with a huge collection of employee (like 3 status for 1k+ employee). I was just answering the question.
@All I was just wondering about the EmployeeStatus class. Do I need to configure the relationship at both sides? I mean does the EmployeeStatus class needs to have a List of Employees also? Is it a requirement in Hibernate that the relationship should be configured at both sides?
@Renan Is the purpose of configuring the association at both sides of the association is that so that I could query for a list of employees at the two entities? Like this at the EmployeeStatus: 'select empList from EmployeeStatus a where a.statusDesc = :status' and at the Employee Side : 'from Employee where empStatus.statusDesc = :status'
|
2

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.

Comments

1

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.

1 Comment

Wouldn't @ManyToOne make more sense? Since many employees can have the same status.
0

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

2 Comments

Thanks for your accomodating newbie like me sir. But some question though. Do I have to annotate the EmployeeStatus class also? Sorry if I cannot understand it correctly but would it be possible for you to show some snippets of the classes involved with annotation? I would really appreciate it. Thanks.
@Mark Estrada: You do have to annotate the status class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.