Question

Inspection Entity:

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

And the table is created as follows:

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

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]

Am I missing some relationship here? It looks as though the list_index is being ignored completely.

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.

Was it helpful?

Solution 2

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.

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?

OTHER TIPS

I have done this before but using the @OrderBy annotation, for instance, an piece of code I wrote recently:

@OneToMany(mappedBy = "product")
@OrderBy("createdDateTime ASC")
private Collection<SkuUpc> skuUpcs;

Where SkuUpc has a fied

@Column(name = "created_dt")
private Date createdDateTime = new Timestamp(new Date().getTime());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top