Search code examples
spring-bootjpaeclipselink

Why is PostPersist required to set the id of oneToOne child entity?


I am trying to implement bidirectional OneToOne Mapping and insert child entity(ProjectDetails.java) from parent entity(Project.java). However, entity manager is trying to insert null as id of child entity(ProjectDetails).

Error logs:

[EL Fine]: sql: 2019-08-19 01:16:50.969--ClientSession(1320691525)--Connection(926343068)--INSERT INTO project (name) VALUES (?)
    bind => [Project]
[EL Fine]: sql: 2019-08-19 01:16:50.973--ClientSession(1320691525)--Connection(926343068)--SELECT @@IDENTITY
[EL Fine]: sql: 2019-08-19 01:16:50.983--ClientSession(1320691525)--Connection(926343068)--INSERT INTO project_details (project_id, details) VALUES (?, ?)
    bind => [null, Details]
[EL Fine]: sql: 2019-08-19 01:16:50.986--ClientSession(1320691525)--SELECT 1
[EL Warning]: 2019-08-19 01:16:50.991--UnitOfWork(1746098804)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.4.v20190115-ad5b7c6b2a): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column 'project_id' cannot be null
Error Code: 1048

I have tried by removing insertable=false, and updatable=false from @OneToOne but it gives me error, that same column can't be referenced twice.

I have following entity classes.

Class : Project

package com.example.playground.domain.dbo;

import com.example.playground.jsonviews.BasicView;
import com.example.playground.jsonviews.ProjectView;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Data;
import lombok.ToString;
import org.eclipse.persistence.annotations.JoinFetch;
import org.eclipse.persistence.annotations.JoinFetchType;

import javax.persistence.*;

@JsonView(BasicView.class)
@Data
@Entity
@Table(name = "project")
public class Project {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="project_id")
    private Integer projectId;

    @Column(name="name")
    private String name;

    @ToString.Exclude
    @JsonView(ProjectView.class)
    @OneToOne(mappedBy = "project", cascade = CascadeType.ALL, optional = false)
    private ProjectDetails projectDetails;

}

Class : ProjectDetails

package com.example.playground.domain.dbo;

import com.example.playground.jsonviews.BasicView;
import com.example.playground.jsonviews.ProjectDetailsView;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Data;
import lombok.ToString;

import javax.persistence.*;

@JsonView(BasicView.class)
@Data
@Entity
@Table(name = "project_details")
public class ProjectDetails {

    @Id
    @Column(name = "project_id")
    private Integer projectId;

    @ToString.Exclude
    @JsonView(ProjectDetailsView.class)
    @OneToOne
    @JoinColumn(name = "project_id", nullable = false, insertable = false, updatable = false)
    private Project project;

    @Column(name = "details")
    private String details;


}

class: ProjectController

package com.example.playground.web;

import com.example.playground.domain.dbo.Project;
import com.example.playground.jsonviews.ProjectView;
import com.example.playground.service.ProjectService;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/projects")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @GetMapping("/{projectId}")
    @JsonView(ProjectView.class)
    public ResponseEntity<Project> getProject(@PathVariable Integer projectId){
        Project project = projectService.getProject(projectId);
        return ResponseEntity.ok(project);
    }

    @PostMapping
    @JsonView(ProjectView.class)
    public ResponseEntity<Project> createProject(@RequestBody Project projectDTO){
        Project project =  projectService.createProject(projectDTO);
        return ResponseEntity.ok(project);
    }


}

class ProjectService

package com.example.playground.service;

import com.example.playground.domain.dbo.Project;

public interface ProjectService {
    Project createProject(Project projectDTO);

    Project getProject(Integer projectId);
}

class ProjectServiceImpl

package com.example.playground.impl.service;

import com.example.playground.domain.dbo.Project;
import com.example.playground.repository.ProjectRepository;
import com.example.playground.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectRepository projectRepository;

    @Transactional
    @Override
    public Project createProject(Project projectDTO) {
        projectDTO.getProjectDetails().setProject(projectDTO);
        return projectRepository.saveAndFlush(projectDTO);
    }

    @Override
    public Project getProject(Integer projectId) {
        return projectRepository.findById(projectId).get();
    }
}

JPAConfig

package com.example.playground.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableTransactionManagement(mode= AdviceMode.ASPECTJ)
public class JPAConfig{

    @Bean("dataSource")
    @ConfigurationProperties(prefix = "db1")
    public DataSource getDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean getEntityManager(@Qualifier("dataSource") DataSource dataSource){
        EclipseLinkJpaVendorAdapter adapter = new EclipseLinkJpaVendorAdapter();
        LocalContainerEntityManagerFactoryBean em =  new LocalContainerEntityManagerFactoryBean();
        em.setPackagesToScan("com.example.playground.domain.dbo");
        em.setDataSource(dataSource);
        em.setJpaVendorAdapter(adapter);
        em.setPersistenceUnitName("persistenceUnit");
        em.setJpaPropertyMap(getVendorProperties());
        return em;
    }

    @Bean(name = "transactionManager")
    public JpaTransactionManager
    transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory)
    {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }

    protected Map<String, Object> getVendorProperties()
    {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("eclipselink.ddl-generation", "none");
        map.put("eclipselink.ddl-generation.output-mode", "database");
        map.put("eclipselink.weaving", "static");
        map.put("eclipselink.logging.level.sql", "FINE");
        map.put("eclipselink.logging.parameters", "true");
        map.put(
                "eclipselink.target-database",
                "org.eclipse.persistence.platform.database.SQLServerPlatform");
        return map;
    }

}

and pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>playground</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Java-Spring-Boot-Playground</name>
    <description>Java playground.</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.1.9.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa</artifactId>
            <version>2.7.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jdbc -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
            <version>9.0.21</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Edit : Got it working, but still looking for explaination

If I add below code to my Project class , It works as expected.

  @PostPersist
    public void fillIds(){
        projectDetails.setProjectId(this.projectId);
    }

Buy why is postPersist required? Isn't JPA suppose to autofill these values given the relation is marked as oneToOne? Is there any better way?


Solution

  • JPA is following what you instructed it to do: You have two mappings to the "project_id", and the one with a value, the OneToOne is read-only. This means JPA must pull the value from the basic ID mapping 'projectId' which you have left as null, causing it to insert null into the field.

    This is a common issue and there are a number of solutions within JPA. First would have been to mark the @ID mapping as read-only (insertable/updatable=false) and let the relationship mapping control the value.

    JPA 2.0 brought in other solutions though. For this same setup, you can mark the relationship with the @MapsId annotation. This tells JPA that the relationship foreign key value is to be used in the specified ID mapping, and will set it for you exactly as you seem to expect without the postPersist method.

    Another alternative in JPA 2.0 is that you can just mark the OneToOne as the ID mapping, and remove the projectId property from the class. A more complex example is shown here