首页 > 解决方案 > SecondaryTable 可以是 OneToMany 关系吗?

问题描述

目前我有以下数据库结构:

+----------------+             +-----------------------+
|Person          |             |LegalGuardian_Person   |
|----------------+             |-----------------------+
|personId, int   |             |legGuard_person_id, int|
|name, varchar   |             |represented_person, int|
|surname, varchar|             |representative, int    |
+----------------+             +-----------------------+

代表人和代表都引用了不同的人。现在我正在尝试使用@SecondaryTable 或@OneToOne 来“加入”这些实体,我可以通过 personId 找出谁是法定监护人。

例如,Maria 和 Michael 是 John 的合法监护人:

personId: 1
name: Maria
surname: Doe
legalGuardian: []

personId: 2
name: Michael
surname: Doe
legalGuardian: []

personId: 3
name: John
surname: Doe
legalGuardian: [Maria, Michael]

到目前为止,我这样尝试过,但我总是得到一个空列表:

@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "LegalGuardian_Person",
joinColumns = @JoinColumn(name = "represented_person"),
inverseJoinColumns = @JoinColumn(name = "representative"))
private List<PersonRole> representatives;

当我尝试它时,@OneToMany(mappedBy="represented_person")我显然会得到一个永无止境的结果。所以我有 Person 实体并尝试使用当前的 personId 找到represented_person并让代表回来。

有办法吗?

标签: javahibernatejpahibernate-mapping

解决方案


实现您所需的一种方法是拥有OneToMany relationship between Person and LegalGuardian_Person,并且避免映射表,我们不妨拥有ManyToOne relationship between LegalGuardian_Person and Person

@Entity
class LegalGuardian_Person {
    @Id@GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @OneToOne
    private Person legGuard_person_id;
    @OneToOne
    private Person representative;

    @ManyToOne
    private Person represented_person;

    //getters and setters
}
@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int personId;
    private String name;
    private String surname;

    @OneToMany(mappedBy = "represented_person")
    Set<LegalGuardian_Person> legalGuardian_personSet;

    //getters and setters

    public static void main(String[] args) {
        Person Maria = new Person();
        Maria.setName("Maria");
        Maria.setSurname("Doe");

        Person Michael = new Person();
        Michael.setName("Michael");
        Michael.setSurname("Doe");

        Person John = new Person();
        John.setName("John");
        John.setSurname("Doe");

        {
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            Session session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();

            session.save(Maria);
            session.save(Michael);
            session.save(John);

            LegalGuardian_Person John_LegalGuardian_Person = new LegalGuardian_Person();
            John_LegalGuardian_Person.setRepresented_person(John);
            John_LegalGuardian_Person.setLegGuard_person_id(Maria);

            session.save(John_LegalGuardian_Person);
            Set<LegalGuardian_Person> legalGuardian_personSet = new HashSet<>();
            legalGuardian_personSet.add(John_LegalGuardian_Person);

            John_LegalGuardian_Person = new LegalGuardian_Person();
            John_LegalGuardian_Person.setRepresented_person(John);
            John_LegalGuardian_Person.setLegGuard_person_id(Michael);

            session.save(John_LegalGuardian_Person);
            legalGuardian_personSet.add(John_LegalGuardian_Person);

            John.setLegalGuardian_personSet(legalGuardian_personSet);

            transaction.commit();
            session.close();
        }

        {
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            Session session = sessionFactory.openSession();

            Person person = session.get(Person.class, 3);
            person.legalGuardian_personSet.forEach(legalGuardian_person -> System.out.println(legalGuardian_person.getLegGuard_person_id().getName()));

            session.close();
        }
    }
}

hibernate 和 Output 创建的表

Hibernate: create table LegalGuardian_Person (id integer not null auto_increment, legGuard_person_id_personId integer, representative_personId integer, represented_person_personId integer, primary key (id)) engine=MyISAM
Hibernate: create table Person (personId integer not null auto_increment, name varchar(255), surname varchar(255), primary key (personId)) engine=MyISAM
Hibernate: alter table LegalGuardian_Person add constraint FKoe3yay7hrvwic3od1f7ibdm0k foreign key (legGuard_person_id_personId) references Person (personId)
Hibernate: alter table LegalGuardian_Person add constraint FKc7p7lx5e837i0blk0q1ukydnk foreign key (representative_personId) references Person (personId)
Hibernate: alter table LegalGuardian_Person add constraint FKpkmx0g3ix2myf9py309ngwfaa foreign key (represented_person_personId) references Person (personId)
Hibernate: insert into Person (name, surname) values (?, ?)
Hibernate: insert into Person (name, surname) values (?, ?)
Hibernate: insert into Person (name, surname) values (?, ?)
Hibernate: insert into LegalGuardian_Person (legGuard_person_id_personId, representative_personId, represented_person_personId) values (?, ?, ?)
Hibernate: insert into LegalGuardian_Person (legGuard_person_id_personId, representative_personId, represented_person_personId) values (?, ?, ?)
Hibernate: alter table LegalGuardian_Person add constraint FKoe3yay7hrvwic3od1f7ibdm0k foreign key (legGuard_person_id_personId) references Person (personId)
Hibernate: alter table LegalGuardian_Person add constraint FKc7p7lx5e837i0blk0q1ukydnk foreign key (representative_personId) references Person (personId)
Hibernate: alter table LegalGuardian_Person add constraint FKpkmx0g3ix2myf9py309ngwfaa foreign key (represented_person_personId) references Person (personId)
Hibernate: select person0_.personId as personId1_1_0_, person0_.name as name2_1_0_, person0_.surname as surname3_1_0_ from Person person0_ where person0_.personId=?
Hibernate: select legalguard0_.represented_person_personId as represen4_0_0_, legalguard0_.id as id1_0_0_, legalguard0_.id as id1_0_1_, legalguard0_.legGuard_person_id_personId as legGuard2_0_1_, legalguard0_.representative_personId as represen3_0_1_, legalguard0_.represented_person_personId as represen4_0_1_, person1_.personId as personId1_1_2_, person1_.name as name2_1_2_, person1_.surname as surname3_1_2_, person2_.personId as personId1_1_3_, person2_.name as name2_1_3_, person2_.surname as surname3_1_3_ from LegalGuardian_Person legalguard0_ left outer join Person person1_ on legalguard0_.legGuard_person_id_personId=person1_.personId left outer join Person person2_ on legalguard0_.representative_personId=person2_.personId where legalguard0_.represented_person_personId=?
Maria
Michael

推荐阅读