Mapping composite IDS

3

I have a problem mapping a class in Hibernate 2.

I have a table "CONF_PGTO_CLIENTE" which has columns (LOOP_ID, CLIENT_ID, TYPE_PGTO), all 3 columns are primary keys and I need to map this in ".hbm.xml".

My class is:

/**
 * Configuração de quais clientes podem utilizar determinada forma de pagamento.
 *
 * @hibernate.class
 *      table="CONF_CLIENTE_PGTO"
 */
public class ClienteFormaPgto {

    private Long idLoja;
    private int tipoPgto;

    private Set clientesPermitidos;

    /**
     * @hibernate.key-property 
     *  column="ID_LOJA"
     *  not-null="true"
     *  type="java.lang.Long"
     */
    public Long getIdLoja() {
        return idLoja;
    }

    public void setIdLoja(Long idLoja) {
        this.idLoja = idLoja;
    }

    /**
     * @hibernate.key-property 
     *  column="TIPO_PGTO"
     *  not-null="true"
     *  type="int"
     */
    public int getTipoPgto() {
        return tipoPgto;
    }

    public void setTipoPgto(int tipoPgto) {
        this.tipoPgto = tipoPgto;
    }

    /**
     * 
     * @hibernate.set
     *  table="CLIENTE"
     *  lazy="false"
     *  cascade="none"
     * @hibernate.collection-key
     *  column="ID_CLIENTE"
     * @hibernate.collection-many-to-many
     *  column="ID"
     *  class="net.alforria.b2c.modelo.Cliente"
     */
    public Set getClientesPermitidos() {
        return clientesPermitidos;
    }

    public void setClientesPermitidos(Set clientesPermitidos) {
        this.clientesPermitidos = clientesPermitidos;
    }

}

and my .hbm.xml looks like this:

<hibernate-mapping
>
    <class
        name="net.alforria.b2c.modelo.ClienteFormaPgto"
        table="CONF_CLIENTE_PGTO"
    >

        <composite-id>
            <key-property name="idLoja" column="ID_LOJA" type="java.lang.Long" />
            <key-property name="tipoPgto" column="TIPO_PGTO" type="int" />
        </composite-id>

        <set
            name="clientesPermitidos"
            table="CLIENTE"
            lazy="false"
            cascade="none"
            sort="unsorted"
        >

            <key
                column="ID_CLIENTE"
            >
            </key>

            <many-to-many
                class="net.alforria.b2c.modelo.Cliente"
                column="ID"
                outer-join="auto"
             />

        </set>

    </class>

</hibernate-mapping>

I've tried several variations of this mapping, but nothing I do seems to work ...

@ Jonathan Hudler

So, what happens is that when I run a find that I have from my DAO, the result always comes empty ...

a hql that I execute is:

public List findClientesByFormaPgto(Long idLoja, int tipoPgto) {
    List list = findByQuery(
            " select ccp.idLoja, ccp.idCliente, c.nome " +
            " from " + ClienteFormaPgto.class.getName() + " as ccp " +
            " , " + Cliente.class.getName() + " as c" +
            " where ccp.idLoja = ? " +
            " and ccp.tipoPgto = ? " +
            " and ccp.idCliente = c.id",
            new Object[] {idLoja, tipoPgto},
            new Type[] {Hibernate.LONG, Hibernate.INTEGER}
    );
    return list;

}

The findByQuery () function is implemented from another BaseDAO class. And it receives as parameters a query hql, a list of parameters, and a list that informs the typing of the items of the parametres given in parameter 2.

There are no error messages or anything like this.

@Topic

As a temporary solution, I added an id in the bank and mapping, so I bypassed the composite-id problem ... But it is not the ideal solution for my case, which would be with the 3 columns of the bank as primary keys ...

    
asked by anonymous 22.12.2014 / 12:31

1 answer

0

Change the 2 mappings to the same type. And implement the equal and hashcode methods.

    
22.12.2014 / 19:54