log
log

Reputation: 51

hibernate join two entity in different schema or session

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?

Upvotes: 4

Views: 4497

Answers (1)

log
log

Reputation: 51

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.

Upvotes: 1

Related Questions