【问题标题】:Why is PostPersist required to set the id of oneToOne child entity?为什么需要 PostPersist 来设置 oneToOne 子实体的 id?
【发布时间】:2019-08-18 19:51:36
【问题描述】:

我正在尝试实现双向 OneToOne 映射并从父实体 (Project.java) 插入子实体 (ProjectDetails.java)。但是,实体管理器正在尝试将 null 作为子实体 (ProjectDetails) 的 id 插入。

错误日志:

[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

我尝试从 @OneToOne 中删除 insertable=false 和 updatable=false,但它给了我错误,同一列不能被引用两次。

我有以下实体类。

类:项目

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;

}

类:项目详细信息

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;


}

类:项目控制器

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);
    }


}

类项目服务

package com.example.playground.service;

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

public interface ProjectService {
    Project createProject(Project projectDTO);

    Project getProject(Integer projectId);
}

类 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();
    }
}

JPA配置

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;
    }

}

和 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>

编辑:得到它的工作,但仍在寻找解释

如果我将以下代码添加到我的项目类中,它会按预期工作。

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

为什么需要 postPersist?鉴于关系被标记为 oneToOne,JPA 不应该自动填充这些值吗?有没有更好的办法?

【问题讨论】:

    标签: spring-boot jpa eclipselink


    【解决方案1】:

    JPA 正在按照您的指示进行操作:您有两个到“project_id”的映射,一个有值,OneToOne 是只读的。这意味着 JPA 必须从您保留为 null 的基本 ID 映射“projectId”中提取值,从而将 null 插入到字段中。

    这是一个常见问题,JPA 中有许多解决方案。首先是将@ID 映射标记为只读(可插入/可更新=false)并让关系映射控制值。

    不过,JPA 2.0 引入了其他解决方案。 对于相同的设置,您可以使用 @MapsId 注释标记关系。这告诉 JPA 关系外键值将在指定的 ID 映射中使用,并且会在没有 postPersist 方法的情况下完全按照您的预期为您设置它。

    JPA 2.0 中的另一个替代方法是您可以只将 OneToOne 标记为 ID 映射,然后从类中删除 projectId 属性。一个更复杂的例子显示在here

    【讨论】:

    • 克里斯,很少观察,虽然您的第一个解决方案适用于 eclipselink 但不适用于休眠。此外,带有 eclipselink 的输出 json 对于 projectDetails 的 projectId 为 null。在与 eclipselink 的关系上,您的第二个解决方案不适用于 insertable/updatable =false (运行到“应该为主键字段 [project_details.project_id] 定义一个非只读映射”)同时使用休眠;不确定不同行为的原因。通过删除关系上的 insertable/updatable=false ,它可以完美地工作。我是否需要在 @Id 上设置 insertable/updatable = false?
    • 如果使用 mapsId 注释,您不应该在关系或 id 映射上使用 insertable/updatable=false 标记,您是在告诉 JPA 关系控制 ID 属性值以及在何处绘制值来自。关系映射现在控制该字段并从中设置,因此它需要是可插入的。将其标记为可更新 = 假是不必要的,因为无论如何都无法更新 ID。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    相关资源
    最近更新 更多