简体   繁体   中英

Composite id in Hibernate

Good evening folks.

I'm developing an application which uses Hibernate 4.x. I'm currently having problems while using a composite id in a class and, mapping a relation of another class to this one.

I've got a ParadaBus object, which represent a Bus Stop (in Spanish) and each one is identified by a integer and a String:

public class ParadaBus implements Serializable {

   private Integer id;
   private String concesionRecogida;

   ......

   @Override
   public int hashCode() {
       return this.id.hashCode() + this.concesionRecogida.hashCode();
   }

   @Override
   public String toString() {
        return "ParadaBus [" + id + " " + concesionRecogida + "]";
   }
}

On the other hand, I've got another class which holds a single instance of a ParadaBus object:

public class TrabajadorNombramiento implements Serializable {
    ...
   private ParadaBus suParada;
    ...
}

I wanna map this relation with an XML HBM file, and I guess I'm doing incorrectly

This is the .hbm.xml file for ParadaBus:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "Hibernate Mapping 3.0" 
    "http://hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>

<class name="com.ingartek.cavwebapp.model.ParadaBus" table="ParadasBus">

    <composite-id >
        <key-property name="id" type="integer">
            <column name="id" />    
        </key-property>

        <key-property name="concesionRecogida" type="string">
            <column name="concesion_recogida" />    
        </key-property>

    </composite-id>
    ...
</class>
</hibernate-mapping>

Up here, everything seems right. The following snippet code represents the hbm.xml for the other class, where the relation to ParadaBus should be mapped:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "Hibernate Mapping 3.0" 
    "http://hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>

    <class name="com.ingartek.cavwebapp.model.TrabajadorNombramiento" table="nombramientos">
        ...
        <many-to-one name="suParada" class="com.ingartek.cavwebapp.model.ParadaBus" cascade="none">
            <column name="id_parada" />
            <column name="concesion_recogida" />
        </many-to-one>
        ...
    </class>
</hibernate-mapping>

Am I doing something wrong?

Thanks for your help.

EDIT:

Eventually, I decided to use a UUID to identify each ParadaBus object/row.

the column name in the relationship isn't the same as the column name in the class mapping.

try: .hbm.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "Hibernate Mapping 3.0" 
    "http://hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>

<class name="com.ingartek.cavwebapp.model.ParadaBus" table="ParadasBus">

    <composite-id >
        <key-property name="id" type="integer">
            <column name="id_parada" />    
        </key-property>

        <key-property name="concesionRecogida" type="string">
            <column name="concesion_recogida" />    
        </key-property>

    </composite-id>
    ...
</class>
</hibernate-mapping>

hbm.xml for the other class:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "Hibernate Mapping 3.0" 
    "http://hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>

    <class name="com.ingartek.cavwebapp.model.TrabajadorNombramiento" table="nombramientos">
        ...
        <many-to-one name="suParada" class="com.ingartek.cavwebapp.model.ParadaBus" cascade="none">
            <column name="id_parada" />
            <column name="concesion_recogida" />
        </many-to-one>
        ...
    </class>
</hibernate-mapping>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM