H2 中的@ElementCollection 未删除(DataIntegrityViolationException)
@ElementCollection in H2 not deleted (DataIntegrityViolationException)
当我尝试从内部有 @ElementCollection 的 table 中删除一个条目时,我得到以下 H2 数据库异常。使用 SQL-服务器一切正常。
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)
这是一个一般的 Hibernate 问题 https://hibernate.atlassian.net/browse/HHH-5529。
为解决此问题,向 ElementCollection 添加了以下注释。
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name = "component_id")
当我尝试从内部有 @ElementCollection 的 table 中删除一个条目时,我得到以下 H2 数据库异常。使用 SQL-服务器一切正常。
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)
这是一个一般的 Hibernate 问题 https://hibernate.atlassian.net/browse/HHH-5529。
为解决此问题,向 ElementCollection 添加了以下注释。
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name = "component_id")