JPA One Entity with two table via SecundaryTable

463 views Asked by At

I am unable to get the following simplified setup to work. I just want to get the two values of the tables in ONE class with the criteria API. I do not want to use two classes with @OneToOne or @OneToMany etc.

Is this possible? Is such a thing not intended with JPA?

First Table:

test.table_1(
  id integer,
  value_1 text,
  table_2_id integer
)

Second Table:

test.table_2(
  id integer,
  value_2 text
)

Entity Class:

@Entity
@Table(schema = "test", name = "table_1")
@SecondaryTable(schema = "test", name = "table_2", pkJoinColumns = {
    @PrimaryKeyJoinColumn(name = "id", referencedColumnName = "table_2_id")
})
public class JointEntity {
  @Id
  private Integer id;
  @Column(name = "value_1")
  private String value1;
  @Column(name = "value_2", table = "table_2")
  private String value2;

  // getter and setter
}

Exception thrown on execution:

Exception in thread "main" org.hibernate.cfg.RecoverableException: Unable to find column with logical name: table_2_id in org.hibernate.mapping.Table(test.table_1) and its related supertables and secondary tables
    at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:831)
    at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:608)
    at org.hibernate.cfg.annotations.EntityBinder.bindJoinToPersistentClass(EntityBinder.java:794)
    at org.hibernate.cfg.annotations.EntityBinder.createPrimaryColumnsToSecondaryTable(EntityBinder.java:786)
    at org.hibernate.cfg.annotations.EntityBinder.finalSecondaryTableBinding(EntityBinder.java:714)
    at org.hibernate.cfg.SecondaryTableSecondPass.doSecondPass(SecondaryTableSecondPass.java:29)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1586)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at de.icybits.test.HibernatePerformanceTest.main(HibernatePerformanceTest.java:18)
Caused by: org.hibernate.MappingException: Unable to find column with logical name: table_2_id in org.hibernate.mapping.Table(test.table_1) and its related supertables and secondary tables
    at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:826)
    ... 14 more
1

There are 1 answers

0
Iceac Sarutobi On BEST ANSWER

Like Billy Frost mentioned in his commend @SecundaryTable is not intended to be used this way. So I did further research. Because Billy Frost comment not fully answered my question.

  1. Is it Possible to archive the above Setup (2 Table in 1 Entity)?

The Answer to this is Yes I found a workaround in an other Question Hibernate - Join only single column instead of whole entity

  1. Is such a thing not intended with JPA?

As I mentioned in 1. this is just a workaround. In general is it not intended to just Assosiate a Single foreign column to an entity. (I am not 100% sure of this, but after further research, that's what I concluded.)

Lastly I want to thank Billy Frost for his fast comment. Because of this I excluded @SecundaryTable from my Research and could find a better answer.