I am working on spring boot api where I have a entity model course and another is subjects.
Under a course I want to have a list of multiple subjects.I am using a OneToMany
annotation to do this.
Here's my code:
course.java
@Entity
@EntityListeners(AuditingEntityListener.class)
public class course {
@Override
public String toString() {
return "course [id=" + id + ", title=" + title + ", description=" + description + "]";
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<subject> getSubjects() {
return subjects;
}
public void setSubjects(List<subject> subjects) {
this.subjects = subjects;
}
public String getProperty(String key) {
switch (key) {
case "title":
return this.title;
case "description":
return this.description;
case "date":
return this.date.toString();
case "id":
return Integer.toString(this.id);
default:
return "helo";
}
}
public course() {
super();
// TODO Auto-generated constructor stub
}
public course(int id, String title, List<subject> subjects, String description, Date date) {
super();
this.id = id;
this.title = title;
this.subjects = subjects;
this.description = description;
this.date = date;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String title;
@OneToMany
private List<subject> subjects;
private String description;
@CreatedDate
@Temporal(TemporalType.DATE)
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
subject.java
@Entity
public class subject {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getComplexity() {
return complexity;
}
public void setComplexity(String complexity) {
this.complexity = complexity;
}
public subject(String name, String complexity) {
super();
this.name = name;
this.complexity = complexity;
}
@Id
private String name;
private String complexity;
}
Now When I send a POST
request like:
{
"title":"test course sossk",
"description":"Is it",
"subjects":[
{
"name":"java",
"complexity":"easy"
},
{
"name":"c++",
"complexity":"easy"
}
]
}
I get a error like this:
SQL Error: 1452, SQLState: 23000
2021-07-18 20:01:48.818 ERROR 10408 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Cannot add or update a child row: a foreign key constraint fails (`courses`.`course_subjects`, CONSTRAINT `FKgy72njp8nmip43dy1cwk43ue6` FOREIGN KEY (`subjects_name`) REFERENCES `subject` (`name`))
2021-07-18 20:01:48.819 INFO 10408 --- [nio-8080-exec-2] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
2021-07-18 20:01:48.861 ERROR 10408 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException:
How can I solve this? How can I add a foreign key?
any help will be highly appreciated.
Update
course.java
@Entity
@EntityListeners(AuditingEntityListener.class)
public class course {
@Override
public String toString() {
return "course [id=" + id + ", title=" + title + ", description=" + description + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<subject> getSubjects() {
return subjects;
}
public void setSubjects(List<subject> subjects) {
this.subjects = subjects;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getProperty(String key) {
switch (key) {
case "title":
return this.title;
case "description":
return this.description;
case "date":
return this.date.toString();
case "id":
return Integer.toString(this.id);
default:
return "helo";
}
}
public course() {
super();
// TODO Auto-generated constructor stub
}
public course(int id, String title, List<subject> subjects, String description, Date date) {
super();
this.id = id;
this.title = title;
this.subjects = subjects;
this.description = description;
this.date = date;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String title;
@OneToMany(targetEntity = subject.class,cascade = CascadeType.ALL)
private List<subject> subjects;
private String description;
@CreatedDate
@Temporal(TemporalType.DATE)
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
subject.java
@Entity
public class subject {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getComplexity() {
return complexity;
}
public void setComplexity(String complexity) {
this.complexity = complexity;
}
public subject(String name, String complexity) {
super();
this.name = name;
this.complexity = complexity;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
private String complexity;
}