[英]JPA @OneToMany with 1 - 1..* relationship
How to properly map @OneToMany
relationship where to create entity, on @One
side of @OneToMany
relationship, it is required to have atleast one entity from @Many
side but entity on @Many
side also requires entity on @One
side to exist? 如何正确映射
@OneToMany
关系,其中创建实体,在@One
的侧@OneToMany
关系,就需要有从ATLEAST一个实体@Many
的一面,但实体上@Many
方还需要在实体@One
侧存在吗? To put this nightmare of a sentence simply, this is the scenario I have: 简单地说这个噩梦,这就是我的情景:
This is what I want: 这就是我要的:
[ENTITY A] 1 <-----> (1..*)[ENTITY B]
At the moment I have this: 目前我有这个:
[ENTITY A] 1 <-----> (0..*)[ENTITY B]
Which is easily done like this. 这很容易做到这一点。
@OneToMany(cascade=CascadeType.ALL, mappedBy="customer")
public Set<Agreement> agreements = new HashSet<>();
and 和
@ManyToOne
@JoinColumn(name = "CUSTOMER_ID", nullable=false)
private Customer customer;
So the problem is my CUSTOMER
table has no column corresponding to AGREEMENT
table therefore I can't enforce rule of creating Customer
only when Agreement
is given. 所以问题是我的
CUSTOMER
表没有对应于AGREEMENT
表的列,因此我不能仅在给出Agreement
时强制创建Customer
规则。 At the moment I can only setup rule to create Agreement
when Customer
is given because AGREEMENT
table has column corresponding to CUSTOMER
tabel, which is easily done by nullable=false
condition. 目前,我只能在给出
Customer
时设置规则以创建Agreement
,因为AGREEMENT
表具有与CUSTOMER
tabel相对应的列,这可以通过nullable=false
条件轻松完成。
It depends very much on what type of relationship you want to enforce. 这在很大程度上取决于您想要强制执行的关系类型。 If the Agreement can exist independently from the Customer then this mean that the customer_id in agreement must be nullable.
如果协议可以独立于客户存在,那么这意味着协议中的customer_id必须可以为空。
If the Agreement can not exist independently this presumes that the customer id is not nullable in which case the Agreement can not be created in first place without the customer being created. 如果协议不能独立存在,则假定客户ID不可为空,在这种情况下,如果没有创建客户,则无法在第一时间创建协议。 This mean you have stronger association in between the customer and the corresponding Agreement.
这意味着您在客户和相应协议之间有更强的关联。
Once we define that we have a relationship that is strong we need to investigate how strong it really is and who will own whom. 一旦我们确定我们的关系是强大的,我们就需要调查它的真实性以及谁将拥有谁。 Normaly the it is the Many side that owns the relationship and the updates are happening through the many side.
Normaly是拥有这种关系的许多方面,并且更新正在通过许多方面发生。 This mean that your JoinColumn needs to be on the MANY and the mapped by needs to be on the ONE side.
这意味着您的JoinColumn需要在MANY上,并且映射的需要在ONE侧。
It is interesting case when the ownership is inverse when the ONE side actually owns the relationship in this case the foreign key on the many side can not be NULL because there is no way for the owning ONE side to know what the MANY side key is. 有趣的情况是,当ONE侧实际拥有关系时所有权是反向的,在这种情况下,多方的外键不能为NULL,因为拥有一方无法知道多方侧键是什么。
JPA doesn't provide a way to validate this, but Hibernate Validator does: JPA没有提供验证方法,但Hibernate Validator会:
@NotNull
@Size(min=1)
public Set<Agreement> agreements = new HashSet<>();
Then you have to manually test it via the Validator: 然后你必须通过Validator手动测试它:
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();
validator.validate(customer)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.