Hibernate filter manyToMany with extra columns
<p>I have a problem filtering a manytomany relation. My classes are developed like the next example:</p>

<p><a href="http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/" rel="nofollow">http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/</a></p>

<p>"i want the 'Pagadoras' of an specific 'Locutorio'. The 'Pagadoras' must have 'operativa' iqual to 1 and the field 'visible' of the relationship between 'Locutorio' and 'Pagador' must be iqual to 1"</p>

<p>Furthermore, i have to check that 'Pagador.id' exists in other entity 'Sucursal' that have a relation ManyToOne with 'Pagador'.</p>

<p>I don't know which is the best way to get this information. I have tried: filters, criterias and directs queries. But its impossible.</p>

<p>If I do with direct query ("from LocutorioPagador inner join Locutorio...."), the cast to LocutorioPagador fails.</p>

<p>If i get the Locutorio i want in a query by id and then i try to apply a Filter to the Pagadoras and get them with Locutorio.getPagadoras , it fails because i can't filter Pagador since the query only makes the join between LocutorioPagador and Locutorio.</p>

<p>If i do with criteria, the problem is that LocutorioPagador doesn't make de join neither with Locutorioor with Pagador.</p>

<p>I have seen many examples of filters but no one in this kind of relation (manytomany with extra columns: oneToMany with mapped and manyToOne) and trying to filter fields from the relation and fields from the both entities relationated.</p>

<p>Can anybody help me? I would like to use filters but i hear any solution.</p>

<p>Thank you in advance.</p>

<p>(Sorry about my English)</p>

<p>My Code.</p>

<pre><code>package com.precios.entity.view.test;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "TBL_LOCUTORIO")

public class Locutorio implements java.io.Serializable
{

    private static final long serialVersionUID = 3803114774434200256L;
    private int id;
    private Integer idPais;
    private int estado;;
    private Set&lt;LocutorioPagador&gt; locutorioPagadoras = new HashSet&lt;LocutorioPagador&gt;(0);

    public Locutorio()
    {
    }
    public Locutorio(int id, Integer idPais, int estado)
    {
        this.id = id;
        this.idPais = idPais;
        this.estado = estado;
    }

    public Locutorio(int id, Integer idPais, int estado, Set&lt;LocutorioPagador&gt; locutorioPagadoras)
    {
        super();
        this.id = id;
        this.idPais = idPais;
        this.estado = estado;
        this.locutorioPagadoras = locutorioPagadoras;
    }


    @Id
    @Column(name = "Id", nullable = false)
    public int getId()
    {
        return this.id;
    }

    public void setId(int id)
    {
        this.id = id;
    }



    @Column(name = "Id_Pais")
    public Integer getIdPais()
    {
        return this.idPais;
    }

    public void setIdPais(Integer idPais)
    {
        this.idPais = idPais;
    }


    @Column(name = "Estado", nullable = false)
    public int getEstado()
    {
        return this.estado;
    }

    public void setEstado(int estado)
    {
        this.estado = estado;
    }


    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.locutorio", cascade=CascadeType.ALL)
    public Set&lt;LocutorioPagador&gt; getLocutorioPagadoras() {
        return this.locutorioPagadoras;
    }

    public void setLocutorioPagadoras(Set&lt;LocutorioPagador&gt; locutorioPagadoras)
    {
        this.locutorioPagadoras = locutorioPagadoras;
    }

}

    package com.precios.entity.view.test;


import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "TBL_PAGADOR")


public class Pagador implements java.io.Serializable
{

    private static final long serialVersionUID = -8386483901842298844L;
    private int id;
    private Integer idPais;
    private String empresa;
    private Integer idMoneda;
    private int operativa;
    private Set&lt;LocutorioPagador&gt; locutorioPagadoras = new HashSet&lt;LocutorioPagador&gt;(0);


    public Pagador()
    {
    }

    public Pagador(int id, Integer idPais, String empresa, Integer idMoneda, int operativa)
    {
        this.id = id;
        this.idPais = idPais;
        this.empresa = empresa;
        this.idMoneda = idMoneda;
        this.operativa = operativa;
    }

    public Pagador(int id, Integer idPais, String empresa, Integer idMoneda, int operativa, Set&lt;LocutorioPagador&gt; locutorioPagadoras)
    {
        this.id = id;
        this.idPais = idPais;
        this.empresa = empresa;
        this.idMoneda = idMoneda;
        this.operativa = operativa;
        this.locutorioPagadoras = locutorioPagadoras;
    }



