4

是否可以使用entity-namein 属性class来设置实体并引用它?我想这样做是因为我想映射到具有相同实体类的多个表。

表 1 和 adble 2 具有相同的架构

@Entity
public class POJO{
    @Id
    @Column(name="column1")
    private String column1;

    @Column(name="column2")
   private String column2;

   //getters and setters

}



<hibernate mapping>
    <class name="package.POJO" entiy-name="EntityTable1" table="table1">
        <id>.....</id>
            <property>....</property>
            <property>....</property>
     </class>

     <class name="package.POJO" entiy-name="EntityTable2" table="table2">
        <id>.....</id>
            <property>....</property>
            <property>....</property>
     </class>
</hibernate mapping>


Session s = SessionFactory.openSession();
List table1List = s.createQuery("FROM EntityTable1").list();

List table1List = s.createQuery("FROM EntityTable2").list();

我在Hibernate Documentation中读到这只是在实验阶段。有没有人使用过这种方法并工作?

4

1 回答 1

3

是的,您可以通过 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 更灵活的地方。

于 2012-12-13T19:33:53.900 回答