1

有一个结构。我想以这种方式链接三个实体:公司应该包含 id、公司名称和部门列表,每个部门都有一个工人列表、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

我试图与一对多和多对一建立联系,但没有成功。

公司

@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")
private Company company;

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

它填充公司,但不填充其他表,也没有创建任何连接(映射)错误:

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 (?)
4

3 回答 3

10

除了 Glenn Lane 的回答中提到的级联之外,您还需要了解双向关联是如何工作的。

它们有所有者的一面,也有相反的一面。JPA 只关心所有者方来决定实体之间存在哪种关联。所有者方是没有mappedBy属性的一方。

您的代码将部门添加到公司,将工人添加到部门,但它只初始化反面。您忘记初始化所有者方:

worker1.setDepartment(department);
worker2.setDepartment(department);
...
department.setCompany(company);
于 2013-11-10T23:14:27.507 回答
2

如果您希望 JPA 自动持久化孩子,您需要进一步装饰您的@OneToMany

@OneToMany(mappedBy = "department", cascade={CascadeType.PERSIST})

如果您想要其他操作的级联行为,例如删除、合并、刷新,您还需要将它们添加到级联列表中。

于 2013-11-10T22:34:09.167 回答
0

您可以使用另一种方法:

单向 OneToMany: http ://en.wikibooks.org/wiki/Java_Persistence/OneToMany#Unidirectional_OneToMany.2C_No_Inverse_ManyToOne.2C_No_Join_Table_.28JPA_2.0_ONLY.29

您仍然需要保留对象,然后将其添加到您的集合中。

于 2015-03-10T22:13:07.267 回答