JPA composite primary key/foreign key mapping
I have 2 table like
then i try to mapping with using @Embeddable
like This reference
Week_Effort.java
@Entity
@IdClass(Week_Effort.class)
@Table(name = "week_effort")
public class Week_Effort implements Serializable {
private static final long serialVersionUID = -2825819221813101109L;
@Id
@Column(name="week_id")
private int week_id;
@Id
@Column(name="timesheet_id")
private int timesheet_id;
@Column(name="day1")
private int day1;
@ManyToOne(fetch = FetchType.LAZY ,cascade=CascadeType.ALL)
@JoinColumn(name="timesheet_id" , referencedColumnName = "timesheet_id",insertable=false, updatable=false , foreignKey = @ForeignKey(name = "fkweekts"))
private Timesheet timesheet_id ;
.
.
.
//get and set
}
Timesheet.java
@Entity
@Table(name = "timesheet")
public class Timesheet implements Serializable{
private static final long serialVersionUID = 7014333990749288304L;
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "timesheet_id")
private int timesheet_id;
@OneToMany(mappedBy="timesheet_id" , cascade=CascadeType.ALL)
private Set<Week_Effort> weekeffort ;
.
.
.
//get and set
}
WeekEmbed.java
@Embeddable
public class WeekEmbed implements Serializable {
private static final long serialVersionUID = 3229945076116848141L;
int timesheet_id;
int week_id;
//get set
}
I use AJAX to post parameter as json like
{"timesheet_id":1,"day1":"8"} //and more 6 parameter
week_id generate by some process and then set it before add with CRUD in Jparepository.But error
java.sql.SQLException: Parameter index out of range (10 > number of parameters, which is 9)
How to fix this? What I'm doing wrong here?
java hibernate jpa
add a comment |
I have 2 table like
then i try to mapping with using @Embeddable
like This reference
Week_Effort.java
@Entity
@IdClass(Week_Effort.class)
@Table(name = "week_effort")
public class Week_Effort implements Serializable {
private static final long serialVersionUID = -2825819221813101109L;
@Id
@Column(name="week_id")
private int week_id;
@Id
@Column(name="timesheet_id")
private int timesheet_id;
@Column(name="day1")
private int day1;
@ManyToOne(fetch = FetchType.LAZY ,cascade=CascadeType.ALL)
@JoinColumn(name="timesheet_id" , referencedColumnName = "timesheet_id",insertable=false, updatable=false , foreignKey = @ForeignKey(name = "fkweekts"))
private Timesheet timesheet_id ;
.
.
.
//get and set
}
Timesheet.java
@Entity
@Table(name = "timesheet")
public class Timesheet implements Serializable{
private static final long serialVersionUID = 7014333990749288304L;
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "timesheet_id")
private int timesheet_id;
@OneToMany(mappedBy="timesheet_id" , cascade=CascadeType.ALL)
private Set<Week_Effort> weekeffort ;
.
.
.
//get and set
}
WeekEmbed.java
@Embeddable
public class WeekEmbed implements Serializable {
private static final long serialVersionUID = 3229945076116848141L;
int timesheet_id;
int week_id;
//get set
}
I use AJAX to post parameter as json like
{"timesheet_id":1,"day1":"8"} //and more 6 parameter
week_id generate by some process and then set it before add with CRUD in Jparepository.But error
java.sql.SQLException: Parameter index out of range (10 > number of parameters, which is 9)
How to fix this? What I'm doing wrong here?
java hibernate jpa
What is this about?foreignKey = @ForeignKey(name = "fkweekts")
– K.Nicholas
Nov 20 '18 at 3:44
it's just naming for FK . not effective to code. @K.Nicholas
– I3eaver
Nov 20 '18 at 3:50
I don't see you need such a thing. You should start with a much simpler setup - no@JoinColumn
annotation and get that working first. I don't even see where you think you are using your composite key.
– K.Nicholas
Nov 20 '18 at 3:52
add a comment |
I have 2 table like
then i try to mapping with using @Embeddable
like This reference
Week_Effort.java
@Entity
@IdClass(Week_Effort.class)
@Table(name = "week_effort")
public class Week_Effort implements Serializable {
private static final long serialVersionUID = -2825819221813101109L;
@Id
@Column(name="week_id")
private int week_id;
@Id
@Column(name="timesheet_id")
private int timesheet_id;
@Column(name="day1")
private int day1;
@ManyToOne(fetch = FetchType.LAZY ,cascade=CascadeType.ALL)
@JoinColumn(name="timesheet_id" , referencedColumnName = "timesheet_id",insertable=false, updatable=false , foreignKey = @ForeignKey(name = "fkweekts"))
private Timesheet timesheet_id ;
.
.
.
//get and set
}
Timesheet.java
@Entity
@Table(name = "timesheet")
public class Timesheet implements Serializable{
private static final long serialVersionUID = 7014333990749288304L;
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "timesheet_id")
private int timesheet_id;
@OneToMany(mappedBy="timesheet_id" , cascade=CascadeType.ALL)
private Set<Week_Effort> weekeffort ;
.
.
.
//get and set
}
WeekEmbed.java
@Embeddable
public class WeekEmbed implements Serializable {
private static final long serialVersionUID = 3229945076116848141L;
int timesheet_id;
int week_id;
//get set
}
I use AJAX to post parameter as json like
{"timesheet_id":1,"day1":"8"} //and more 6 parameter
week_id generate by some process and then set it before add with CRUD in Jparepository.But error
java.sql.SQLException: Parameter index out of range (10 > number of parameters, which is 9)
How to fix this? What I'm doing wrong here?
java hibernate jpa
I have 2 table like
then i try to mapping with using @Embeddable
like This reference
Week_Effort.java
@Entity
@IdClass(Week_Effort.class)
@Table(name = "week_effort")
public class Week_Effort implements Serializable {
private static final long serialVersionUID = -2825819221813101109L;
@Id
@Column(name="week_id")
private int week_id;
@Id
@Column(name="timesheet_id")
private int timesheet_id;
@Column(name="day1")
private int day1;
@ManyToOne(fetch = FetchType.LAZY ,cascade=CascadeType.ALL)
@JoinColumn(name="timesheet_id" , referencedColumnName = "timesheet_id",insertable=false, updatable=false , foreignKey = @ForeignKey(name = "fkweekts"))
private Timesheet timesheet_id ;
.
.
.
//get and set
}
Timesheet.java
@Entity
@Table(name = "timesheet")
public class Timesheet implements Serializable{
private static final long serialVersionUID = 7014333990749288304L;
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "timesheet_id")
private int timesheet_id;
@OneToMany(mappedBy="timesheet_id" , cascade=CascadeType.ALL)
private Set<Week_Effort> weekeffort ;
.
.
.
//get and set
}
WeekEmbed.java
@Embeddable
public class WeekEmbed implements Serializable {
private static final long serialVersionUID = 3229945076116848141L;
int timesheet_id;
int week_id;
//get set
}
I use AJAX to post parameter as json like
{"timesheet_id":1,"day1":"8"} //and more 6 parameter
week_id generate by some process and then set it before add with CRUD in Jparepository.But error
java.sql.SQLException: Parameter index out of range (10 > number of parameters, which is 9)
How to fix this? What I'm doing wrong here?
java hibernate jpa
java hibernate jpa
asked Nov 20 '18 at 2:51


