Hibernate外键约束问题多对一

时间:2014-09-18 18:59:15

标签: java oracle hibernate

首先,让我说我知道这是外键的“另一个”Hibernate问题。但是,我在这里找到的解决方案似乎都不适合我。我在这里,寻求帮助。

数据库
这是父表

CREATE TABLE FORM (
    FORM_ID    NUMBER,
    NAME       VARCHAR2(40),
CONSTRAINT form_pk PRIMARY KEY (form_id));

这是子表

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));

这些表中的每一个都有一个触发器,在插入之前填充主键

实体对象
这是父级的实体对象:

@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
}

这是孩子的实体对象:

@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-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>

代码
最后,这是我正在使用的代码片段:

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();

错误
我得到的错误是:
java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (USER.REJ_FK1) violated - parent key not found

两个System.out实际上都返回一个值。

我在想的是生成ID的触发器存在问题。

我很欣赏这方面的任何帮助或指示。谢谢!

1 个答案:

答案 0 :(得分:0)

我想出来了。问题在于IDS上的触发器和SequenceGenerators。它本质上产生了2个IDS ......一个来自触发器,一个来自Hibernate。当Hibernate需要来自Hibernate的那个时,它试图使用Trigger中的那个。

我刚刚禁用了触发器,它工作正常: - )