【问题标题】:integrity constraint violated - parent key not found - OneToOne and ManyToOne违反完整性约束 - 未找到父键 - OneToOne 和 ManyToOne
【发布时间】:2014-01-21 16:14:21
【问题描述】:

在将实体插入数据库时​​遇到问题。我正在使用 Hibernate JPA。

我有 Reminder 和 ReminderAction 表,每个 Reminder 都将保存 ReminderAction 的当前状态(即 OneToOne),并且每个 Reminder 的交易结束都会有许多 ReminderActions(具有不同的状态)。

DB Table columns are

REMINDER --> REMINDER_ID (PK), CURRENT_ACTION_ID (FK)...
REMINDER_ACTION --> REMINDER_ACTION_ID (PK), REMINDER_ID (FK)...


Reminder Entity
---------------------

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "REMINDER_SEQ_STORE")
@Column(name = "REMINDER_ID", unique = true, nullable = false, precision = 22, scale = 0)
public Integer getReminderId() {
return reminderId;
}



@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, targetEntity = ReminderAction.class)
@JoinColumn(name = "CURRENT_ACTION_ID")
public ReminderAction getCurrentAction() {
return currentAction;
}


ReminderAction Entity
---------------------

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="REMINDER_ACTION_SEQ_STORE")
@Column(name = "REMINDER_ACTION_ID", unique = true, nullable = false)
public Integer getReminderActionId() {
return reminderActionId;
}


@ManyToOne(cascade=CascadeType.ALL,targetEntity=Reminder.class)
@JoinColumn(name="REMINDER_ID")
public Reminder getReminder() {
return reminder;
}


DAO
---

public void saveOrUpdate(final E transientObject) {
getCurrentSession().saveOrUpdate(transientObject);
getCurrentSession().flush();
}

在 saveOrUpdate 期间,hibernate 从 DB 中获取 Reminder 和 ReminderAction 的序列

Hibernate: select REMINDER_SEQ.nextval from dual
Hibernate: select REMINDER_ACTION_SEQ.nextval from dual

但在 flush() 期间,hibernate 尝试首先插入 ReminderAction,并且由于 ReminderAction 中的 ReminderID 为空,它抛出 ORA-02291:违反完整性约束 (REMINDER_ACTION_FK) - 未找到父键

由于数据库中的触发器,请参阅下面的线程和问题。 但我在 DB 中没有任何 Reminder 和 Reminder_Action 表的触发器。

  1. JPA Bidirectional One to Many Foreign Key Issues

  2. Integrity constraint violated just when I commit the transaction

  3. Hibernate: Insert issue - Parent Key not found

如何让hibernate先插入Reminder再插入ReminderAction?

更新:

我尝试从 Reminder 和 ReminderAction 中删除 SequenceGenerator 并在 saveOrUpdate 期间手动设置 ID 并以同样的异常结束

Reminder rem = (Reminder)transientObject;
rem.getCurrentAction().setReminderId(5);
rem.getCurrentAction().setReminderActionId(5);
rem.setReminderId(5);

getCurrentSession().saveOrUpdate(transientObject);
getCurrentSession().flush();



Hibernate: insert into REMINDER_ACTION (REMINDER_ID, REMINDER_ACTION_ID) values (?, ?)
binding parameter [1] as [INTEGER] - 5
binding parameter [2] as [INTEGER] - 5
SQL Error: 2291, SQLState: 23000
ORA-02291: integrity constraint (REMINDER_ACTION_FK) violated - parent key not found

非常感谢任何帮助。谢谢。

【问题讨论】:

    标签: hibernate hibernate-mapping hibernate-annotations


    【解决方案1】:

    我将您的答案从问题中提取出来,并将其放入专门的答案中:

    将 Reminder 中的 ReminderAction 设为 nullable=true,将 ReminderAction 中的 Reminder 设为 nullable=false

    Reminder Entity
    ---------------
    
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, targetEntity = ReminderAction.class)
    @JoinColumn(name = "CURRENT_ACTION_ID", nullable=true)
    public ReminderAction getCurrentAction() {
    return currentAction;
    }
    
    ReminderAction Entity
    ---------------------
    @ManyToOne(cascade=CascadeType.ALL,targetEntity=Reminder.class)
    @JoinColumn(name="REMINDER_ID", nullable=false)
    public Reminder getReminder() {
    return reminder;
    }
    

    执行此休眠将首先插入 Reminder,然后是 ReminderAction。谢谢

    【讨论】:

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