Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Hibernate中将一对多映射到子类_Hibernate_Hibernate Mapping_Hibernate Onetomany_Table Per Class - Fatal编程技术网

在Hibernate中将一对多映射到子类

在Hibernate中将一对多映射到子类,hibernate,hibernate-mapping,hibernate-onetomany,table-per-class,Hibernate,Hibernate Mapping,Hibernate Onetomany,Table Per Class,我有以下课程: public class Step { private int id; private String name; private List<CreatedVariable> createdVariables; private List<CaptureVariable> captureVariables; } public abstract class Variable implements Serializable {

我有以下课程:

public class Step {
    private int id;
    private String name;
    private List<CreatedVariable> createdVariables;
    private List<CaptureVariable> captureVariables;
}

public abstract class Variable implements Serializable {
    protected int id;
    protected String name;
}

public abstract class CaptureVariable extends Variable {
}

public abstract class CreatedVariable extends Variable {
}

public class FixedValueVariable extends CreatedVariable {
    private String value;
}

public class RegExVariable extends CaptureVariable {
    private String regex;
}
xml映射文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field">
    <class name="br.com.edeploy.watcher.step.Step" table="STEP">
        <id name="id" column="STEP_ID" type="int">
            <generator class="native"/>
        </id>

        <property name="name" column="STEP_NAME" not-null="true"/>

        <list name="createdVariables" table="VARIABLE" cascade="all" inverse="false">
            <key column="VARI_FK_STEP_ID" not-null="true"/>
            <list-index column="VARI_ORDER"/>

            <one-to-many class="br.com.edeploy.watcher.variable.Variable" />
        </list>
    </class>

    <class name="br.com.edeploy.watcher.variable.Variable" table="VARIABLE">
        <id name="id" column="VARI_ID" type="int">
            <generator class="native"/>
        </id>

        <property name="name" column="VARI_NAME" type="string"/>

        <joined-subclass name="br.com.edeploy.watcher.variable.CaptureVariable" table="CAPTURED_VARIABLE">
            <key column="CAVA_FK_VARI_ID"/>

            <joined-subclass name="br.com.edeploy.watcher.variable.RegExVariable" table="REGEX_VARIABLE">
                <key column="RGVA_FK_VARI_ID"/>
                <property name="regex" column="RGVA_REGEX" type="string"/>
            </joined-subclass>
        </joined-subclass>

        <joined-subclass name="br.com.edeploy.watcher.variable.CreatedVariable" table="CREATED_VARIABLE">
            <key column="CRVA_FK_VARI_ID"/>

            <joined-subclass name="br.com.edeploy.watcher.variable.FixedValueVariable" table="FIXED_VALUE_VARIABLE">
                <key column="FVVA_FK_VARI_ID"/>
                <property name="value" column="FVVA_VALUE" type="string"/>
            </joined-subclass>
        </joined-subclass>
    </class>
</hibernate-mapping>
但是hibernate正在生成要插入变量表中的以下SQL:

Hibernate: 
insert 
into
    VARIABLE
    (VARI_ID, VARI_NAME) 
values
    (default, ?)
13:36:56.209 [main] DEBUG o.h.e.jdbc.spi.SqlExceptionHelper - could not execute statement [n/a]
java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10390 table: VARIABLE column: VARI_FK_STEP_ID
如果我只使用变量类创建了一个列表,那么我就能够插入,并且hibernate会正确地生成所有SQL,以便在所有5个表中插入

是否可以将列表(一对多)映射到Hibernate中的子类?我是否缺少一些额外的配置

  • 为子类创建接口,并让子类实现它们
  • 将@Entity@表注释添加到接口本身
  • 向接口的get方法添加@Column/@OneToMany etc注释
  • 并使用该界面创建集合/列表/行李。 这对我有用
  • Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
        SessionFactory sessionFactory = configuration
                .buildSessionFactory(serviceRegistry);
    
        Session session = sessionFactory.openSession();
        session.beginTransaction();
    
        Step step = new Step("step1");
    
        RegExVariable regExVariable = new RegExVariable("regVariable", ".");
        step.getCaptureVariables().add(regExVariable);
    
        FixedValueVariable fixedValueVariable = new FixedValueVariable("fixedValueVariable", "value");
        step.getCreatedVariables().add(fixedValueVariable);
    
    
        session.save(step);
        session.getTransaction().commit();
        session.close();
    
        sessionFactory.close();
    
    Hibernate: 
    insert 
    into
        VARIABLE
        (VARI_ID, VARI_NAME) 
    values
        (default, ?)
    13:36:56.209 [main] DEBUG o.h.e.jdbc.spi.SqlExceptionHelper - could not execute statement [n/a]
    java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10390 table: VARIABLE column: VARI_FK_STEP_ID