I3eaverI3eaver
124
124
What is this about?foreignKey = @ForeignKey(name = "fkweekts")
– K.Nicholas
Nov 20 '18 at 3:44
it's just naming for FK . not effective to code. @K.Nicholas
– I3eaver
Nov 20 '18 at 3:50
I don't see you need such a thing. You should start with a much simpler setup - no@JoinColumn
annotation and get that working first. I don't even see where you think you are using your composite key.
– K.Nicholas
Nov 20 '18 at 3:52
add a comment |
What is this about?foreignKey = @ForeignKey(name = "fkweekts")
– K.Nicholas
Nov 20 '18 at 3:44
it's just naming for FK . not effective to code. @K.Nicholas
– I3eaver
Nov 20 '18 at 3:50
I don't see you need such a thing. You should start with a much simpler setup - no@JoinColumn
annotation and get that working first. I don't even see where you think you are using your composite key.
– K.Nicholas
Nov 20 '18 at 3:52
What is this about?
foreignKey = @ForeignKey(name = "fkweekts")
– K.Nicholas
Nov 20 '18 at 3:44
What is this about?
foreignKey = @ForeignKey(name = "fkweekts")
– K.Nicholas
Nov 20 '18 at 3:44
it's just naming for FK . not effective to code. @K.Nicholas
– I3eaver
Nov 20 '18 at 3:50
it's just naming for FK . not effective to code. @K.Nicholas
– I3eaver
Nov 20 '18 at 3:50
I don't see you need such a thing. You should start with a much simpler setup - no
@JoinColumn
annotation and get that working first. I don't even see where you think you are using your composite key.– K.Nicholas
Nov 20 '18 at 3:52
I don't see you need such a thing. You should start with a much simpler setup - no
@JoinColumn
annotation and get that working first. I don't even see where you think you are using your composite key.– K.Nicholas
Nov 20 '18 at 3:52
add a comment |
1 Answer
1
active
oldest
votes
From :Hibernate: Parameter index out of range (8 > number of parameters, which is 7)
I change @IdClass
to WeekEmbed.class and delete some part then add @Id
to object mapping.
Week_Effort.java
@Entity
@IdClass(WeekEmbed.class)
@Table(name = "week_effort")
public class Week_Effort implements Serializable {
private static final long serialVersionUID = -2825819221813101109L;
@Id
@Column(name="week_id")
private int week_id;
//Delete this part
//@Id
//@Column(name="timesheet_id")
//private int timesheet_id;
@Column(name="day1")
private int day1;
@Id
@ManyToOne(fetch = FetchType.LAZY )
@JoinColumn(name="timesheet_id" , referencedColumnName = "timesheet_id",insertable=false, updatable=false , foreignKey = @ForeignKey(name = "fkweekts"))
private Timesheet timesheet_id ;
.
.
.
//get and set
}
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%2f53385521%2fjpa-composite-primary-key-foreign-key-mapping%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
From :Hibernate: Parameter index out of range (8 > number of parameters, which is 7)
I change @IdClass
to WeekEmbed.class and delete some part then add @Id
to object mapping.
Week_Effort.java
@Entity
@IdClass(WeekEmbed.class)
@Table(name = "week_effort")
public class Week_Effort implements Serializable {
private static final long serialVersionUID = -2825819221813101109L;
@Id
@Column(name="week_id")
private int week_id;
//Delete this part
//@Id
//@Column(name="timesheet_id")
//private int timesheet_id;
@Column(name="day1")
private int day1;
@Id
@ManyToOne(fetch = FetchType.LAZY )
@JoinColumn(name="timesheet_id" , referencedColumnName = "timesheet_id",insertable=false, updatable=false , foreignKey = @ForeignKey(name = "fkweekts"))
private Timesheet timesheet_id ;
.
.
.
//get and set
}
add a comment |
From :Hibernate: Parameter index out of range (8 > number of parameters, which is 7)
I change @IdClass
to WeekEmbed.class and delete some part then add @Id
to object mapping.
Week_Effort.java
@Entity
@IdClass(WeekEmbed.class)
@Table(name = "week_effort")
public class Week_Effort implements Serializable {
private static final long serialVersionUID = -2825819221813101109L;
@Id
@Column(name="week_id")
private int week_id;
//Delete this part
//@Id
//@Column(name="timesheet_id")
//private int timesheet_id;
@Column(name="day1")
private int day1;
@Id
@ManyToOne(fetch = FetchType.LAZY )
@JoinColumn(name="timesheet_id" , referencedColumnName = "timesheet_id",insertable=false, updatable=false , foreignKey = @ForeignKey(name = "fkweekts"))
private Timesheet timesheet_id ;
.
.
.
//get and set
}
add a comment |
From :Hibernate: Parameter index out of range (8 > number of parameters, which is 7)
I change @IdClass
to WeekEmbed.class and delete some part then add @Id
to object mapping.
Week_Effort.java
@Entity
@IdClass(WeekEmbed.class)
@Table(name = "week_effort")
public class Week_Effort implements Serializable {
private static final long serialVersionUID = -2825819221813101109L;
@Id
@Column(name="week_id")
private int week_id;
//Delete this part
//@Id
//@Column(name="timesheet_id")
//private int timesheet_id;
@Column(name="day1")
private int day1;
@Id
@ManyToOne(fetch = FetchType.LAZY )
@JoinColumn(name="timesheet_id" , referencedColumnName = "timesheet_id",insertable=false, updatable=false , foreignKey = @ForeignKey(name = "fkweekts"))
private Timesheet timesheet_id ;
.
.
.
//get and set
}
From :Hibernate: Parameter index out of range (8 > number of parameters, which is 7)
I change @IdClass
to WeekEmbed.class and delete some part then add @Id
to object mapping.
Week_Effort.java
@Entity
@IdClass(WeekEmbed.class)
@Table(name = "week_effort")
public class Week_Effort implements Serializable {
private static final long serialVersionUID = -2825819221813101109L;
@Id
@Column(name="week_id")
private int week_id;
//Delete this part
//@Id
//@Column(name="timesheet_id")
//private int timesheet_id;
@Column(name="day1")
private int day1;
@Id
@ManyToOne(fetch = FetchType.LAZY )
@JoinColumn(name="timesheet_id" , referencedColumnName = "timesheet_id",insertable=false, updatable=false , foreignKey = @ForeignKey(name = "fkweekts"))
private Timesheet timesheet_id ;
.
.
.
//get and set
}
answered Nov 20 '18 at 4:01


I3eaverI3eaver
124
124
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%2f53385521%2fjpa-composite-primary-key-foreign-key-mapping%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
What is this about?
foreignKey = @ForeignKey(name = "fkweekts")
– K.Nicholas
Nov 20 '18 at 3:44
it's just naming for FK . not effective to code. @K.Nicholas
– I3eaver
Nov 20 '18 at 3:50
I don't see you need such a thing. You should start with a much simpler setup - no
@JoinColumn
annotation and get that working first. I don't even see where you think you are using your composite key.– K.Nicholas
Nov 20 '18 at 3:52