Hibernate @SecondaryTable - Specifying foreign key of primary table












1















I have two tables:





  1. language



    CREATE TABLE language (
    id BIGSERIAL,
    name TEXT NOT NULL UNIQUE,
    PRIMARY KEY (id)
    );



  2. translation



    CREATE TABLE translation (
    id BIGSERIAL,
    language_id BIGINT REFERENCES language (id),
    translation_key TEXT NOT NULL,
    translation_value TEXT NOT NULL,
    PRIMARY KEY (id)
    );


    And I would like to get such entity, where translation table (primary table) joins language table by language_id from primary table. Problem: at the moment it joins by translation PK(id).



    @Entity
    @Table(name = "translation")
    @SecondaryTable(name = "language", pkJoinColumns = @PrimaryKeyJoinColumn(name = "id"))
    public class Translation {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(table = "language", name = "name")
    // ON translation.language_id = language.id
    private String language;

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

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

    // getters and setters
    }


    Where I should specify it in my code to do it correctly?
    SQL example: SELECT t.id, l.name, translation_key, translation_value FROM translation t INNER JOIN language l on t.language_id = l.id;












share|improve this question




















  • 1





    You do understand that the relation between entries in a primary and a secondary table is necessarily one-to-one, right?

    – crizzis
    Jan 3 at 17:10













  • Why it is one to one? I think different translations can have the same language. So, it's many-to-one, i think.

    – MZxFK
    Jan 3 at 17:15













  • Well then, you cannot join a secondary table to a primary table using anything other than its id. language is not part of the Translation entity's id

    – crizzis
    Jan 3 at 17:19













  • And what I should do to get what I want? To create separate language entity?

    – MZxFK
    Jan 3 at 17:21






  • 1





    If you want a many-to-one association, you cannot use a secondary table. Use a separate Language entity and a @ManyToOne instead

    – crizzis
    Jan 3 at 17:22


















1















I have two tables:





  1. language



    CREATE TABLE language (
    id BIGSERIAL,
    name TEXT NOT NULL UNIQUE,
    PRIMARY KEY (id)
    );



  2. translation



    CREATE TABLE translation (
    id BIGSERIAL,
    language_id BIGINT REFERENCES language (id),
    translation_key TEXT NOT NULL,
    translation_value TEXT NOT NULL,
    PRIMARY KEY (id)
    );


    And I would like to get such entity, where translation table (primary table) joins language table by language_id from primary table. Problem: at the moment it joins by translation PK(id).



    @Entity
    @Table(name = "translation")
    @SecondaryTable(name = "language", pkJoinColumns = @PrimaryKeyJoinColumn(name = "id"))
    public class Translation {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(table = "language", name = "name")
    // ON translation.language_id = language.id
    private String language;

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

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

    // getters and setters
    }


    Where I should specify it in my code to do it correctly?
    SQL example: SELECT t.id, l.name, translation_key, translation_value FROM translation t INNER JOIN language l on t.language_id = l.id;












share|improve this question




















  • 1





    You do understand that the relation between entries in a primary and a secondary table is necessarily one-to-one, right?

    – crizzis
    Jan 3 at 17:10













  • Why it is one to one? I think different translations can have the same language. So, it's many-to-one, i think.

    – MZxFK
    Jan 3 at 17:15













  • Well then, you cannot join a secondary table to a primary table using anything other than its id. language is not part of the Translation entity's id

    – crizzis
    Jan 3 at 17:19













  • And what I should do to get what I want? To create separate language entity?

    – MZxFK
    Jan 3 at 17:21






  • 1





    If you want a many-to-one association, you cannot use a secondary table. Use a separate Language entity and a @ManyToOne instead

    – crizzis
    Jan 3 at 17:22
















1












1








1








I have two tables:





  1. language



    CREATE TABLE language (
    id BIGSERIAL,
    name TEXT NOT NULL UNIQUE,
    PRIMARY KEY (id)
    );



  2. translation



    CREATE TABLE translation (
    id BIGSERIAL,
    language_id BIGINT REFERENCES language (id),
    translation_key TEXT NOT NULL,
    translation_value TEXT NOT NULL,
    PRIMARY KEY (id)
    );


    And I would like to get such entity, where translation table (primary table) joins language table by language_id from primary table. Problem: at the moment it joins by translation PK(id).



    @Entity
    @Table(name = "translation")
    @SecondaryTable(name = "language", pkJoinColumns = @PrimaryKeyJoinColumn(name = "id"))
    public class Translation {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(table = "language", name = "name")
    // ON translation.language_id = language.id
    private String language;

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

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

    // getters and setters
    }


    Where I should specify it in my code to do it correctly?
    SQL example: SELECT t.id, l.name, translation_key, translation_value FROM translation t INNER JOIN language l on t.language_id = l.id;












share|improve this question
















I have two tables:





  1. language



    CREATE TABLE language (
    id BIGSERIAL,
    name TEXT NOT NULL UNIQUE,
    PRIMARY KEY (id)
    );



  2. translation



    CREATE TABLE translation (
    id BIGSERIAL,
    language_id BIGINT REFERENCES language (id),
    translation_key TEXT NOT NULL,
    translation_value TEXT NOT NULL,
    PRIMARY KEY (id)
    );


    And I would like to get such entity, where translation table (primary table) joins language table by language_id from primary table. Problem: at the moment it joins by translation PK(id).



    @Entity
    @Table(name = "translation")
    @SecondaryTable(name = "language", pkJoinColumns = @PrimaryKeyJoinColumn(name = "id"))
    public class Translation {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(table = "language", name = "name")
    // ON translation.language_id = language.id
    private String language;

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

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

    // getters and setters
    }


    Where I should specify it in my code to do it correctly?
    SQL example: SELECT t.id, l.name, translation_key, translation_value FROM translation t INNER JOIN language l on t.language_id = l.id;









