简体   繁体   English

JPA双向关系

[英]JPA Bidirectional Relationship

I am trying to establish Bidirectional Relationship between User and Address, 我正在尝试在用户和地址之间建立双向关系,

User 1---------->*Address 用户1 ----------> *地址

But

Address 1 -------> 1 User 地址1 -------> 1个用户

On referring the internet I got these information 在介绍互联网时,我得到了这些信息

  • For one-to-one bidirectional relationships, the owning side corresponds to the side that contains the corresponding foreign key 对于一对一的双向关系,拥有方对应于包含相应外键的方

  • The inverse side of a bidirectional relationship must refer to its 双向关系的反面必须参考其
    owning side by use of the mappedBy element of the OneToOne, 通过使用OneToOne的mappedBy元素拥有一面,
    OneToMany, or ManyToMany annotation. OneToMany或ManyToMany批注。 The mappedBy element designates the property or field in the entity that is the owner of the mapledBy元素指定实体的属性或字段,该属性或字段是
    relationship. 关系。

But If I proceed as per information then 但是如果我按照信息进行操作

I need to use Mapped By on User Entity where Set <Address> hold OnetoMany mapping At the same time I need to use Mapped By on Address Entity where user holds OnetoOne mapping 我需要在Set <Address>拥有OnetoMany映射的用户实体上使用Mapped By,同时我需要在用户拥有OnetoOne映射的地址实体上使用Mapped By。

Note : But @JoinColumn on Address entity for user works fine. 注意:但是用户的Address实体上的@JoinColumn工作正常。 If I use mappedBy on either User Entity or Address Entity I got the exception stating that 如果我在用户实体或地址实体上使用mapledBy,则会出现异常,指出

"The attribute [addresses] in entity class [class com.entity.User] has a mappedBy value of [User] which does not exist in its owning entity class [class com.entity.Address]. If the owning entity class is a @MappedSuperclass, this is invalid, and your attribute should reference the correct subclass." “实体类别[com.entity.User]类中的[addresss]属性具有[User]的mappingBy值,该值在其所属实体类别[com.entity.Address]类中不存在。如果拥有实体类是@MappedSuperclass,这是无效的,并且您的属性应引用正确的子类。”

I am confused on how to use mapped By for this type of Bi-directional relationship. 我对如何将映射的By用于这种类型的双向关系感到困惑。

Update : 更新:

<pre>
@Entity
public class User {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    protected Long id;
    ...
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY,mappedBy="user")
    private Set<Address> adresses;
} 


@Entity
public class Address {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    protected Long id;
    ...
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "userid", nullable = false)
    private User user;
}
</pre>

The problem is that @OneToMany can only have as inverse @ManyToOne , and not @OneToOne . 问题在于@OneToMany只能具有相反的@ManyToOne ,而不能具有@OneToOne @OneToOne can only have as mappedBy another @OneToOne , check the javadoc for those methods. @OneToOne只能具有与另一个@OneToOne ,请检查javadoc中的那些方法。

The mapping bellow gives you the meaning you want: one user can have many addresses, but an address can only refer to only one user using the id in the foreign key specified in @JoinColumn : 下面的映射为您提供了您想要的含义:一个用户可以有多个地址,但是一个地址只能使用@JoinColumn指定的外键中的id引用一个用户:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    protected Long id;
    ...
    @OneToMany(mappedBy="user")
    private Set<Address> adresses;
} 


@Entity
public class Address {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    protected Long id;
    ...
    @ManyToOne(optional=false)
    @JoinColumn("your_foreign_key_name")
    private User user;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM