I have 3 classes as below.
Location class
@ToString @Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity @Getter @Setter
@Table(name = "location")
public class Location {
@Id
@Column(name = "id")
private String id;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "id_hour")
private Hour hour;
}
Hour class
@Setter @Getter @Builder
@AllArgsConstructor @NoArgsConstructor
@Entity
@Table(name = "hour")
public class Hour {
@Id
@Column(name = "id_hour")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer idHour;
@Column(name = "twentyfourseven")
private Boolean twentyfourseven;
@OneToMany(mappedBy="hour", cascade = CascadeType.ALL)
private List<RegularHour> regularHours;
}
RegularHour class
@Setter @Getter @Builder
@AllArgsConstructor @NoArgsConstructor
@Entity
@Table(name = "regular_hour")
public class RegularHour {
@Id
@Column(name = "id_regular_hour")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer idRegularHour;
@Column(name = "weekday")
private Integer weekday;
@Column(name = "period_begin")
private String periodBegin;
@Column(name = "period_end")
private String periodEnd;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "id_hour")
private Hour hour;
}
The Problem:
I am trying to save my location object like this locationRepository.save(location);
Then I am having "Column 'id_hour' cannot be null" error while hibernate tries to insert data to regular_hour
table
What I am trying:
When I run locationRepository.save(location);
I want all of them saved in the related tables. PLEASE NOT THAT, I CANNOT SET id_hour
BECAUSE I AM CREATING NEW RECORD IN DB
What I have tried:
I have tried to change CascadeType
from location and Hour, I replaced ALL
with PERSIST
and MERGE
but none of them worked.
I tried to change label of Hour class as below
@OneToMany(mappedBy = "hour", cascade = CascadeType.ALL)
private List<HourExceptionalPeriod> exceptionalOpenings;
That didn't work too.
The error that I am getting
Hibernate:
insert
into
regular_hour
(id_hour, period_begin, period_end, weekday)
values
(?, ?, ?, ?)
2023-08-07 20:02:54.802 TRACE 24457 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [null]
2023-08-07 20:02:54.805 TRACE 24457 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - [07:00]
2023-08-07 20:02:54.808 TRACE 24457 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [VARCHAR] - [18:00]
2023-08-07 20:02:54.812 TRACE 24457 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [4] as [INTEGER] - [1]
2023-08-07 20:02:54.960 WARN 24457 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1048, SQLState: 23000
2023-08-07 20:02:54.962 ERROR 24457 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Column 'id_hour' cannot be null