    @Id
    @Column(name = "Id", nullable = false)
    public int getId()
    {
        return this.id;
    }

    public void setId(int id)
    {
        this.id = id;
    }



    @Column(name = "Id_Pais")
    public Integer getIdPais()
    {
        return this.idPais;
    }

    public void setIdPais(Integer idPais)
    {
        this.idPais = idPais;
    }

    @Column(name = "Empresa", length = 150)
    public String getEmpresa()
    {
        return this.empresa;
    }

    public void setEmpresa(String empresa)
    {
        this.empresa = empresa;
    }

    @Column(name = "Id_Moneda")
    public Integer getIdMoneda()
    {
        return this.idMoneda;
    }

    public void setIdMoneda(Integer idMoneda)
    {
        this.idMoneda = idMoneda;
    }

    @Column(name = "Operativa", nullable = false)
    public int getOperativa()
    {
        return this.operativa;
    }

    public void setOperativa(int operativa)
    {
        this.operativa = operativa;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.pagador")
    public Set&lt;LocutorioPagador&gt; getLocutorioPagadoras() {
        return this.locutorioPagadoras;
    }

    public void setLocutorioPagadoras(Set&lt;LocutorioPagador&gt; locutorioPagadoras) {
        this.locutorioPagadoras = locutorioPagadoras;
    }


}

    package com.precios.entity.view.test;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "TBL_REL_LOCUTORIO_PAGADOR")

@AssociationOverrides({
    @AssociationOverride(name = "pk.locutorio", 
        joinColumns = @JoinColumn(name = "IdLocutorio")),
    @AssociationOverride(name = "pk.pagador", 
        joinColumns = @JoinColumn(name = "IdPagador")) })
public class LocutorioPagador implements java.io.Serializable
{

    private static final long serialVersionUID = 1414926985992376395L;
    private LocutorioPagadorId pk = new LocutorioPagadorId();
    private int visible;

    public LocutorioPagador()
    {
    }

    @EmbeddedId
    public LocutorioPagadorId getPk() {
        return pk;
    }

    public void setPk(LocutorioPagadorId pk) {
        this.pk = pk;
    }

    @Transient
    public Locutorio getLocutorio() {
        return getPk().getLocutorio();
    }

    public void setLocutorio(Locutorio locutorio) {
        getPk().setLocutorio(locutorio);
    }

    @Transient
    public Pagador getPagador() {
        return getPk().getPagador();
    }

    public void setPagador(Pagador pagador) {
        getPk().setPagador(pagador);
    }


    @Column(name = "Visible", nullable = false)
    public int getVisible()
    {
        return this.visible;
    }

    public void setVisible(int visible)
    {
        this.visible = visible;
    }

    public boolean equals(Object other)
    {
        if (this == other)
            return true;
        if (other == null || getClass() != other.getClass())
            return false;

        LocutorioPagador that = (LocutorioPagador) other;

        if (getPk() != null ? !getPk().equals(that.getPk())
                : that.getPk() != null)
            return false;

        return true;
    }

    public int hashCode()
    {
        return (getPk() == null ? 0 : getPk().hashCode());
    }

}

    package com.precios.entity.view.test;

import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;



@Embeddable
public class LocutorioPagadorId implements java.io.Serializable {

    private static final long serialVersionUID = 1962350834544505635L;
    private Locutorio locutorio;
    private Pagador pagador;

    @ManyToOne
    public Locutorio getLocutorio() {
        return locutorio;
    }

    public void setLocutorio(Locutorio locutorio) {
        this.locutorio = locutorio;
    }

    @ManyToOne
    public Pagador getPagador() {
        return pagador;
    }

    public void setPagador(Pagador pagador) {
        this.pagador = pagador;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        LocutorioPagadorId that = (LocutorioPagadorId) o;

        if (locutorio != null ? !locutorio.equals(that.locutorio) : that.locutorio != null) return false;
        if (pagador != null ? !pagador.equals(that.pagador) : that.pagador != null)
            return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (locutorio != null ? locutorio.hashCode() : 0);
        result = 31 * result + (pagador != null ? pagador.hashCode() : 0);
        return result;
    }

}
</code></pre>