级联上违反了JPA / HIbernate完整性约束

时间:2017-04-28 09:35:09

标签: java sql spring hibernate

我有以下与WorkFlowDetail类有关系的实体:

@Entity()
@DynamicUpdate()
@Table(schema = "RACQUISTI", name = "RICHIESTA_DI_ACQUISTO")
public class RichiestaDiAcquisto {
   @Id
   @Column(name = "ID_RICHIESTA_ACQUISTO")
   @SequenceGenerator(name="SEQ_GEN",sequenceName="RICHIESTA_DI_ACQUISTO_SEQ",   allocationSize=1)
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_GEN")
   private int id;

   @OneToOne(fetch=FetchType.LAZY, cascade = {CascadeType.ALL})
   @JoinColumn(name="ID_RICHIESTA_ACQUISTO")
   private WorkFlowDetail workFlowDetail;

  .....
}

WorkFlowDetail实体是:

@Entity()
@Table(schema = "RACQUISTI", name = "WORKFLOW_DETAIL")
public class WorkFlowDetail {

   @Id
   @Column(name = "ID_RICHIESTA_ACQUISTO")
   private int idRichiestaDiAcquisto;

   @Column(name = "ID_WORKFLOW_INSTANCE")
   private String idWorkFlowInstance;

   ....
}

表WORKFLOW_DETAIL在RICHIESTA_DI_ACQUISTO主键上有一个外键。

当我执行以下代码时:

RichiestaDiAcquisto entity = new RichiestaDiAcquisto();
getSession().saveOrUpdate(entity);

我收到此错误:

Hibernate: select RICHIESTA_DI_ACQUISTO_SEQ.nextval from dual
Hibernate: select workflowde_.ID_RICHIESTA_ACQUISTO,    workflowde_.CURRENT_NODE_ID as CURRENT_2_2_, workflowde_.DATA_MOD as DATA_MOD3_2_, workflowde_.ID_WORKFLOW_INSTANCE as ID_WORKF4_2_, workflowde_.WORKFLOW_TOKEN as WORKFLOW5_2_ from RACQUISTI.WORKFLOW_DETAIL workflowde_ where workflowde_.ID_RICHIESTA_ACQUISTO=?
Hibernate: select richiedent_.ID_RICHIESTA_ACQUISTO, richiedent_.COGNOME as   COGNOME2_0_, richiedent_.GODIVA_ANAGRAFICA_ID as GODIVA_A3_0_, richiedent_.NOME as NOME4_0_ from RACQUISTI.RICHIEDENTE richiedent_ where richiedent_.ID_RICHIESTA_ACQUISTO=?
Hibernate: insert into RACQUISTI.RICHIESTA_DI_ACQUISTO (ACQUISTO_DI_BENI, CONFERMA_CAPITOLO, CONFERMA_ELEGIBILITA, COSTO_ACQUISTO, DATA_MOD, DESCRIZIONE_ACQUISTO, GARA_NAZIONALE_CATALOGO, ID_ANAG_MOD, LUOGO_DI_CONSEGNA, SIGLA_ESPERIMENTO, STATUS, STRUTTURA_PAGANTE, ID_RICHIESTA_ACQUISTO) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into RACQUISTI.WORKFLOW_DETAIL (CURRENT_NODE_ID, DATA_MOD, ID_WORKFLOW_INSTANCE, WORKFLOW_TOKEN, ID_RICHIESTA_ACQUISTO) values (?, ?, ?, ?, ?)
WARN    2017-04-28 10:14:28,817 [http-nio-8080-exec-6] org.hibernate.engine.jdbc.spi.SqlExceptionHelper  - SQL Error: 2291, SQLState: 23000
ERROR   2017-04-28 10:14:28,819 [http-nio-8080-exec-6] org.hibernate.engine.jdbc.spi.SqlExceptionHelper  - ORA-02291: integrity constraint (RACQUISTI.WORKFLOW_DETAIL_FK1) violated - parent key not found

2 个答案:

答案 0 :(得分:0)

您的insert语句正在尝试将值插入到RACQUISTI表中不存在的列中

答案 1 :(得分:0)

我找到了解决方案。我修改了主类child(WorkFlowDetail)上的注释:

@Entity()
@DynamicUpdate()
@Table(schema = "RACQUISTI", name = "RICHIESTA_DI_ACQUISTO")
public class RichiestaDiAcquisto {
 @Id
 @Column(name = "ID_RICHIESTA_ACQUISTO")     
 @SequenceGenerator(name="SEQ_GEN",sequenceName="RICHIESTA_DI_ACQUISTO_SEQ",   allocationSize=1)
 @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_GEN")
 private int id;

 @OneToOne(fetch=FetchType.LAZY, mappedBy = "richiestaDiAcquisto", cascade = {CascadeType.ALL})
 private WorkFlowDetail workFlowDetail;

 .....
}

然后我修改了子类:

@Entity()
@Table(schema = "RACQUISTI", name = "WORKFLOW_DETAIL")
public class WorkFlowDetail {
    @GenericGenerator(name = "generator", strategy = "foreign",
        parameters = @org.hibernate.annotations.Parameter(name = "property", value = "richiestaDiAcquisto"))
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name = "ID_RICHIESTA_ACQUISTO", unique = true, nullable = false)
    private int idRichiestaAcquisto;



  @OneToOne(fetch = FetchType.LAZY)
  @PrimaryKeyJoinColumn
  private RichiestaDiAcquisto richiestaDiAcquisto;

  ....

}

在保存主类之前(RichiestaDiAcquisto)我必须通过以下代码将父类放在子类上:

        richiestaDiAcquisto.getWorkFlowDetail().setRichiestaDiAcquisto(richiestaDiAcquisto);