Bidirectional OneToMany and ManyToOne returns “NULL not allowed for column” on save

Bidirectional OneToMany and ManyToOne returns “NULL not allowed for column” on save

This is a shortened version of the entities where I only show the relevant parts.

This is a shortened version of the entities where I only show the relevant parts.

    @Entity
    @Data
    public class Wrapper {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id

    @OneToOne(mappedBy = "wrapper", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    private Application application;

    public Wrapper(Application application) {
        this.application = application;
        application.setWrapper(this);
    }
}

@Data
@Entity
@EqualsAndHashCode(exclude = "wrapper")
public class Application {
    @Id
    private Integer id;

    @JsonIgnore
    @OneToOne
    @JoinColumn(name = "id")
    @MapsId
    private Wrapper wrapper;

    @OneToMany(mappedBy = "application", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    @SortNatural
    private SortedSet<Apartement> ownedApartements = new TreeSet<>();
}

@Entity
@Data
public class Apartement {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "application_id", insertable = false, updatable = false)
    private Application application;
}

@Repository public interface WrapperRepository extends JpaRepository<Wrapper, Integer> { }

The above entities generates the following create table statements:

create table Wrapper (
       id int identity not null,
        primary key (id)
    )

create table Application ( id int not null, primary key (id) )

create table Apartement (
   id int identity not null,
    application_id int not null,
    primary key (id)
)

 alter table Apartement 
   add constraint FKsrweh1i1p29mdjfp03or318od 
   foreign key (application_id) 
   references Application

   alter table Application
   add constraint FKgn7j3pircupa2rbqn8yte6kyc 
   foreign key (id) 
   references Wrapper

Given the follow entities and the following code:

Apartement apartement1 = new Apartement()
Apartement apartement2 = new Apartement()

Wrapper wrapper = new Wrapper(new Application());

Application application = wrapper.getApplication(); application.getOwnedApartements().addAll(Arrays.asList(apartement1, apartement2)); apartement1.setApplication(application); apartement2.setApplication(application);

WrapperRepository.saveAndFlush(wrapper);

I see three inserts in the log. First wrapper, then application, and finally apartement. But for some reason application_id is null on the first save. But I know it has a bi-directional relationship.

The error I get is:

Caused by: org.h2.jdbc.JdbcSQLException: NULL not allowed for column "APPLICATION_ID"; SQL statement:
insert into Apartement (id) values (null) [23502-197]

Why does this happen? Do I need to store everything in the correct order? Do I need to first store wrapper and application, then finally store the apartement once I have application ID? Cannot hibernate store all three in one go? Or figure this out it self?

java spring hibernate jpa

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Multitenant configuration: StaleObjectStateException on Transaction (hibernate + spring-data-jpa)

i'm trying to setup a configuration for manage a multi-tenant enviroment with spring-boot, spring-data-jpa, hibernate and mysql (same schema, every tenant table has a tenant_code column). For dependencies the parent maven project is&nbsp;<strong>spring-boot-starter-parent</strong>&nbsp;(2.1.2.RELEASE).

Understand to Spring Boot: MySQL and JPA, Hibernate

Spring Boot uses Hibernate as the default JPA implementation. The property spring.jpa.hibernate.ddl-auto is used for database initialization. I've used the value “update” for this property.

Spring Boot + JPA + Hibernate + Oracle

In this tutorial, we will learn how to create a Spring Boot application that communicates with an Oracle data source through Hibernate.

Learn Hibernate and Spring for Beginners (Includes Spring Boot)

Spring 5: Learn Spring 5 Core, AOP, Spring MVC, Spring Security, Spring REST, Spring Boot 2, Thymeleaf, JPA & Hibernate