Hibernate配置subclass
程序员文章站
2022-05-10 10:43:01
...
在配置subclass的时候出现了
写道
[main] ERROR org.hibernate.util.XMLHelper - Error parsing XML: XML InputStream(26)
The content of element type "class" must match "(meta*,subselect?,cache?
,synchronize*,(id|composite-id),discriminator?,(version|timestamp)?,
(property|many-to-one|one-to-one|component|dynamic-component
|properties|any|map|set|list|bag|idbag|array|primitive-array|query-list)*,
((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?
,sql-update?,sql-delete?,filter*)"
The content of element type "class" must match "(meta*,subselect?,cache?
,synchronize*,(id|composite-id),discriminator?,(version|timestamp)?,
(property|many-to-one|one-to-one|component|dynamic-component
|properties|any|map|set|list|bag|idbag|array|primitive-array|query-list)*,
((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?
,sql-update?,sql-delete?,filter*)"
我的xx.hbm.xml配置是这样的:
写道
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.summit.pojo.Animal" table="animal">
<id column="id" type="long" name="id">
<generator class="uuid"></generator>
</id>
<property name="name" column="name" type="string"></property>
<discriminator type="string">
<column name="animalType"></column>
</discriminator>
<subclass name="com.summit.pojo.Dog" discriminator-value="dog">
<property name="jump" type="string"></property>
</subclass>
<subclass name="com.summit.pojo.Kitchen" discriminator-value="kitchen">
<property name="fly" column="fly" type="string"></property>
</subclass>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.summit.pojo.Animal" table="animal">
<id column="id" type="long" name="id">
<generator class="uuid"></generator>
</id>
<property name="name" column="name" type="string"></property>
<discriminator type="string">
<column name="animalType"></column>
</discriminator>
<subclass name="com.summit.pojo.Dog" discriminator-value="dog">
<property name="jump" type="string"></property>
</subclass>
<subclass name="com.summit.pojo.Kitchen" discriminator-value="kitchen">
<property name="fly" column="fly" type="string"></property>
</subclass>
</class>
</hibernate-mapping>
当我把discriminator删除掉的就正确了,可是又不符合定义子类映射的要求,所以 从google上搜索,包括查看hibernate-mapping-3.0.dtd,可以看到是到class的这一段描述:
写道
<!ELEMENT class (
meta*,
subselect?,
cache?,
synchronize*,
comment?,
tuplizer*,
(id|composite-id),
discriminator?,
natural-id?,
(version|timestamp)?,
(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,
((join*,subclass*)|joined-subclass*|union-subclass*),
loader?,sql-insert?,sql-update?,sql-delete?,
filter*,
resultset*,
(query|sql-query)*
)>
meta*,
subselect?,
cache?,
synchronize*,
comment?,
tuplizer*,
(id|composite-id),
discriminator?,
natural-id?,
(version|timestamp)?,
(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,
((join*,subclass*)|joined-subclass*|union-subclass*),
loader?,sql-insert?,sql-update?,sql-delete?,
filter*,
resultset*,
(query|sql-query)*
)>
可以看到discriminator必须要在id的后面定义,所以把discriminator改到id后面就OK了,像这样:
写道
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.summit.pojo.Animal" table="animal">
<id column="id" type="long" name="id">
<generator class="uuid"></generator>
</id>
<discriminator type="string">
<column name="animalType"></column>
</discriminator>
<property name="name" column="name" type="string"></property>
<subclass name="com.summit.pojo.Dog" discriminator-value="dog">
<property name="jump" type="string"></property>
</subclass>
<subclass name="com.summit.pojo.Kitchen" discriminator-value="kitchen">
<property name="fly" column="fly" type="string"></property>
</subclass>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.summit.pojo.Animal" table="animal">
<id column="id" type="long" name="id">
<generator class="uuid"></generator>
</id>
<discriminator type="string">
<column name="animalType"></column>
</discriminator>
<property name="name" column="name" type="string"></property>
<subclass name="com.summit.pojo.Dog" discriminator-value="dog">
<property name="jump" type="string"></property>
</subclass>
<subclass name="com.summit.pojo.Kitchen" discriminator-value="kitchen">
<property name="fly" column="fly" type="string"></property>
</subclass>
</class>
</hibernate-mapping>
上一篇: Java EE 生成网站表单的验证码