I am blocked on this error for a couple of days now and don't know how to resolve it. I have an entity CD that contains a list of tracks. Here is how I define them with JPA :
@Entity
public class Cd extends Media {
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name="CD_TRACKS",joinColumns={ @JoinColumn(name="CD_ID", referencedColumnName = "ID") },
inverseJoinColumns={ @JoinColumn(name="TRACK_ID",table = "TRACKS", referencedColumnName = "ID") })
private List<Tracks> tracks;
//other fields + getters/setters
}
The Tracks
class :
@Entity
public class Tracks {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
private String name;
private int duration;
//getters and setters
}
Here is the CD_TRACKS
script that creates the table :
CREATE TABLE CD_TRACKS(
CD_ID BIGINT NOT NULL,
TRACK_ID BIGINT NOT NULL,
FOREIGN KEY (CD_ID) REFERENCES CD(ID),
FOREIGN KEY (TRACK_ID) REFERENCES TRACKS(ID)
);
When I try to insert a new CD in the database, I get this error :
Caused by: java.sql.SQLException: Field 'TRACK_ID' doesn't have a default value
I tried setting the SET GLOBAL sql_mode='';
but it still doesn't work. I also verified in the Tracks table if the id is defined as primary and have auto_increment.
Can somebody gives me some insight to how to solve that problem.
Thanks for your help
[EDIT] Here for example that I use to persist a CD :
Cd cd = new Cd();
cd.setAvailable(true);
cd.setTitle("The essential of Michael Jackson");
Tracks tracks = new Tracks();
tracks.setName("Billie Jean");
tracks.setDuration(10);
cd.setTracks(Arrays.asList(tracks));
cdCatalogService.saveMedia(cd);