在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中的子类?我是否缺少一些额外的配置
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