Question

I have some doubts about how to make a hbm.xml file for a mapping in Hibernate. The problem comes when mapping a Java object which has fields in several tables.

The custom framework we are using requires to have a hbm.xml file for each object, but in this case its fields are in three tables, so I want to make a native SQL query (another requirement from above) and move the field values in the result over the new object described in the Hibernate mapping file.

My hibernate mapping file is something like:

<hibernate-mapping>
<class name="com.mycompany.myproject.model.scommons.companhiaalmacen.CompanhiaAlmacen" table="COMPANHIA_ALMACEN">
    <id name="companyWarehouseId" type="java.lang.Integer" access="field">
        <column name="ID_EMPRESA_NAVE" />
        <generator class="assigned" />
    </id>
    <property name="companyId" type="java.lang.Integer">
        <column name="ID_EMPRESA" />
    </property>
    <property name="warehouseDCId" type="java.lang.Integer">
        <column name="ID_CENTRO_DISTRIBUCION_NAVE" />
    </property>

    <!-- HERE: This attribute NOMBRE_COMERCIAL belongs not only to another table (EMPRESA) -->
    <!-- but also to another schema (GENERAL) as well -->
    <property name="name" type="java.lang.String">
        <column name="NOMBRE_COMERCIAL" />
    </property>

    <property name="creationDate" type="java.util.Date">
        <column name="FECHA_ALTA" />
    </property>
    <property name="creationUser" type="java.lang.String">
        <column name="USUARIO_ALTA" />
    </property>
</class>
</hibernate-mapping>

I don't know how to make this without making a oneToMany or ManyToOne. I don't want Hibernate to retrieve the object and its mapped entities because I want to get this from my own db2 query.

Anyone knows how to do it or where can I look for this, please? (I already have looked for the SQLQuery in the Hibernate API, is just the mapping which I don't get).

Thank you in advance.

Was it helpful?

Solution

You can override any CRUD statement as required in Hibernate. See section 18.3 and 18.4 at the below:

http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/querysql.html#querysql-load

It is also possible to map an Entity to multiple tables and to have tables in multiple schemas via the schema attribute of the table definition so this may possibly be a better approach.

See section 2.2.7 below:

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e2235

and the following showing secondary table can be in another schema:

http://docs.oracle.com/javaee/5/api/javax/persistence/SecondaryTable.html

There will be XML equivalents for these annotations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top