面对将实体插入数据库的问题。我正在使用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为null,因此违反了 ORA-02291:完整性约束(REMINDER_ACTION_FK) - 未找到父键
因为DB中的触发器而引用了线程和问题。 但是我在DB中没有Reminder和Reminder_Action表的任何触发器。
Integrity constraint violated just when I commit the transaction
如何让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
非常感谢任何帮助。感谢。
答案 0 :(得分:0)
我正在回答您的问题,并将其置于一个专门的答案中:
将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。感谢