[英]Hibernate Foreign Key Constraint issue for many to one
First, let me say that I know this is yet "another" Hibernate issue with foreign keys. 首先,让我说,我知道这是外键的另一个“休眠”问题。 However, none of the solutions I found on here seem to work for me.
但是,我在这里找到的所有解决方案似乎都不适合我。 So here I am, looking for help.
所以我在这里寻求帮助。
Database 数据库
This is the parent table 这是父表
CREATE TABLE FORM (
FORM_ID NUMBER,
NAME VARCHAR2(40),
CONSTRAINT form_pk PRIMARY KEY (form_id));
And this is the child table 这是子表
CREATE TABLE REJECTION (
REJECTION_ID NUMBER,
REASON VARCHAR2(2000),
FORM_ID NUMBER,
CONSTRAINT rej_pk PRIMARY_KEY (rejection_id),
CONSTRAINT rej_fk1 FOREIGN KEY (form_id) REFERENCE form(form_id));
Each of these table have a trigger that populates the primary key before insertion 这些表中的每一个都有一个触发器,该触发器在插入之前填充主键
Entity Objects 实体对象
This is the Entity Object for the parent: 这是父级的实体对象:
@Entity
@Table(name = "FORM")
public class Form implements Serializable {
@Id
@Column(name = "FORM_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "G1")
@SequenceGenerator(name = "G1", sequenceName = "FORM_SEQ")
// This uses a trigger to generate the ID
private Integer formId;
@Column(name="NAME")
private String name;
@OneToMany(mappedBy = "form")
private Set<Rejection> rejections;
// getters and setters
}
And here is the Entity Object for the child: 这是孩子的实体对象:
@Entity
@Table(name = "REJECTION")
public class Rejection implements Serializable {
@Id
@Column(name = "REJECTION_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "G2")
@SequenceGenerator(name = "G2", sequenceName = "REJECTION_SEQ")
// This uses a trigger to generate the ID
private Integer rejectionId;
@Column(name="REASON")
private String reason;
@ManyToOne
@JoinColumn(name="FORM_ID")
private Form form;
// getters and setters
}
hibernate.cfg.xml 的hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@someConnection</property>
<property name="hibernate.connection.username">pancakes</property>
<property name="hibernate.connection.password">syrup</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">false</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">3000</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.preferredTestQuery">SELECT 1 from DUAL</property>
<mapping class="com.entity.Form"></mapping>
<mapping class="com.entity.Rejection"></mapping>
</session-factory>
</hibernate-configuration>
The Code 编码
And finally, here is the code snippet I am using: 最后,这是我正在使用的代码段:
session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Form form = new Form();
form.setName("Test");
session.save(form);
System.out.println("Form ID: " + form.getFormId());
Rejections rej = new Rejection();
rej.setReason("Some Reason");
rej.setForm(form);
session.save(rej);
System.out.println("Rejection ID: " + rej.getRejectionId());
session.getTransaction().commit();
session.close();
The Error 错误
The error I am getting is: 我得到的错误是:
java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (USER.REJ_FK1) violated - parent key not found
Both of the System.outs actually return a value. 两个System.outs实际上都返回一个值。
What I am thinking is that there is an issue with the trigger generating the IDs. 我在想的是生成ID的触发器存在问题。
I appreciate ANY help or direction on this. 感谢您对此的任何帮助或指导。 Thank you!
谢谢!
I figured this out. 我想通了。 The issue was with the triggers and the SequenceGenerators on the IDS.
问题出在IDS上的触发器和SequenceGenerators上。 It was essentially generating 2 IDS... one from the trigger and one from Hibernate.
它本质上是生成2个IDS ...一个来自触发器,另一个来自Hibernate。 Hibernate was trying to use the one from the Trigger when it was needing the one from Hibernate.
当Hibernate需要Hibernate中的一个时,它试图使用Trigger中的一个。
I just disabled the triggers and it worked fine :-) 我只是禁用了触发器,但效果很好:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.