为什么JPA在基本用例中忽略了我的@OrderColumn? [英] Why is JPA ignoring my @OrderColumn in a basic use case?
问题描述
检查实体:
@Entity
@Table(name="INSPECTION")
public class Inspection implements Serializable
{
...
@OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, orphanRemoval=true)
@OrderColumn(name="LIST_INDEX", nullable=false)
@JoinColumn(name="INSPECTION_ID")
private List<RecommendationInstance> recommendations;
...
}
RecommendationInstance实体
RecommendationInstance Entity
@Entity
@Table(name = "RECOMMENDATION_INSTANCE")
public class RecommendationInstance implements Serializable
{
@SequenceGenerator(name="RECOMMENDATION_INST_SEQ_GEN", sequenceName="RECOMMENDATION_INST_SEQ", allocationSize=1, initialValue=100)
@Id @GeneratedValue(generator="RECOMMENDATION_INST_SEQ_GEN", strategy=GenerationType.SEQUENCE)
private Long id;
@Column(name="INSPECTION_ID")
private Long inspectionId;
@ManyToOne
@JoinColumn(name="RECOMMENDATION_ID")
private Recommendation recommendation;
@Column(name="DESCRIPTION")
private String description;
...
}
该表的创建过程如下:
CREATE TABLE "RECOMMENDATION_INSTANCE"
( "ID" NUMBER(19,0) NOT NULL,
"INSPECTION_ID" NUMBER(19,0) NOT NULL,
"RECOMMENDATION_ID" NUMBER(19,0) NOT NULL,
"DESCRIPTION" VARCHAR2(4000 BYTE) NOT NULL,
"LIST_INDEX" NUMBER(4,0) NOT NULL
) ;
创建新的RecommendationInstance并尝试保存InspectionEntity时,出现以下错误:
When a new RecommendationInstance is created and I attempt to save the InspectionEntity I get the following error:
Caused by: org.eclipse.persistence.exceptions.DatabaseException:
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10161 table: "RECOMMENDATION_INSTANCE" column: "LIST_INDEX"
Error Code: -10
Call: INSERT INTO RECOMMENDATION_INSTANCE (ID, DESCRIPTION, INSPECTION_ID, RECOMMENDATION_ID) VALUES (?, ?, ?, ?)
bind => [102, Sprinkler System DESCRIPTION, 110, 40]
我在这里错过了一些关系吗?看来list_index已被完全忽略.
Am I missing some relationship here? It looks as though the list_index is being ignored completely.
如果需要的话,为了提供更多信息,我确实使用联接表进行了这项工作.但是我正在进行重构,因为不需要联接表.这将LIST_INDEX列从联接表移到了RecommendationInstance表.
To give further information, if needed, I did have this working using a join table. However I am doing a refactor since the join table is not needed. This moved the LIST_INDEX column from the join table to the RecommendationInstance table.
推荐答案
我发现,当我删除NOT NULL约束后,一切都正常了(duh),但是我决定现在可以解决这个问题.查看日志,JPA首先插入不包含list_index的行(因此违反约束),然后在运行更新以设置list_index之后立即插入.
I found that when I removed the NOT NULL constraint then everything worked (duh), but I decided I can deal with that for now. Looking at the logs, JPA first inserts the row without the list_index (thus the constraint violation) then immediately after runs an update to set the list_index.
这个答案确实提出了一个更具体的问题,即为什么即使我指定了nullable=false
This answer really creates a more specific question as to why it doesn't set the list_index upon insertion of the row, even when I specify nullable=false
I asked the more specific question here: Why does JPA update the OrderColumn instead of setting it on creation?
这篇关于为什么JPA在基本用例中忽略了我的@OrderColumn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!