integrity constraint violated - parent key not found - OneToOne and ManyToOne
I am pulling your answer out of the question, and putting it into a dedicated answer:
Make ReminderAction in Reminder as nullable=true and Reminder in ReminderAction as 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;
}
Doing this hibernate will insert Reminder first then ReminderAction. Thanks
Comments
-
user3133577 almost 2 years
Facing issue inserting an entity into database. I am using Hibernate JPA.
I have tables Reminder and ReminderAction and Each Reminder will hold current status of ReminderAction (ie. OneToOne) and end of transaction each Reminder will have many ReminderActions (with different status).
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(); }
During saveOrUpdate, hibernate gets the sequence for Reminder and ReminderAction from DB
Hibernate: select REMINDER_SEQ.nextval from dual Hibernate: select REMINDER_ACTION_SEQ.nextval from dual
But during flush(), hibernate trying to insert ReminderAction first and due to ReminderID in ReminderAction is null its throwing ORA-02291: integrity constraint (REMINDER_ACTION_FK) violated - parent key not found
Refered below threads and problem because of trigger in DB. But I dont have any triggers for Reminder and Reminder_Action table in DB.
How can I ask hibernate to insert Reminder first then ReminderAction?
UPDATE:
I tried by removing SequenceGenerator from Reminder and ReminderAction and manually set the IDs during saveOrUpdate and end up with same exception
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
Any help is highly appreciated. Thanks.