简体   繁体   English

Hibernate中的复合ID

[英]Composite id in Hibernate

Good evening folks. 大家晚上好。

I'm developing an application which uses Hibernate 4.x. 我正在开发一个使用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. 我目前在类中使用复合ID并将另一个类的关系映射到该类时遇到问题。

I've got a ParadaBus object, which represent a Bus Stop (in Spanish) and each one is identified by a integer and a String: 我有一个ParadaBus对象,它代表一个公共汽车站(西班牙语),并且每个对象都由一个整数和一个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: 另一方面,我还有另一个类,它包含一个ParadaBus对象的单个实例:

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 我想将此关系与XML HBM文件映射,我想我做错了

This is the .hbm.xml file for ParadaBus: 这是ParadaBus的.hbm.xml文件:

<?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: 以下代码段代表另一个类的hbm.xml,其中应映射与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.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. 最终,我决定使用UUID来标识每个ParadaBus对象/行。

the column name in the relationship isn't the same as the column name in the class mapping. 关系中的列名与类映射中的列名不同。

try: .hbm.xml file : 尝试:.hbm.xml文件:

<?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: hbm.xml用于另一个类:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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