Hibernate在类中存储字段 [英] Hibernate Storing Fields inside Classes

查看:105
本文介绍了Hibernate在类中存储字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班,有两个单独的表,员工和公司。我想保留公司班级内的员工名单。这很容易,但我不知道如何在数据库端表示这个列表。



Class'Company':

  @Entity 
@Table(name =company)
public class Company {

@Id
@ GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =id)
private Integer id;

@Column(name =name)
私有字符串名称;

私人清单<员工> EmployeeList的;

$ b

'Employee'类:

  @Entity 
@Table(name =employee)
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =id)
private Integer id;

@Column(name =name)
私有字符串名称;

@Column(name =age)
私人整数年龄;

如果没有hibernate,我会选择使用两列'employeeId'和'companyId'创建另一个表的设计,并尝试让'employeeIds'与'companyId'匹配的所有员工。我不知道是否有可能在hibernate中做同样的事情。如果是这样如何?如果不是,你的解决方案是什么?

解决方案

简单的答案是,您可以配置Hibernate生成一个三十张表来存储如你所愿。
您可以使用 @JoinTable 实现此目的注解。您的示例将如下所示:

  @Entity 
@Table(name =company)
public class Company {
...
@ OneToMany //或@ManyToMany
@JoinTable(name =table_name,
joinColumns = @JoinColumn(name =employeeId ,referenceColumnName =id),
inverseJoinColumns = @JoinColumn(name =companyId,referencedColumnName =id))
private List< Employee> EmployeeList的;

现在Hibernate会创建一个名为 table_name 的表格,希望以及对表 employee company 的对应外键约束。
您可以使用 @FrontKey 注释。

I have two classes with two individual tables, "Employee" and "Company". I would like to keep a list of employee inside Company class. It is easy but I do not know how to represent this list in the database side.

Class 'Company':

@Entity
@Table(name = "company")
public class Company {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;

    private List<Employee> employeeList;

}

Class 'Employee':

@Entity
@Table(name = "employee")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private Integer age;

Without hibernate, I would choose the design creating another table with two columns 'employeeId' and 'companyId' and try to get all employees with the 'employeeIds' matching a 'companyId'. I do not know if it is possible to do same in hibernate or not. If so how? If not, what is your solution?

解决方案

The short answer is that you can configure Hibernate to generate a thirty table to store the association as you wish. You can achieve this using @JoinTable annotation. Following your example it will be something like:

@Entity
@Table(name = "company")
public class Company {
...
@OneToMany// or @ManyToMany
@JoinTable(name = "table_name",
    joinColumns = @JoinColumn(name="employeeId", referencedColumnName="id"),
    inverseJoinColumns = @JoinColumn(name="companyId",  referencedColumnName="id"))
private List<Employee> employeeList;

Now Hibernate will create a table named table_name with the two columns that you want and the correspondoning foreign key constraints to the tables employee and company. You can specify the freign keys names using the @ForeignKey annotation too.

这篇关于Hibernate在类中存储字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