简体   繁体   English

Hibernate mapBy在聚合关联中可以使用吗?

[英]Hibernate mappedBy in aggregate associations, possible to use?

I am trying to figure out mappedBy ... If I have a Person { Address address; 我正在尝试找出mappingBy ...如果我有一个Person { } then I can add a Person person in Address with mappedBy "address"... However, if Person has class Person { PersonDetails personDetails } and PersonDetails { Address address } then is there a way to do a mappedby back to the Person from Address ? }然后我可以在地址中使用MapdBy“ address”添加一个Person人。但是,如果Person具有类Person {PersonDetails personDetails}和PersonDetails {Address address}类,那么有一种方法可以通过Address返回一个映射到Person的人? How can I achieve this? 我该如何实现?

This setup is what is commonly used and works fine: 此设置是常用的并且可以正常运行:

    class Person {
            @OneToOne
            Address address;
    }


    class Address {
            @OneToOne(mappedBy="address")
            Person person;
    }

The above will allow for person to automatically be set on Address when when new Person() is called. 上面的代码允许在调用new Person()时在“地址”上自动设置人员。

However, can I do something along these lines, if so how? 但是,如果可以的话,我可以按照这些方式做些什么吗? The below example doesn't work: 以下示例不起作用:

    class Person {
            @OneToOne
            PersonDetails personDetails;
    }

    class PersonDetails {
            @OneToOne
            Address address;
    }

    class Address {
            @OneToOne(mappedBy="IS THERE A WAY TO REFER TO THE PERSON THROUGH MY OWNER, PERSONDETAILS??")
            Person person;
    }

Do I have to manually set the person on Address when creating a new Person(). 创建新的Person()时,是否必须在“地址”上手动设置此人。

Is it possible to use @Column(table, or @JoinColumn in combination somehow? 是否可以以某种方式组合使用@Column(table或@JoinColumn?

mappedBy is used to mark the other side of bidirectional relationship between two entities. mappedBy用于标记两个实体之间的双向关系的另一端。 So, it is not intended for the purpose you describe. 因此,它并非旨在用于您描述的目的。

If you just want to access Person from Address , you can just do this 如果您只想从Address访问Person ,则可以执行此操作

class Address {
    @OneToOne(mappedBy = "address")
    PersonDetails personDetails;
    ...
    public Person getPerson() {
        return personDetails == null ? null : personDetails.getPerson();
    }
}

无法使用Hibernate或JPA。

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

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