简体   繁体   English

带有两个表的休眠映射

[英]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. 我正在尝试学习Hibernate,并且可以使用Single Class和Single Table创建一些简单的CRUD操作。 I am just reading the Hibernate Doc and some online tutorial. 我正在阅读Hibernate Doc和一些在线教程。

But I have a problem on how to define this relationship with two tables involved. 但是我对如何使用两个涉及的表定义这种关系有疑问。 I basically have an Employee table with this structure. 我基本上有一个具有这种结构的Employee表。

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_ID字段引用另一个表。 STATUS_DESC can either be 'PERMANENT', 'CONTRACTUAL', 'ON-DEMAND' STATUS_DESC可以是“ 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. 现在,我的目标是返回状态为Employee的对象列表,但是我不知道该怎么做。

@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? ManyToOne?

Employee.java 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... 不要忘记更改“ referencedColumnName”值...

EmployeeStatus.java 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: 在这种情况下,您可以像这样在Employee类上添加注释:

@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. 您可以执行自己的操作,但是我建议,如果状态只能是三个值之一,则用三个值创建一个Enum。 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. 这样做的缺点是您需要创建一个休眠的自定义类型(代码在Wiki上)以支持持久枚举。

A simpler answer is to not use a secondary table, and just save the status as a String on the domain object. 一个简单的答案是不使用辅助表,而只是将状态另存为String到域对象上。 You can put business logic on your model to ensure the String is in the list of acceptable values. 您可以在模型上放置业务逻辑,以确保String在可接受值的列表中。

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. 您可以使用HQL查询实体。 Like so 像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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