是的,您可以通过 XML 执行此操作,我没有遇到任何问题。这是这个项目的一个例子(因为我已经很久没有接触那个项目了,我没有清理代码来展示最小的工作示例):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.jtalks.common.model.entity.Property" table="PROPERTIES">
<id column="PROPERTY_ID" name="id" unsaved-value="0">
<generator class="native" />
</id>
<property column="UUID" name="uuid" not-null="true" unique="true" />
<property column="NAME" name="name" not-null="true" unique="false" />
<property column="VALUE" name="value" type="text" not-null="false" unique="false" />
<property column="VALIDATION_RULE" name="validationRule" not-null="false" unique="false" />
</class>
</hibernate-mapping>
和:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.jtalks.common.model.entity.Property" table="DEFAULT_PROPERTIES" entity-name="DefaultProperty">
<id column="PROPERTY_ID" name="id" unsaved-value="0">
<generator class="native" />
</id>
<property column="UUID" name="uuid" not-null="true" unique="true" />
<property column="NAME" name="name" not-null="true" unique="false" />
<property column="VALUE" name="value" not-null="false" unique="false" />
<property column="VALIDATION_RULE" name="validationRule" not-null="false" unique="false" />
</class>
</hibernate-mapping>
这是用法#1的示例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.jtalks.common.model.entity.Component" table="COMPONENTS">
<id column="CMP_ID" name="id" unsaved-value="0">
<generator class="native" />
</id>
<discriminator column="COMPONENT_TYPE" force="false" insert="false" />
<property column="UUID" name="uuid" not-null="true" unique="true" />
<property column="NAME" name="name" not-null="true" unique="true" />
<property column="DESCRIPTION" name="description" />
<bag name="properties" cascade="all-delete-orphan" inverse="false" lazy="false">
<cache usage="nonstrict-read-write" region="org.jtalks.EHCOMMON"/>
<key column="CMP_ID" foreign-key="FK_COMPONENT" />
<one-to-many class="org.jtalks.common.model.entity.Property" />
</bag>
<property name="componentType" column="COMPONENT_TYPE" unique="true">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">org.jtalks.common.model.entity.ComponentType</param>
<!-- 12 means 'VARCHAR', see java.sql.Types.VARCHAR -->
<param name="type">12</param>
</type>
</property>
<!-- discriminator - ComponentType.FORUM -->
<subclass name="org.jtalks.poulpe.model.entity.Jcommune" discriminator-value="FORUM">
<list name="sections" cascade="all-delete-orphan" inverse="false" lazy="false">
<cache usage="nonstrict-read-write" />
<key column="COMPONENT_ID" foreign-key="FK_JCOMMUNE" />
<list-index column="POSITION" />
<one-to-many class="org.jtalks.poulpe.model.entity.PoulpeSection" />
</list>
</subclass>
<!-- discriminator - ComponentType.ADMIN_PANEL -->
<subclass name="org.jtalks.poulpe.model.entity.Poulpe" discriminator-value="ADMIN_PANEL" />
<subclass name="org.jtalks.common.model.entity.Component" entity-name="Antarticle" discriminator-value="ARTICLE" />
</class>
</hibernate-mapping>
这里分别是#2:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.jtalks.poulpe.model.entity.ComponentBase" table="BASE_COMPONENTS">
<id column="COMPONENT_TYPE" name="componentType">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">org.jtalks.common.model.entity.ComponentType</param>
<!-- 12 means 'VARCHAR', see java.sql.Types.VARCHAR -->
<param name="type">12</param>
</type>
</id>
<set name="defaultProperties" cascade="all" inverse="false" lazy="false">
<cache usage="read-only" />
<key column="BASE_COMPONENT_TYPE" foreign-key="COMPONENT_TYPE" />
<one-to-many class="org.jtalks.common.model.entity.Property" entity-name="DefaultProperty" />
</set>
</class>
</hibernate-mapping>
请注意,您不能对注释做同样的事情,这就是 XML 更灵活的地方。