简体   繁体   English

Hibernate Mysql映射关系

[英]Hibernate Mysql mapping relationship

I'm not sure if this is possible to do in Spring 3 framework using hibernate and mysql but I would appreciate any help. 我不确定在Spring 3框架中使用hibernate和mysql是否可以做到这一点,但我将不胜感激。 I have two classes - Employee and Examiner. 我有两个班级-员工和考官。 The examiner is an employee and an employee is also an examiner. 审查员是雇员,雇员也是审查员。 At the same time each Examiner can examine one or more employees and an employee can only have one Examiner. 同时,每个考官可以检查一个或多个员工和雇员只能有一个考官。

Basically what I want to know is if it is possible to show the inheritance between the Employee and Examiner, and at the same time map a unidirectional one to many from Examiner to Employee? 基本上我想知道的是,是否有可能显示Employee和Examiner之间的继承关系,同时将一个单向的映射关系映射到Examiner和Employee?

What I have so far - the Examiner table with the inheritance constraint: 到目前为止,我所拥有的-具有继承约束的Examiner表:

CREATE TABLE `examiner` (
`employee_id` varchar(255) NOT NULL,
`employee_name` varchar(255) NOT NULL,
 PRIMARY KEY (`enployee_id`),
 FOREIGN KEY (`employee_id`) REFERENCES `employee` (`employee_id`)): 

The employee table: 员工表:

 CREATE TABLE `employee` (
`employee_id` varchar(255) NOT NULL,
`employee_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`employee_id`)):

I was thinking of a join table for showing the one to many behaviour but getting a compsite key for the table is not possible as I have a primarykeyjoin column. 我当时在考虑联接表用于显示一对多行为,但是由于我有一个primarykeyjoin列,因此无法获得该表的compsite键。

I would appreciate any help in pulling this together as I have been stumped for days. 由于将我困扰了好几天,我很乐意将其组合在一起。

Java doesn't allow multiple inheritance, so unless you are using an interface I am not sure how you plan to make the two described classes instances of each other. Java不允许多重继承,因此除非您使用接口,否则我不确定如何计划使两个所描述的类实例互为实例。

You could just make a class called EmployeeExaminer, and make it reference itself. 您可以只创建一个名为EmployeeExaminer的类,并使其自身引用。 With annotations it might look something like: 带有注释的内容可能类似于:

@Entity
public class EmployeeExaminer {
  @ManyToOne
  private EmployeeExaminer getExaminer() {/*...*/}

  @OneToMany
  private List<EmployeeExaminer> getEmployees() { /*...*/}
}

Documentation on the annotations can be found here . 有关注释的文档可在此处找到。

Thanks @CodeChimp for your replies. 感谢@CodeChimp的回复。 I ended up following the first suggestion, and created a Self Reference class with the @onetomany and @manytoone annotations. 我最终遵循了第一个建议,并创建了带有@onetomany和@manytoone批注的Self Reference类。 The helper class for it works. 它的帮助程序类起作用。 I just have some problems implementing a controller and jsp page for adding the parent/child. 我只是在实现添加父项/子项的控制器和jsp页面时遇到一些问题。 But I will make a new question for that. 但是我将对此提出一个新问题。 Thanks again. 再次感谢。

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

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