hibernate – Mapear composite IDS

Question:

I have a problem mapping a class in Hibernate 2.

I have a table "CONF_PGTO_CLIENTE" which has columns (SHOPID, CLIENT_ID, PMT_TYPE), 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…


@Jonathans Hudler

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

the hql I run 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 hql query, a list of parameters, and a list that informs the type of items of the parameters given in parameter 2.

No error messages or anything like that.

@Topic

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

Answer:

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

Scroll to Top