Search code examples
javahibernatesessionormentity

Hibernate mapping class entity-name


Is it possible to use the entity-name attribute in class to set an entity and to reference it? I want to do this because I want to map to multiple tables with the same entity class.

Table 1 and adble 2 have the same schema

@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();

I read in the Hibernate Documentation that this is only in the experimental stage. Has anyone used this method and work?


Solution

  • Yes, you can do this via XML, I didn't experience any problems with that. Here's an example from this project (since it's being a long time since I touched that project, I'm not cleaning up the code to show the minimal working example):

    <?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>
    

    And:

    <?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>
    

    Here's an example of usage #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>
    

    And here's #2 respectively:

    <?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>
    

    Note, that you can't do that same with annotations, that's where XML is more flexible.