了解注释和JPA(休眠) [英] Understanding annotations and JPA(hibernate)

查看:130
本文介绍了了解注释和JPA(休眠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个结构。我想以这种方式链接这三个实体:公司应该包含ID,公司名称和部门名单,每个部门都有一个工人名单,部门名称和部门名称。

  +公司
-int companyId
- 公司名称
-Set< Department> listOfDepartments = new HashSet< Department>();

+部门
-int departmentId
- 字符串departmentName
- Set< Worker> listOfWorkers = new HashSet< Worker>();

+ Worker
-int workerId
- String workerName

我尝试与一对多和多对一建立联系,但并不成功。



Сompany

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

@ Id @GeneratedValue(strategy = GenerationType.AUTO)
private int idCompany;
private String companyName;

@OneToMany(mappedBy =company)
private Set< Department> listOfDepartments = new HashSet< Department>();

部门

  @Entity 
public class Department {

@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int idDepartment;
private String departmentName;

@ManyToOne
@JoinColumn(name =idCompany)
私人公司公司;

@OneToMany(mappedBy =department)
private Set< Worker> listOfWorkers = new HashSet< Worker>();

工作人员

  @Entity 
public class Worker {

@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int idWorker;
private String workerName;

@ManyToOne
@JoinColumn(name =idDepartment)



<

  Session session = sessionFactory.openSession(); 
session.beginTransaction();
Worker worker1 =新工人(WorkerName1);
Worker worker2 =新工人(WorkerName2);
Worker worker3 =新工人(WorkerName3);
工人worker4 =新工人(WorkerName4);
部门部门=新部门(技术);
department.getListOfWorkers()。add(worker1);
department.getListOfWorkers()。add(worker2);
department.getListOfWorkers()。add(worker3);
department.getListOfWorkers()。add(worker4);
company = new Company(MyCompanyName);
company.getListOfDepartments()。add(department);
session.save(company);
session.getTransaction()。commit();
session.close();

它填充公司,但不填充其他表,也不创建任何联接(地图)
错误:

  Hibernate:alter table Department放置外键FK_l7sg67atqhnn0soqynpvxrtpk 
Hibernate:alter table工作者放置外键FK_s53hyohtyjy93srd2wkksairk
Hibernate:如果存在drop table公司
Hibernate:如果存在drop table Department b $ b Hibernate:drop table如果存在Worker
Hibernate:create table公司(idCompany整数不为null auto_increment,companyName Hibernate:创建表部门(idDepartment整型不为空auto_increment,departmentName varchar(255),idCompany整型,主键(idDepartment))
Hibernate:create table varchar(255),主键(idCompany))
Hibernate: Worker(idWorker整数不为空auto_increment,workerName varchar(255),idDepartment整数,主键(idWorker))
Hibernate:alter table部门添加索引FK_l7sg67atqhnn0soqynpvxrtpk(idCompany),add约束FK_l7sg67atqhnn0soqynpvxrtpk外键(idCompany)引用公司(idCompany)
Hibernate:alter table工作人员添加索引FK_s53hyohtyjy93srd2wkksairk(idDepartment),添加约束FK_s53hyohtyjy93srd2wkksairk外键(idDepartment)引用部门(idDepartment)
ноя11,2013 3 :10:3​​1 AM org.hibernate.tool.hbm2ddl.SchemaExport执行
INFO:HHH000230:模式导出完成
Hibernate:插入公司(公司名称)值(?)


他们有一个所有者方面和一个反面。 JPA只关心所有者方面来决定实体之间存在哪种关联。所有者方是不具有 mappedBy 属性的那一方。



你的代码将公司的职员和工作人员添加到部门,但它只是初始化反面。您忘记初始化所有者端:

  worker1.setDepartment(department); 
worker2.setDepartment(department);
...
department.setCompany(company);


There is a structure. I want to link the three entities in this way: the Company should contain id, name of company and the list of Departments, each Department has a list of Workers, id and name of department. Each worker has name, id.

+Company
-int companyId
-String companyName
-Set<Department> listOfDepartments = new HashSet<Department>();

+Department
-int departmentId
-String departmentName
-Set<Worker> listOfWorkers = new HashSet<Worker>();

+Worker
-int workerId
-String workerName

I tried to make a connection with the one-to-many and many-to-one, but is not successful.

Сompany

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

@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int idCompany;
private String companyName;

@OneToMany(mappedBy = "company")
private Set<Department> listOfDepartments = new HashSet<Department>();

Department

@Entity
public class Department {

@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int idDepartment;
private String departmentName;

@ManyToOne
@JoinColumn(name="idCompany")
private Company company;

@OneToMany(mappedBy = "department")
private Set<Worker> listOfWorkers = new HashSet<Worker>();

Worker

@Entity
public class Worker {

@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int idWorker;
private String workerName;

@ManyToOne
@JoinColumn(name="idDepartment")

I start with:

Session session = sessionFactory.openSession();
        session.beginTransaction();
        Worker worker1 = new Worker("WorkerName1");
        Worker worker2 = new Worker("WorkerName2");
        Worker worker3 = new Worker("WorkerName3");
        Worker worker4 = new Worker("WorkerName4");
        Department department = new Department("Technical");
        department.getListOfWorkers().add(worker1);
        department.getListOfWorkers().add(worker2);
        department.getListOfWorkers().add(worker3);
        department.getListOfWorkers().add(worker4);
        company = new Company("MyCompanyName");
        company.getListOfDepartments().add(department);
        session.save(company);
        session.getTransaction().commit();
        session.close();

It fills company, but not fills other tables and also it's not creating any joins(maps) Error:

Hibernate: alter table Department drop foreign key FK_l7sg67atqhnn0soqynpvxrtpk
Hibernate: alter table Worker drop foreign key FK_s53hyohtyjy93srd2wkksairk
Hibernate: drop table if exists Company
Hibernate: drop table if exists Department
Hibernate: drop table if exists Worker
Hibernate: create table Company (idCompany integer not null auto_increment, companyName varchar(255), primary key (idCompany))
Hibernate: create table Department (idDepartment integer not null auto_increment, departmentName varchar(255), idCompany integer, primary key (idDepartment))
Hibernate: create table Worker (idWorker integer not null auto_increment, workerName varchar(255), idDepartment integer, primary key (idWorker))
Hibernate: alter table Department add index FK_l7sg67atqhnn0soqynpvxrtpk (idCompany), add constraint FK_l7sg67atqhnn0soqynpvxrtpk foreign key (idCompany) references Company (idCompany)
Hibernate: alter table Worker add index FK_s53hyohtyjy93srd2wkksairk (idDepartment), add constraint FK_s53hyohtyjy93srd2wkksairk foreign key (idDepartment) references Department (idDepartment)
ноя 11, 2013 3:10:31 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: insert into Company (companyName) values (?)

解决方案

In addition to the cascade mentioned in Glenn Lane's answer, you also need to understand how bidirectional associations work.

They have an owner side, and an inverse side. JPA only cares about the owner side to decide which association exists between entities. The owner side is the one which doesn't have the mappedBy attribute.

Your code add depertments to the company, and workers to the departments, but it only initializes the inverse sides. You forgot to initialize the owner side:

worker1.setDepartment(department);
worker2.setDepartment(department);
...
department.setCompany(company);

这篇关于了解注释和JPA(休眠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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