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?
– Renan Gomes
Yes, this is the format that Hibernate 2 works with...
– Vitor Carvalho
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?
– Jônatas Hudler