How to make Composite Primary Key Using Two Foreign key?
I have Three table in which one table is using the foreign key of other two table to make the composite primary key. Now i am using the @Embeddable but as both the key are the foreign key, How i will create the composite primary key now in the Entity.
CREATE TABLE table1
(table1id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (table1id));
table2
CREATE TABLE table2
(table2id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (table2id));
table3
CREATE TABLE table3 (
table1id INT NOT NULL,
table2id INT NOT NULL,
PRIMARY KEY (table1id, table2id),
FOREIGN KEY (table1id) REFERENCES table1 (table1id),
FOREIGN KEY (table2id) REFERENCES table2 (table2id)
);
How to convert this table into an Hibenate Entity.
@Entity
@Table(name="table3")
class Table1 {
@Id
long table1id;
//getter and setter
}
@Entity
@Table(name="table3")
class Table2 {
@Id
long table2id;
//getter and Setter
}
@Entity
@Table(name="table3")
class Table3 {
@EmbeddedId
private table3PK table3PKId;
//getter and Setter
}
@Embeddable
Class table3PK{
@ManyToOne
@JoinColumn(name="table1Id" ,referencedColumnName="table1id")
Table1 table1;
@ManyToOne
@JoinColumn(name="table2Id" ,referencedColumnName="table2id")
Table2 table2;
public table3PK(){
}
public table3PK(Table1 table1 ,Table2 table2){
this.table1;
this.table2;
}
}
}
hibernate jpa spring-data-jpa
add a comment |
I have Three table in which one table is using the foreign key of other two table to make the composite primary key. Now i am using the @Embeddable but as both the key are the foreign key, How i will create the composite primary key now in the Entity.
CREATE TABLE table1
(table1id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (table1id));
table2
CREATE TABLE table2
(table2id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (table2id));
table3
CREATE TABLE table3 (
table1id INT NOT NULL,
table2id INT NOT NULL,
PRIMARY KEY (table1id, table2id),
FOREIGN KEY (table1id) REFERENCES table1 (table1id),
FOREIGN KEY (table2id) REFERENCES table2 (table2id)
);
How to convert this table into an Hibenate Entity.
@Entity
@Table(name="table3")
class Table1 {
@Id
long table1id;
//getter and setter
}
@Entity
@Table(name="table3")
class Table2 {
@Id
long table2id;
//getter and Setter
}
@Entity
@Table(name="table3")
class Table3 {
@EmbeddedId
private table3PK table3PKId;
//getter and Setter
}
@Embeddable
Class table3PK{
@ManyToOne
@JoinColumn(name="table1Id" ,referencedColumnName="table1id")
Table1 table1;
@ManyToOne
@JoinColumn(name="table2Id" ,referencedColumnName="table2id")
Table2 table2;
public table3PK(){
}
public table3PK(Table1 table1 ,Table2 table2){
this.table1;
this.table2;
}
}
}
hibernate jpa spring-data-jpa
add a comment |
I have Three table in which one table is using the foreign key of other two table to make the composite primary key. Now i am using the @Embeddable but as both the key are the foreign key, How i will create the composite primary key now in the Entity.
CREATE TABLE table1
(table1id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (table1id));
table2
CREATE TABLE table2
(table2id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (table2id));
table3
CREATE TABLE table3 (
table1id INT NOT NULL,
table2id INT NOT NULL,
PRIMARY KEY (table1id, table2id),
FOREIGN KEY (table1id) REFERENCES table1 (table1id),
FOREIGN KEY (table2id) REFERENCES table2 (table2id)
);
How to convert this table into an Hibenate Entity.
@Entity
@Table(name="table3")
class Table1 {
@Id
long table1id;
//getter and setter
}
@Entity
@Table(name="table3")
class Table2 {
@Id
long table2id;
//getter and Setter
}
@Entity
@Table(name="table3")
class Table3 {
@EmbeddedId
private table3PK table3PKId;
//getter and Setter
}
@Embeddable
Class table3PK{
@ManyToOne
@JoinColumn(name="table1Id" ,referencedColumnName="table1id")
Table1 table1;
@ManyToOne
@JoinColumn(name="table2Id" ,referencedColumnName="table2id")
Table2 table2;
public table3PK(){
}
public table3PK(Table1 table1 ,Table2 table2){
this.table1;
this.table2;
}
}
}
hibernate jpa spring-data-jpa
I have Three table in which one table is using the foreign key of other two table to make the composite primary key. Now i am using the @Embeddable but as both the key are the foreign key, How i will create the composite primary key now in the Entity.
CREATE TABLE table1
(table1id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (table1id));
table2
CREATE TABLE table2
(table2id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (table2id));
table3
CREATE TABLE table3 (
table1id INT NOT NULL,
table2id INT NOT NULL,
PRIMARY KEY (table1id, table2id),
FOREIGN KEY (table1id) REFERENCES table1 (table1id),
FOREIGN KEY (table2id) REFERENCES table2 (table2id)
);
How to convert this table into an Hibenate Entity.
@Entity
@Table(name="table3")
class Table1 {
@Id
long table1id;
//getter and setter
}
@Entity
@Table(name="table3")
class Table2 {
@Id
long table2id;
//getter and Setter
}
@Entity
@Table(name="table3")
class Table3 {
@EmbeddedId
private table3PK table3PKId;
//getter and Setter
}
@Embeddable
Class table3PK{
@ManyToOne
@JoinColumn(name="table1Id" ,referencedColumnName="table1id")
Table1 table1;
@ManyToOne
@JoinColumn(name="table2Id" ,referencedColumnName="table2id")
Table2 table2;
public table3PK(){
}
public table3PK(Table1 table1 ,Table2 table2){
this.table1;
this.table2;
}
}
}
hibernate jpa spring-data-jpa
hibernate jpa spring-data-jpa
asked Nov 19 '18 at 13:09
Prabhat YadavPrabhat Yadav
1962417
1962417
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
It's not clear what problem you are encountering; but you might try using "derived identities" and mapping your entities like this:
@Entity
public class Table1 {
@Id
long table1id;
// ...
}
@Entity
public class Table2 {
@Id
long table2id;
// ...
}
@Embeddable
public class Table3PK {
long table1PK; // corresponds to PK type of Table1
long table2PK; // corresponds to PK type of Table2
}
@Entity
public class Table3 {
@EmbeddedId
private Table3PK id;
@MapsId("table1PK") // maps table1PK attribute of embedded id
@ManyToOne
Table1 table1;
@MapsId("table2PK") // maps table2PK attribute of embedded id
@ManyToOne
Table2 table2;
// ...
}
Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.
add a comment |
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
});
}
});
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%2f53375369%2fhow-to-make-composite-primary-key-using-two-foreign-key%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
It's not clear what problem you are encountering; but you might try using "derived identities" and mapping your entities like this:
@Entity
public class Table1 {
@Id
long table1id;
// ...
}
@Entity
public class Table2 {
@Id
long table2id;
// ...
}
@Embeddable
public class Table3PK {
long table1PK; // corresponds to PK type of Table1
long table2PK; // corresponds to PK type of Table2
}
@Entity
public class Table3 {
@EmbeddedId
private Table3PK id;
@MapsId("table1PK") // maps table1PK attribute of embedded id
@ManyToOne
Table1 table1;
@MapsId("table2PK") // maps table2PK attribute of embedded id
@ManyToOne
Table2 table2;
// ...
}
Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.
add a comment |
It's not clear what problem you are encountering; but you might try using "derived identities" and mapping your entities like this:
@Entity
public class Table1 {
@Id
long table1id;
// ...
}
@Entity
public class Table2 {
@Id
long table2id;
// ...
}
@Embeddable
public class Table3PK {
long table1PK; // corresponds to PK type of Table1
long table2PK; // corresponds to PK type of Table2
}
@Entity
public class Table3 {
@EmbeddedId
private Table3PK id;
@MapsId("table1PK") // maps table1PK attribute of embedded id
@ManyToOne
Table1 table1;
@MapsId("table2PK") // maps table2PK attribute of embedded id
@ManyToOne
Table2 table2;
// ...
}
Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.
add a comment |
It's not clear what problem you are encountering; but you might try using "derived identities" and mapping your entities like this:
@Entity
public class Table1 {
@Id
long table1id;
// ...
}
@Entity
public class Table2 {
@Id
long table2id;
// ...
}
@Embeddable
public class Table3PK {
long table1PK; // corresponds to PK type of Table1
long table2PK; // corresponds to PK type of Table2
}
@Entity
public class Table3 {
@EmbeddedId
private Table3PK id;
@MapsId("table1PK") // maps table1PK attribute of embedded id
@ManyToOne
Table1 table1;
@MapsId("table2PK") // maps table2PK attribute of embedded id
@ManyToOne
Table2 table2;
// ...
}
Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.
It's not clear what problem you are encountering; but you might try using "derived identities" and mapping your entities like this:
@Entity
public class Table1 {
@Id
long table1id;
// ...
}
@Entity
public class Table2 {
@Id
long table2id;
// ...
}
@Embeddable
public class Table3PK {
long table1PK; // corresponds to PK type of Table1
long table2PK; // corresponds to PK type of Table2
}
@Entity
public class Table3 {
@EmbeddedId
private Table3PK id;
@MapsId("table1PK") // maps table1PK attribute of embedded id
@ManyToOne
Table1 table1;
@MapsId("table2PK") // maps table2PK attribute of embedded id
@ManyToOne
Table2 table2;
// ...
}
Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.
answered Nov 21 '18 at 3:31
Brian VosburghBrian Vosburgh
2,0131912
2,0131912
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%2f53375369%2fhow-to-make-composite-primary-key-using-two-foreign-key%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