Home > Blockchain >  Unknown column in 'field list' in cascading persistance
Unknown column in 'field list' in cascading persistance

Time:11-06

I am using Spring Boot Version 2.5.2 , spring data jpa and MySQL Database

    CREATE TABLE Timesheet (
  `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
  `tsYear` VARCHAR(4) NOT NULL,
  `weekNumber` INT NOT NULL,
  `startDate` DATE NOT NULL,
  `endDate` DATE NOT NULL,
   PRIMARY KEY (`id`)
  );
  
CREATE TABLE TimesheetDays (
  `id` INT NOT NULL AUTO_INCREMENT,
  `timesheetId` BIGINT(20) NOT NULL,
  `dayNumber` INT NOT NULL,
  `dayDate` DATE NOT NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `tsdTimesheetId`
    FOREIGN KEY (`timesheetId`)
    REFERENCES Timesheet (`id`)
);



@Entity
@Table(name = "timesheet")
public class Timesheet {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    
    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "timesheet")
    private List<TimesheetDay> days = null;

}


@Entity
@Table(name = "timesheetdays")
public class TimesheetDay {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    
    @ManyToOne
    private Timesheet timesheet;


}

When i tried to save (timesheet and timesheetdays), for timesheetdays table, hibernate is generating wrong column.

[2m2021-11-05 18:29:44.147[0;39m [32mDEBUG[0;39m [35m15036[0;39m [2m---[0;39m [2m[nio-8080-exec-2][0;39m [36morg.hibernate.SQL [0;39m [2m:[0;39m insert into timesheetdays (daydate, daynumber, timesheet_id) values (?, ?, ?) [2m2021-11-05 18:29:44.147[0;39m [32mTRACE[0;39m [35m15036[0;39m [2m---[0;39m [2m[nio-8080-exec-2][0;39m [36mo.h.type.descriptor.sql.BasicBinder [0;39m [2m:[0;39m binding parameter [1] as [DATE] - [2017-12-20] [2m2021-11-05 18:29:44.148[0;39m [32mTRACE[0;39m [35m15036[0;39m [2m---[0;39m [2m[nio-8080-exec-2][0;39m [36mo.h.type.descriptor.sql.BasicBinder [0;39m [2m:[0;39m binding parameter [2] as [INTEGER] - [1] [2m2021-11-05 18:29:44.148[0;39m [32mTRACE[0;39m [35m15036[0;39m [2m---[0;39m [2m[nio-8080-exec-2][0;39m [36mo.h.type.descriptor.sql.BasicBinder [0;39m [2m:[0;39m binding parameter [3] as [INTEGER] - [18] [2m2021-11-05 18:29:44.148[0;39m [33m WARN[0;39m [35m15036[0;39m [2m---[0;39m [2m[nio-8080-exec-2][0;39m [36mo.h.engine.jdbc.spi.SqlExceptionHelper [0;39m [2m:[0;39m SQL Error: 1054, SQLState: 42S22 [2m2021-11-05 18:29:44.148[0;39m [31mERROR[0;39m [35m15036[0;39m [2m---[0;39m [2m[nio-8080-exec-2][0;39m [36mo.h.engine.jdbc.spi.SqlExceptionHelper [0;39m [2m:[0;39m Unknown column 'timesheet_id' in 'field list'

Since the generated column name is timesheet_id, it is not working. If the generated column name becomes timesheetid, it will work.

Please help to resolve this issue

CodePudding user response:

You need to add the @JoinColumn annotation to give the column name value specifically in your TimesheetDay entity.

@ManyToOne
@JoinColumn(name="timesheetid")
private Timesheet timesheet;
  • Related