简体   繁体   English

无法使用带有 InhertanceType.JOINED 的 Spring JPA 持久化实体

[英]Cannot persist entity with Spring JPA with InhertanceType.JOINED

I have the following JPA entity class.我有以下 JPA 实体 class。 And another Entity OptProject that extends Project.还有另一个扩展 Project 的实体 OptProject。 I'm using InheritanceType.JOINED我正在使用 InheritanceType.JOINED

public class Project {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Long id;

    @Basic
    @Column(name = "NAME", nullable = false, length = 150)
    private String name;

    @Basic
    @Column(name = "DESCRIPTION", nullable = false, length = 500)
    private String description;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

@Entity
@Table( name = "opt_project")
public class OptProject extends Project {

    @OneToMany(mappedBy = "project", cascade = {CascadeType.ALL})
    private List<Case> cases = new ArrayList<>();

    public OptProject() {}

    public OptProject(String name, String description) {
        super(name, description);
    }        
}

The JPA repository for OptProject entity is the following OptProject 实体的 JPA 存储库如下

@Repository
public interface OptProjectRepository extends CrudRepository<OptProject, Long> {

}

When I create a OptProject and try to save it, I get the error message below.当我创建一个 OptProject 并尝试保存它时,我收到以下错误消息。 It makes zero sense to me since I'm using @GeneratedValue(strategy = GenerationType.IDENTITY) for SQL server to generate the ID primary key.这对我来说是零意义,因为我使用 @GeneratedValue(strategy = GenerationType.IDENTITY) 为 SQL 服务器生成 ID 主键。

    OptProject optProject = new OptProject("name", "description");
    OptProject savedProject = optProjectRepo.save(optProject);

com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'optimization_project' when IDENTITY_INSERT is set to OFF. com.microsoft.sqlserver.jdbc.SQLServerException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“optimization_project”中插入标识列的显式值。

After turning hibernate logging to debug with the following snippets将 hibernate 日志记录转为使用以下代码段进行调试后

logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=trace

These are the hibernate SQL logs:这些是 hibernate SQL 日志:

DEBUG org.hibernate.SQL - insert into dbo.project (name, description) values (?, ?)
TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [name]
TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [VARCHAR] - [description]
DEBUG org.hibernate.SQL - insert into opt_project (id) values (?)
TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [11]
WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 544, SQLState: S0001
ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Cannot insert explicit value for identity column in table 'opt_project' when IDENTITY_INSERT is set to OFF.

The first SQL statement to insert the data into the table for the base class Project is correct.第一个 SQL 语句将数据插入表中,用于基础 class 项目是正确的。 But the SQL statement to insert the data to the second table for the child class OptProject is the issue.但是 SQL 语句将数据插入到子 class OptProject 的第二个表是问题所在。 It is manually adding the ID with value 11 and not letting the db generate it perhaps because there is only field here the ID.它是手动添加值为 11 的 ID,而不是让 db 生成它,可能是因为这里只有 ID 字段。 I'm not sure what is going on here.我不确定这里发生了什么。

It is manually adding the ID with value 11 and not letting the db generate it perhaps because there is only field here the ID.它是手动添加值为 11 的 ID,而不是让 db 生成它,可能是因为这里只有 ID 字段。 I'm not sure what is going on here.我不确定这里发生了什么。

You are using joined inheritance type.您使用的是加入 inheritance 类型。 The dependent entity must have the same PK as the parent, otherwise how exactly are they going to be joined ?依赖实体必须与父实体具有相同的 PK,否则它们将如何加入 The only way that is going to happen then is if the ID value for the dependent entity is manually set on insert.唯一会发生的方法是在插入时手动设置依赖实体的 ID 值。 So therefore you need to change the column definition in the opt_project table so it is not an Identity column.因此,您需要更改opt_project表中的列定义,使其不是Identity 列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring / JPA / Hibernate持久化实体:没有任何事情发生 - Spring/JPA/Hibernate persist entity : Nothing happen 无法在JPA + Spring中持久化实体,没有错误 - Not able to Persist entity in JPA + Spring, No error 无法在春季3中在JPA中保留实体 - not being able to persist entity in JPA in spring 3 Spring JPA 基于接口的投影在连接实体上返回 null - Spring JPA Interface based projection returns null on a joined entity 无法使用JPA(Hibernate)在连接的继承链中按根类加载实体 - Cannot load entity by root class in joined inheritance chain with JPA (Hibernate) 在ForEach(Spring MVC + JPA)中的事务上下文中持久化实体 - Persist an Entity in a Transactional Context in a ForEach (Spring MVC + JPA) SPRING JPA-org.hibernate.PersistentObjectException:传递给持久化的分离实体: - SPRING JPA - org.hibernate.PersistentObjectException: detached entity passed to persist: Spring 数据 JPA 和 hibernate 分离的实体传递以保持多对多关系 - Spring data JPA and hibernate detached entity passed to persist on ManyToMany relationship 分离的实体传递给持久化 JPA Spring 引导保存 - detached entity passed to persist JPA Spring boot save Spring Data JPA方法,用于获取具有连接表的实体 - Spring Data JPA method for getting the Entity with Joined table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM