0

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);
Dimitri
  • 8,122
  • 19
  • 71
  • 128
  • seems like the parent table doesn't has entry for TRACK_ID that you'r trying to ref in the child table – avisheks Jul 30 '14 at 10:04
  • Can you be more specific ? – Dimitri Jul 30 '14 at 10:22
  • do the parent table(TRACKS) has the row that your are referencing in the child table(CD_TRACKS)? – avisheks Jul 30 '14 at 10:39
  • Yes. The parent table TRACKS has an ID with NOT NULL AUTO_INCREMENT – Dimitri Jul 30 '14 at 11:17
  • No, I am not asking if the parent table id is auto_increment[that doesn't matter], I am doubting if you are picking up the correct_id from the parent table, try printing the id from both parent and the child table. – avisheks Jul 30 '14 at 11:25
  • Well, this problem happens while inserting. So I do not have any id for both entities – Dimitri Jul 30 '14 at 12:02
  • I would suggest, figure out the error on the inserting and get the id with last_insert_id() to ref for the child table. – avisheks Jul 30 '14 at 12:06
  • The error happends in the insert, so I can't get the Id. It's mysql exception – Dimitri Jul 30 '14 at 12:13
  • Something to look at if you have not mapped the tables and allowing Hibernate to generate the tables (except of course the join table) is the table columns. Hibernate by default uses camelCase for table names and columns, so in table Track, it looks for Column ID, yet if it was generated by Hibernate it would be id. So if you are on unix system, check – maress Jul 30 '14 at 12:35
  • No, all the tables are created with SQL scripts and are mapped with hibernate. All the columns are uppercased – Dimitri Jul 30 '14 at 12:37
  • Show us how are you trying to persist your CD with tracks. Check at leas `Hibernate`'s SQL for insert statements. H probably is trying to persist relation before persisting entities on both sides of that relation. – Antoniossss Jul 30 '14 at 13:09
  • I just updated my post – Dimitri Jul 30 '14 at 15:07
  • Check this answer :http://stackoverflow.com/questions/15438840/mysql-error-1364-field-doesnt-have-a-default-values – Jeffrey Nicholson Carré Jul 31 '14 at 03:11

1 Answers1

0

For someone who comes here, I had the similar error for one of my project. When I tried everything as I couldn't spot anything wrong with the entities. I recreated the database and the same c ode base started to work. It seemed to be caused by some internal issue on the database that kept flagging some missing value on one of the fields that I was trying to persist which on the application server side I had already modified.

Yoku
  • 307
  • 5
  • 16