0

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
Serdar
  • 146
  • 8
  • The setup of the `@OneToMany`/`@ManyToOne` relation seems wrong. The `@ManyToOne`-side should be the the owning side, defining the `joinColum`, while the `@OneToMany`-side should use `mappedBy`, and no `@JoinColumn`: `@OneToMany(mappedBy = "hour", ...)`. – Turing85 Aug 07 '23 at 17:53
  • @Turing85 With these suggestions I have the same error – Serdar Aug 07 '23 at 18:00
  • 1
    You created a bidirectional relationship with parent(`Hour`) and children(`RegularHour`). If you want to save parent and its children with cascade you need to set the parent on you children also otherwise you will have your error. What about, loop on request and so you will have the fields values by setter methods ? – Andrei Lisa Aug 07 '23 at 20:01
  • What is the SQL command for creation of the table(s) with the column id_hour? – dimirsen Z Aug 08 '23 at 08:10
  • @dimirsenZ it's writing on the question. Part of `the error that I am getting`. Which starts with "insert" after "Hibernate:" – Serdar Aug 08 '23 at 09:24
  • it's INSERT command as I can see. Do you have CREATE TABLE command? You can see it if you inspect your DB with say Valentina Studio or some other GUI for DB navigation. There is a possibility, that you created the table with field description like id_hour bigint not null In this case your DB won't let you insert a null value As a fix I can see id_hour bigint not null default – dimirsen Z Aug 08 '23 at 09:33
  • if you want DB to generate hour_id for you, you need to use a specific column type like bigserial – dimirsen Z Aug 08 '23 at 09:42
  • @dimirsenZ oh my bad. here is my table creation CREATE TABLE `hour` ( `id_hour` int unsigned NOT NULL AUTO_INCREMENT, `twentyfourseven` tinyint(1) NOT NULL, PRIMARY KEY (`id_hour`) – Serdar Aug 08 '23 at 11:47
  • 1
    As someone else commented you need to ensure all references are set. In this case, you have not set the RegularHour.hour reference, which causes the foreign key to be set to null. Set that value, and change your Hour->RegularHour OneToMany to use mappedby="hour" and remove the join column definition, and it will work as you expect. – Chris Aug 08 '23 at 14:00
  • @Chris I changed the relation and updated the question as you mentioned but I am having the same error. On the other hand, I can not set `id_hour` because that is a new record. – Serdar Aug 12 '23 at 11:05
  • With the SQL - is the Hour instance being inserted? Unless there is a major bug in Hibernate, there must be some RegularHour instance getting into the context that hasn't had its hour reference set. Since there is a 1:M from Hour->RegularHour, are you sure every RegularHour instance managed or persisted in the context has a Hour instance reference? Maybe relax the constraint for testing and see all the RegularHour instances being inserted to make sure they are what you expect and that some other piece of code isn't doing something wrong – Chris Aug 13 '23 at 15:46
  • Might be related to [this](https://stackoverflow.com/questions/69884667/jpa-hibernate-springboot-inserting-onetomany-relationship-entities/69890168#69890168) – Eric Aug 18 '23 at 05:47

0 Answers0