Hibernate(JPA)多个@OneToMany用于同一模型 [英] Hibernate (JPA) multiple @OneToMany for same model

查看:158
本文介绍了Hibernate(JPA)多个@OneToMany用于同一模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型.

@Entity
public class Student
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    protected long id;

    @?
    protected Address homeAddress;

    @?
    protected Address schoolAddress;
}

@Entity
public class Address
{
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   protected long id;

   @?
   protected List<Student> students;
}

要使关联正常工作,我需要在homeAddressschoolAddressstudents上方放置哪些JPA/休眠注释?

What JPA/hibernate annotations do I need to put above homeAddress, schoolAddress and students to make the association work?

当然,我尝试了很多事情,但没有任何效果. 例如,设置

Of course I've tried many things and nothing worked. For example, setting

    @ManyToOne
    @JoinColumn (name="student_homeAddress_id", updatable = false, insertable = false)
    protected Address homeAddress;

    @ManyToOne
    @JoinColumn (name="student_schoolAddress_id", updatable = false, insertable = false)
    protected Address schoolAddress;

    @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
    @JoinColumns({
    @JoinColumn(name = "student_homeAddress_id", referencedColumnName = "student_homeAddress_id"),
    @JoinColumn(name = "student_schoolAddress_id", referencedColumnName = "student_schoolAddress_id")})
    @IndexColumn(name="students_index")
    protected List<Student> students;

领结Unable to find column with logical name: student_homeAddress_id in org.hibernate.mapping.Table(Address) and its related supertables and secondary tables. 也尝试过使用mappedBy,但是只接受一个参数(不能执行mappedBy="student_homeAddress_id, student_schoolAddress_id".

yealds Unable to find column with logical name: student_homeAddress_id in org.hibernate.mapping.Table(Address) and its related supertables and secondary tables. Also tried using mappedBy but that takes a single argument (can't do mappedBy="student_homeAddress_id, student_schoolAddress_id".

还考虑将JoinColumns移至Student平板电脑,但是我不确定OneToMany和ManyToOne的注释应为什么样,因为我在那里有多个地址,而JoinColumns并没有多大意义.

Also looked into moving the JoinColumns to the Student tablet but I am not sure what the annotations should look like for OneToMany and ManyToOne as I have multiple Addresses there which JoinColumns doesn't make much sense.

确实有用但没有创建关联的东西有:

The thing that did work but was not creating the associations was having:

    @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
    @JoinColumn(name="address_id")
    @IndexColumn(name="students_index")
    protected List<Student> students;

使用此方法,在将模型存储在数据库中时,即使存储了两端(Student and Address模型),student_homeAddress_id和student_schoolAddress_id也始终为空.

Using this, when storing in the DB the models, the student_homeAddress_id and student_schoolAddress_id were always null even after storing both ends (the Student and Address model).

我的想法是,在Address表上将有3个额外的列:student_homeAddress_id(homeAddress在Student表中的Student的ID),student_schoolAddress_id(在schoolAddress的Student表中的Student的id) )和students_index(在students列表上从0开始的位置).这样就足够了,对吗?

My thought is that on the Address table there will be 3 extra columns: student_homeAddress_id (the id of the Student in the Student table for the homeAddress), student_schoolAddress_id (the id of the Student in the Student table for the schoolAddress) and students_index (the 0-based location on the students list). That should suffice, correct?

有什么想法吗?

非常感谢!

推荐答案

我们尝试了mael的建议,但未能成功.

We tried mael's suggestion but we couldn't make it work.

我们最终遵循了.

换句话说,我们有OneToMany关系:

In other words, we have OneToMany relations:

Student上:

protected List<AddressStudentAssociation> addresses;

Address上:

protected List<AddressStudentAssociation> students;

AddressStudentAssociation上:

@ManyToOne
@PrimaryKeyJoinColumn(name="STUDENTID", referencedColumnName="id")
private Student student;
@ManyToOne
@PrimaryKeyJoinColumn(name="ADDRESSID", referencedColumnName="id")
private Address address;

加上一个参数将一个地址与另一个地址(isHome)分开.

plus an argument to separate the one address from the other (isHome).

最后,在Student内部,我们有public Address getHomeAddress(),它遍历addresses列表并返回正确的地址.为了使它起作用,我们还必须使用注释.一般而言,它不是最佳方法,但是它可以工作,并且我们已经花了太多时间尝试使事情正常进行. :|

Finally, inside Student we have public Address getHomeAddress() which traverses the addresses list and returns the correct address. We also had to play with the annotations to make it work. Not optimal in general but it works and we already spent too much time trying to make things work. :|

这篇关于Hibernate(JPA)多个@OneToMany用于同一模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