hibernate join two entity in different schema or session

一个人想着一个人 提交于 2019-12-23 19:36:21

问题


I have multiple schemas having their each table.

ANIMAL.pet

@Entity
@Table(schema = "ANIMAL", name = "pet")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Pet {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idx")
    private Integer idx;

    @Column(name = "name")
    private String name;
}

HUMAN.person

@Entity
@Table(schema = "HUMAN", name = "person")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idx")
    private Integer idx;

    @Column(name = "name")
    private String name;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="pet_idx")
    private Pet pet;
}

I used criteria 5.3 and make each config, which has their own DataSource, SessionFactory and TransactionManager.

Each Repository use their TransactionManager and Session.

Such as

@Repository
@Transactional(value = "AnimalTransactionManager", readOnly = true)
public class AnimalRepository implements BaseRepository {

    @Autowired
    @Qualifier("AnimalSessionFactory")
    private SessionFactory sessionFactory;

    public Session session () {
        return sessionFactory.openSession();
    }

    public CriteriaBuilder createCriteria() {
        return session().getCriteriaBuilder();
    }
}

Those are what I tried.

  1. @JoinTable
  2. @SecondaryTable

the jointable and secondary table annotation can just join their each Id values, so it brings wrong values.

  1. Join < Pet, Person >

so I tried use Join when I use my criteriaquery. Since I use the Session about ANIMAL which is schema of pet, it calls ANIMAL.person and brings error.

I figure it out there is no answer about join two entity in different schemas and having different session.

Those won't bring the result what I want and expected.

So Is there anyone who have an idea about joincolumn that can join two entity in different schema or session?


回答1:


I figured this issue out.

Put the properties names "catalog" with the same value of schema.

@Table(schema = "HUMAN", catalog = "HUMAN", name = "person")

I do not know yet exact the way how it works, but it works well for me. And if i get any idea about it, i will edit this answer.



来源:https://stackoverflow.com/questions/50958784/hibernate-join-two-entity-in-different-schema-or-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!