Spring Boot H2中的@ElementCollection未删除(DataIntegrityViolationException)

qvsjd97n  于 2023-05-28  发布在  Spring
关注(0)|答案(1)|浏览(170)

当我试图从一个内部有@ElementCollection的表中删除一个条目时,我得到了H2-Database的以下异常。使用SQL Server,一切都很好。

application.yml

spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:h2_test;MODE=MSSQLServer;INIT=CREATE SCHEMA IF NOT EXISTS test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    username: sa
    password:
  liquibase:
    change-log: classpath:db/changelog.xml
    default-schema: test
    liquibase-schema: test
    enabled: false
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update

Component.class

@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table
@NoArgsConstructor
public class Component extends Identifiable {

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "component_sequence")
  @SequenceGenerator(name = "component_sequence", sequenceName = "component_sequence", allocationSize = 10)
  private Long id;

  @Column
  private String componentId;

  @ElementCollection
  @CollectionTable(name = "parts", joinColumns = @JoinColumn(name = "component_id"))
  @Column(name = "part")
  private List<Integer> parts = new ArrayList<>();

  ....

删除声明:

componentRepository.deleteById(componentId);

例外情况:

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["FKTCOK4MNSV7BPTFXPGQWRDFKAR: TEST.PARTS FOREIGN KEY(COMPONENT_ID) REFERENCES TEST.COMPONENT(ID)
rta7y2nd

rta7y2nd1#

这是一个通用的Hibernate问题https://hibernate.atlassian.net/browse/HHH-5529
为了解决这个问题,我向ElementCollection添加了以下注解。

@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name = "component_id")

相关问题