Map

Asked

Viewed 135 times

3

I’m having a problem mapping a class in Hibernate 2.

I have a table "CONF_PGTO_CLIENTE" that has columns (ID_LOJA, ID_CLIENTE, TIPO_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 is 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 executing a find q have of my DAO, the result always comes empty...

the hql q 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;

}

findByQuery() 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 parameters given in parameter 2.

No error messages or anything like that.

@Topic

As a temporary solution, I added in the bank and in the mapping, an id, so I answered the "Composite-id" problem... However it is not the ideal solution for my case, which would be with the 3 columns of the bank as Primary Keys...

  • The annotations are inside the comment blocks, that’s correct?

  • Yes, this is the format that Hibernate 2 works with...

  • When you quote "but nothing I do seems to work": what actually happens? Error message? Incorrectly generated SQL query? Would you be so kind as to provide more information in this regard?

1 answer

0

Change the two maps to the same type. And implement the Equal and hashcode methods.

  • 1

    You can develop the answer more?

  • Actually, his answer was a little vague... As for putting the maps of the same type, I believe I can not do it effectively, it would make me create unnecessary validations. As for implementing these methods, what do you mean? The problem could be in the methods and not in the mapping?

  • Declares both as Long.

  • http://answall.com/questions/11108/qual-a-import%C3%A2ncia-de-implement-o-m%C3%A9todo-hashcode-in-java

  • In the Hibernate 2 documentation it indicates that Voce should implement these methods (equals and hashcode) if it uses composite keys.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.