JPA One Entity with two table via SecundaryTable












0















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









share|improve this question























  • A secondary table will have a PK that is the same as the PK of the owning table. You don't have some extra column in the owning table. Any basic JPA docs would tell you this datanucleus.org:15080/products/accessplatform_5_2/jpa/…

    – Billy Frost
    Nov 14 '18 at 11:22













  • Yea nice guide. Google did put out to much irrelevant and redundand information, so i missed this tiny pice of information. Thanks for that. So if SecundaryTable is not the right thing. Do you have a hint on how to get this setup to work?

    – Iceac Sarutobi
    Nov 14 '18 at 11:38
















0















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









share|improve this question























  • A secondary table will have a PK that is the same as the PK of the owning table. You don't have some extra column in the owning table. Any basic JPA docs would tell you this datanucleus.org:15080/products/accessplatform_5_2/jpa/…

    – Billy Frost
    Nov 14 '18 at 11:22













  • Yea nice guide. Google did put out to much irrelevant and redundand information, so i missed this tiny pice of information. Thanks for that. So if SecundaryTable is not the right thing. Do you have a hint on how to get this setup to work?

    – Iceac Sarutobi
    Nov 14 '18 at 11:38














0












0








0








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









share|improve this question














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






hibernate jpa criteria jpa-annotations






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 11:19









Iceac SarutobiIceac Sarutobi

12




12













  • A secondary table will have a PK that is the same as the PK of the owning table. You don't have some extra column in the owning table. Any basic JPA docs would tell you this datanucleus.org:15080/products/accessplatform_5_2/jpa/…

    – Billy Frost
    Nov 14 '18 at 11:22













  • Yea nice guide. Google did put out to much irrelevant and redundand information, so i missed this tiny pice of information. Thanks for that. So if SecundaryTable is not the right thing. Do you have a hint on how to get this setup to work?

    – Iceac Sarutobi
    Nov 14 '18 at 11:38



















  • A secondary table will have a PK that is the same as the PK of the owning table. You don't have some extra column in the owning table. Any basic JPA docs would tell you this datanucleus.org:15080/products/accessplatform_5_2/jpa/…

    – Billy Frost
    Nov 14 '18 at 11:22













  • Yea nice guide. Google did put out to much irrelevant and redundand information, so i missed this tiny pice of information. Thanks for that. So if SecundaryTable is not the right thing. Do you have a hint on how to get this setup to work?

    – Iceac Sarutobi
    Nov 14 '18 at 11:38

















A secondary table will have a PK that is the same as the PK of the owning table. You don't have some extra column in the owning table. Any basic JPA docs would tell you this datanucleus.org:15080/products/accessplatform_5_2/jpa/…

– Billy Frost
Nov 14 '18 at 11:22







A secondary table will have a PK that is the same as the PK of the owning table. You don't have some extra column in the owning table. Any basic JPA docs would tell you this datanucleus.org:15080/products/accessplatform_5_2/jpa/…

– Billy Frost
Nov 14 '18 at 11:22















Yea nice guide. Google did put out to much irrelevant and redundand information, so i missed this tiny pice of information. Thanks for that. So if SecundaryTable is not the right thing. Do you have a hint on how to get this setup to work?

– Iceac Sarutobi
Nov 14 '18 at 11:38





Yea nice guide. Google did put out to much irrelevant and redundand information, so i missed this tiny pice of information. Thanks for that. So if SecundaryTable is not the right thing. Do you have a hint on how to get this setup to work?

– Iceac Sarutobi
Nov 14 '18 at 11:38












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299008%2fjpa-one-entity-with-two-table-via-secundarytable%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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.






    share|improve this answer




























      0














      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.






      share|improve this answer


























        0












        0








        0







        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.






        share|improve this 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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 8:06









        Iceac SarutobiIceac Sarutobi

        12




        12






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299008%2fjpa-one-entity-with-two-table-via-secundarytable%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine