最开始的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping package="subselect">
<!-- mutable指定Container的只读属性,只能从select中生成 -->
<class name="Container" mutable="false">
<id name="id" unsaved-value="0">
<generator class="native"></generator>
</id>
<property name="size" column="size"></property>
<property name="name" column="name"></property>
<property name="description" column="description"></property>
<property name="xparam" column="xparam"></property>
<property name="yparam" column="yparam"></property>
<property name="zparam" column="zparam"></property>
<subselect>
select id,size,name,description,diameter as xparam,diameter as yparam,height as zparam from Bottle
union
select id,size,name,description,width as xparam,length as yparam,height as zparam from Box
</subselect>
<synchronize table="Box"/>
<synchronize table="Bottle"/>
</class>
</hibernate-mapping>
可不知道为什么,class标签旁边总有个小红叉叉,但是在IE中又能正常显示这个xml
最后发现,原来是标签的前后顺序有问题,<synchronize>和<subselect>标签要放在<id>标签前面
并且<subselect>标签要在<synchronize>标签前面
修改后的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping package="subselect">
<!-- mutable指定Container的只读属性,只能从select中生成 -->
<class name="Container" mutable="false">
<subselect>
select id,size,name,description,diameter as xparam,diameter as yparam,height as zparam from Bottle
union
select id,size,name,description,width as xparam,length as yparam,height as zparam from Box
</subselect>
<synchronize table="Box"/>
<synchronize table="Bottle"/>
<id name="id" unsaved-value="0">
<generator class="native"></generator>
</id>
<property name="size" column="size"></property>
<property name="name" column="name"></property>
<property name="description" column="description"></property>
<property name="xparam" column="xparam"></property>
<property name="yparam" column="yparam"></property>
<property name="zparam" column="zparam"></property>
</class>
</hibernate-mapping>
本文解决了Hibernate XML映射文件中因元素顺序不当导致的问题,调整了<synchronize>和<subselect>的位置,并确保<subselect>位于<synchronize>之前且二者都在<id>标签前。
8955

被折叠的 条评论
为什么被折叠?