java spring postgresql hibernate jpa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 17:20







MZxFK

















asked Jan 3 at 17:08









MZxFKMZxFK

1106




1106








  • 1





    You do understand that the relation between entries in a primary and a secondary table is necessarily one-to-one, right?

    – crizzis
    Jan 3 at 17:10













  • Why it is one to one? I think different translations can have the same language. So, it's many-to-one, i think.

    – MZxFK
    Jan 3 at 17:15













  • Well then, you cannot join a secondary table to a primary table using anything other than its id. language is not part of the Translation entity's id

    – crizzis
    Jan 3 at 17:19













  • And what I should do to get what I want? To create separate language entity?

    – MZxFK
    Jan 3 at 17:21






  • 1





    If you want a many-to-one association, you cannot use a secondary table. Use a separate Language entity and a @ManyToOne instead

    – crizzis
    Jan 3 at 17:22
















  • 1





    You do understand that the relation between entries in a primary and a secondary table is necessarily one-to-one, right?

    – crizzis
    Jan 3 at 17:10













  • Why it is one to one? I think different translations can have the same language. So, it's many-to-one, i think.

    – MZxFK
    Jan 3 at 17:15













  • Well then, you cannot join a secondary table to a primary table using anything other than its id. language is not part of the Translation entity's id

    – crizzis
    Jan 3 at 17:19













  • And what I should do to get what I want? To create separate language entity?

    – MZxFK
    Jan 3 at 17:21






  • 1





    If you want a many-to-one association, you cannot use a secondary table. Use a separate Language entity and a @ManyToOne instead

    – crizzis
    Jan 3 at 17:22










1




1





You do understand that the relation between entries in a primary and a secondary table is necessarily one-to-one, right?

– crizzis
Jan 3 at 17:10







You do understand that the relation between entries in a primary and a secondary table is necessarily one-to-one, right?

– crizzis
Jan 3 at 17:10















Why it is one to one? I think different translations can have the same language. So, it's many-to-one, i think.

– MZxFK
Jan 3 at 17:15







Why it is one to one? I think different translations can have the same language. So, it's many-to-one, i think.

– MZxFK
Jan 3 at 17:15















Well then, you cannot join a secondary table to a primary table using anything other than its id. language is not part of the Translation entity's id

– crizzis
Jan 3 at 17:19







Well then, you cannot join a secondary table to a primary table using anything other than its id. language is not part of the Translation entity's id

– crizzis
Jan 3 at 17:19















And what I should do to get what I want? To create separate language entity?

– MZxFK
Jan 3 at 17:21





And what I should do to get what I want? To create separate language entity?

– MZxFK
Jan 3 at 17:21




1




1





If you want a many-to-one association, you cannot use a secondary table. Use a separate Language entity and a @ManyToOne instead

– crizzis
Jan 3 at 17:22







If you want a many-to-one association, you cannot use a secondary table. Use a separate Language entity and a @ManyToOne instead

– crizzis
Jan 3 at 17:22














1 Answer
1






active

oldest

votes


















1














You cannot use @SecondaryTable for the purpose you describe.



@SecondaryTable is used when a single entity is spread across multiple tables. Each of these 'pieces' of the entity must be privately owned by the entity, and is in a one-to-one relation with every other 'piece'.



If you want a many-to-one relation between translations and languages, you need to use @ManyToOne (and create a separate Language entity) instead.






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%2f54026764%2fhibernate-secondarytable-specifying-foreign-key-of-primary-table%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









    1














    You cannot use @SecondaryTable for the purpose you describe.



    @SecondaryTable is used when a single entity is spread across multiple tables. Each of these 'pieces' of the entity must be privately owned by the entity, and is in a one-to-one relation with every other 'piece'.



    If you want a many-to-one relation between translations and languages, you need to use @ManyToOne (and create a separate Language entity) instead.






    share|improve this answer




























      1














      You cannot use @SecondaryTable for the purpose you describe.



      @SecondaryTable is used when a single entity is spread across multiple tables. Each of these 'pieces' of the entity must be privately owned by the entity, and is in a one-to-one relation with every other 'piece'.



      If you want a many-to-one relation between translations and languages, you need to use @ManyToOne (and create a separate Language entity) instead.






      share|improve this answer


























        1












        1








        1







        You cannot use @SecondaryTable for the purpose you describe.



        @SecondaryTable is used when a single entity is spread across multiple tables. Each of these 'pieces' of the entity must be privately owned by the entity, and is in a one-to-one relation with every other 'piece'.



        If you want a many-to-one relation between translations and languages, you need to use @ManyToOne (and create a separate Language entity) instead.






        share|improve this answer













        You cannot use @SecondaryTable for the purpose you describe.



        @SecondaryTable is used when a single entity is spread across multiple tables. Each of these 'pieces' of the entity must be privately owned by the entity, and is in a one-to-one relation with every other 'piece'.



        If you want a many-to-one relation between translations and languages, you need to use @ManyToOne (and create a separate Language entity) instead.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 13:52









        crizziscrizzis

        3,49311523




        3,49311523
































            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%2f54026764%2fhibernate-secondarytable-specifying-foreign-key-of-primary-table%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas