问题描述
我对双向映射有几个问题.
I have several questions on bidirectional mapping.
我有以下实体:
- 员工(1)-(1)停车位
- 员工(*)-(1)部门
-
员工(*)-(1)项目
- Employee(1) - (1) Parking_Lot
- Employee(*) - (1) Department
Employee(*) - (1) Project
上述关系的源实体和目标实体是什么?
What is the source and target entity for the above relationship?
请帮助.
我有下表:项目- Proj_Client -客户端(多对多关系),并保留项目表,但客户端未保留.怎么了
I have the following table:Project - Proj_Client - Client (Many to Many relationship) and persist the project table but the client is not get persist. What wrong with it?
谢谢.
推荐答案
@Entity
@Table(name="empoyee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@ManyToOne
@JoinColumn(name="department_id", referencedColumnName="id")
private Department department;
@ManyToOne
@JoinColumn(name="project_id", referencedColumnName="id")
private Project projects;
@OneToOne(mappedBy="employee")
private ParkingLot parkingLot;
//Other properties,constructors, getters and setters and so on
}
@Entity
@Table(name="department")
public class Department implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@OneToMany(mappedBy="department")
private List<Employee> employees;
//Other properties,constructors, getters and setters and so on}
@Entity
@Table(name="parking_lot")
public class ParkingLot implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@OneToOne
@JoinColumn(name="employee_id",referencedColumnName="id")
private Employee employee;
//Other properties,constructors, getters and setters and so on}
@Entity
@Table(name="project")
public class Project implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@OneToMany(mappedBy="project")
private List<Employee> employees;
//Other properties,constructors, getters and setters and so on
}
如果关系是单向的,则实际上没有拥有的一面,也没有任何mappedBy批注.如果关系是双向的,则有一个带有mappingBy注释的面-另一面是拥有面.拥有一方是拥有关系的一方.因此,该术语不应像ParkingLot拥有其雇员或Employee拥有其ParkingLot那样来应用,而应像ParkingLot和Employee之间的关系由Employee所拥有(或ParkingLot见下文)一样.对于ManyToOne而言,没有mappingBy,因此始终是在拥有关系的OneToMany注释下指定的实体(这很有意义,因为例如project表不能为其所有雇员提供外键)因此,对于您的示例中的两个ManyToOne/OneToMany映射,我们无法选择哪一方拥有该关系.在Employee和ParkingLot之间,我们有一个选择,我选择了ParkingLot.当有选择时,这有什么关系?好吧,主要的区别在于,mappedBy具有多孔级联.请注意,哪个表具有外键或关系是否在其自己的表中都无关紧要-JPA支持所有情况(@InverseJoinColumn等).
If the relationship is unidirectional there really isn't an owning side, and there isn't any mappedBy annotations.If the relationship is bidirectional there is a side with the mappedBy annotation - the other side is the owning side.The owning side is the side that owns the relationship. So the term is not ment to be applied like a ParkingLot owns its Employee or an Employee owns its ParkingLot, but rather like the relationship between ParkingLot and Employee is owned by the Employee (or ParkingLot see below).For ManyToOne there is no mappedBy, so it is always the entity specified under the OneToMany annotation that owns the relationship (makes sense, since the for example the project table can't have foreign keys to all its employees)So for the two ManyToOne/OneToMany mappings in your example we don't have a choice in which side owns the relationship. Between Employee and ParkingLot we have a choice, I choosed ParkingLot.When given a choice, what does it matter? Well, the main difference is that the mappedBy have the porperty cascade. Please note that it doesn't matter which table have the foreign key, or if the relationship is in its own table - JPA supports all cases (@InverseJoinColumn etc).
对于双向映射,没有明确的映射目标和源,这取决于您从哪个方向进行映射.该术语更适用于单向映射,并且源端当然是具有映射的实体(这可能是目标实体的知识)
For bidirectional mappings there isn't a clear target and source for the mapping, it depends on which way you at the mapping from. The term is more applicable to unidirectional mappings, and there the source side is of course the entity with the mapping (that is possible knowledge of the target entity)
4)不适用(除非您使ParkingLot和Employee之间的关系是单向的).5)关系的所有者始终是在一个实体上"6)反面
4) Not applicable (unless you make the relationship between ParkingLot and Employee unidirectional).5) The owner of the relationship is always "on the One entity"6) inverse side
最后的注释:拥有方"令人困惑,例如,我们可以设计为部门拥有员工,而如果我们删除部门,则其所有员工也将被删除.这可以通过将@OneToMany(mappedBy="department")
更改为@OneToMany(mappedBy="department", cascade= CascadeType.REMOVE)
来完成,然后说部门实体拥有其雇员实体"确实很有意义,但是该关系仍然归雇员实体所有.
Final note:"owning side" is confusing, for example we could design so that a Department owns its Employees and if we delete a Department all its employees would also be deleted. This would be done by changing @OneToMany(mappedBy="department")
into @OneToMany(mappedBy="department", cascade= CascadeType.REMOVE)
then it would really make sense to say "the Department entity owns its Employee entities" but the relationship would still be owned by the Employee entity.
这篇关于JPA关系映射概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!