【问题标题】:ERROR org.hibernate.util.JDBCExceptionReporter - ORA-02291: integrity constraint (PERFMGMT.ZAUTOMATION_PHASE_FK1) violated - parent key not found错误 org.hibernate.util.JDBCExceptionReporter - ORA-02291:违反完整性约束 (PERFMGMT.ZAUTOMATION_PHASE_FK1) - 未找到父键
【发布时间】:2021-08-12 22:36:15
【问题描述】:

我有两张桌子。在第一个中,我有一个触发器,它为表创建主键

create or replace TRIGGER zutomation_num_trigger
before INSERT ON zautomation
FOR EACH ROW
  WHEN (new.automation_num IS NULL)
BEGIN
  SELECT zutomation_seq.NEXTVAL  INTO   :new.zAutomationid  FROM   dual;
  :new.automation_num := 'AUT'||LPAD(:new.zAutomationid,9,'0');
END;

当我使用hibernate构建它们时,第二个表引用了它,我得到了错误

Hibernate: insert into ZAUTOMATION (automation_num, description, region, requested, sector, title, zautomationid) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into ZAUTOMATION_PHASE (automationid, PHASE_NUM, ZPHASEID) values (?, ?, ?)
21:17:52.343 [http-nio-8080-exec-94] ERROR org.hibernate.util.JDBCExceptionReporter - ORA-02291: integrity constraint (PERFMGMT.ZAUTOMATION_PHASE_FK1) violated - parent key not found



package com.citi.clg.originations.strategic.entity;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "ZAUTOMATION")
//@SequenceGenerator(name="inputAutomationNumSequence",sequenceName="ZUTOMATION_SEQ",allocationSize=1)
public class AutomationEntity implements Serializable{
    

    private static final long serialVersionUID = -3657415674729533973L;
    
    @Id 
    @Column(name="zautomationid") 
//  @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="inputAutomationNumSequence")
    private long zautomationid;
    
    
    public void setZautomationid(long zautomationid) {
        this.zautomationid = zautomationid;
    }


    public void setAutomationNum(String automationNum) {
        this.automationNum = automationNum;
    }

    @Column(name="automation_num") 
//  @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="inputAutomationNumSequence")
    private String automationNum;
     
    
    public long getZautomationid() {
        return zautomationid;
    }


    public String getAutomationNum() {
        return automationNum;
    }

    /*
     * public void setAutomationNum(String automationNum) { this.automationNum =
     * automationNum; }
     */

    
    @Column(name = "title")
    private String title;
    
    @Column(name = "description")
    private String description;
    
    @Column(name = "requested")
    private String requested;
    
    @Column(name = "sector")
    private String sector;
    
    @Column(name = "region")
    private String region;
    

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getRequested() {
        return requested;
    }

    public void setRequested(String requested) {
        this.requested = requested;
    }

    public String getSector() {
        return sector;
    }

    public void setSector(String sector) {
        this.sector = sector;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }
    
    
    
    
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      
      @JoinColumn(name="automationid")
      private Set<AutomationPhaseEntity> resources;
     


    
      public Set<AutomationPhaseEntity> getResources() { return resources; }
      
      public void setResources(Set<AutomationPhaseEntity> resources) {
      this.resources = resources; }
     
    
    
    
    
    

}






package com.citi.clg.originations.strategic.entity;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "ZAUTOMATION_PHASE")
@SequenceGenerator(name="inputAutomationPHASESequence",sequenceName="ZUTOMATION_PHASE_SEQ",allocationSize=1)
public class AutomationPhaseEntity implements Serializable {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ZPHASEID")
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="inputAutomationPHASESequence")
    private long phaseID;
    public void setPhaseID( long phaseID) {
        this.phaseID=phaseID;
    }

    public long getPhaseID() {
        return phaseID;
    }

    
    
    @Column(name = "automationid")
    private long automationID;
    
    
    
    public long getAutomationID() {
        return automationID;
    }



    /*
     * public void setAutomationID(long automationID) { this.automationID =
     * automationID; }
     */


    @Column(name = "PHASE_NUM")
    String phaseNum;
    


    public String getPhaseNum() {
        return phaseNum;
    }



    public void setPhaseNum(String phaseNum) {
        this.phaseNum = phaseNum;
    }





    

}

谁能告诉我如何使用在触发器的子表中的父表中创建的 id 并使关系正常工作。所以数据保存在两个表中?

【问题讨论】:

  • Hibernate 命令是否从同一个数据库会话中执行?要么两个事务都需要在同一个数据库登录会话中发生,要么您必须在子表看到新键之前将更改提交到父表。
  • 是的,它们在同一个数据库会话中执行

标签: java oracle hibernate jpa triggers


【解决方案1】:

我认为您在一对多关系中将父母的 PK 映射到孩子的 FK 时出错了。

你的父类(AutomationEntity)必须是这样的:(从父类中删除@JoinColumn,放到定义了FK的子类中。)

@OneToMany(mappedBy="automationEntity",targetEntity=PolicyType.class,fetch=FetchType.LAZY, cascade = CascadeType.ALL)
private Set<AutomationPhaseEntity> resources;

您的子类 (AutomationPhaseEntity) 必须是这样的:

@Column(name="automation_entity_id")
private int automationEntityId; // FK that references to automationEntity PK

@ManyToOne(fetch = FetchType.EAGER )
@JoinColumn(name = "automation_entity_id", insertable=false, updatable=false);
private AutomationEntity automationEntity;

它会起作用,但无论如何,如果你想从父级获取 PK 并通过 sql 查询将其注入子级 FK,你可以使用 sql 过程,但在这种情况下,该过程应该通过 jdbc 连接的 CallableStatement 运行,但我认为你没有'不需要它,因为您使用休眠。你应该只纠正上面提到的实体。

【讨论】:

    猜你喜欢
    • 2016-03-12
    • 1970-01-01
    • 2020-07-19
    • 2018-04-18
    • 2014-06-12
    • 2018-09-09
    • 2018-05-23
    • 2018-11-13
    • 1970-01-01
    相关资源
    最近更新 更多