How to make Composite Primary Key Using Two Foreign key?
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 13:09
Prabhat Yadav
1762417
1762417
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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 |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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 at 3:31
Brian Vosburgh
1,9831911
1,9831911
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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