Hibernate @SecondaryTable - Specifying foreign key of primary table
I have two tables:
language
CREATE TABLE language (
id BIGSERIAL,
name TEXT NOT NULL UNIQUE,
PRIMARY KEY (id)
);
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
|
show 2 more comments
I have two tables:
language
CREATE TABLE language (
id BIGSERIAL,
name TEXT NOT NULL UNIQUE,
PRIMARY KEY (id)
);
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
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 theTranslation
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 separateLanguage
entity and a@ManyToOne
instead
– crizzis
Jan 3 at 17:22
|
show 2 more comments
I have two tables:
language
CREATE TABLE language (
id BIGSERIAL,
name TEXT NOT NULL UNIQUE,
PRIMARY KEY (id)
);
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
I have two tables:
language
CREATE TABLE language (
id BIGSERIAL,
name TEXT NOT NULL UNIQUE,
PRIMARY KEY (id)
);
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
java spring postgresql hibernate jpa
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 theTranslation
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 separateLanguage
entity and a@ManyToOne
instead
– crizzis
Jan 3 at 17:22
|
show 2 more comments
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 theTranslation
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 separateLanguage
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
|
show 2 more comments
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 4 at 13:52
crizziscrizzis
3,49311523
3,49311523
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 theTranslation
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