-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Hibernate Filters
PostPosted: Wed Feb 25, 2009 11:42 am 
Newbie

Joined: Sat Nov 29, 2008 3:33 pm
Posts: 4
Hi guys!

I'm trying use a hibernate filter in a entity that participates of an inheritance, but when i enable the filter in the session, and i use the criteria API or the query API to list the records, the filter don't works!
But, when i modify the same entity, removing the inheritance the filter works! What happens ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2009 7:05 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You need to provide more information. You should at least post relevent mapping information/code with annotations, information about the filters, error messages, etc.


Top
 Profile  
 
 Post subject: Mappings
PostPosted: Thu Feb 26, 2009 7:10 am 
Newbie

Joined: Sat Nov 29, 2008 3:33 pm
Posts: 4
I'm Brazilian, thus most of the terms in the code are in portuguese.

The first is the base class Pessoa ( People in english ):

Code:
package br.netsoft.beans;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.validator.NotEmpty;

import br.netsoft.triggers.annotation.Property;
import br.netsoft.triggers.annotation.Trigger;
import br.netsoft.triggers.annotation.Triggers;
import static org.hibernate.annotations.GenerationTime.INSERT;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Pessoa implements Serializable {

   private static final long serialVersionUID = 6618459058833565385L;

   private Integer id;
   private Date dataCadastro;
   private String nome;
   private Bairro bairro;
   private Cidade cidade;
   private String endereco;
   private String telefone;
   private String ramal;
   private String fax;
   private String cep;
   private Zona zona;
   private TipoPessoa tipoPessoa;

   public Pessoa() {
   }

   // ... only getters and setters (omited for brevity)
}




The second is the class Funcionario ( Employee in english ):


Code:

package br.netsoft.beans;


import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.annotations.ParamDef;
import org.hibernate.validator.NotNull;

import br.netsoft.validation.annotations.ValorPositivo;

@Entity @org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)

@FilterDef(name="procurarFuncionarioPorTipo",parameters={@ParamDef(name="tipoFuncionario",
      type="string")})
@Filter(name="procurarFuncionarioPorTipo",condition=":tipoFuncionario = tipo")
public class Funcionario extends Pessoa {

   private static final long serialVersionUID = 657635050835720817L;
   private Loja loja;
   private Date dataAdmissao;
   private Double comissao=0d;
   private TipoFuncionario tipo = TipoFuncionario.Administrativo;
   private Double comissaoPre=0d;
   private Double comissaoAtacado=0d;
   private Date dataDemissao;
   private String grauInstrucao;
   private PIS pis;
   private String funcao;
   private String naturalidade;
   private Double salario=0d;

   private Date horarioInicio;
   private Date horarioFinal;
   private Date horarioInicioSabado;
   private Date horarioFinalSabado;


   private CarteiraTrabalho ctps;

   private EstadoCivil estadoCivil;
   private Date dataNascimento;
   private TituloEleitor titulo;
   private String cpf;
   private RG rg;

   public Funcionario() {
      super();
      pis = new PIS();
   }


   // ... only getters and setters (omited for brevity)

}




The TipoFuncionario enum:
Code:

package br.netsoft.beans;

public enum TipoFuncionario {
   Vendedor,Balconista,Entregador,Tecnico, Cobrador, Indicador,Motorista,Administrativo,Todos

}



Thus, when I will use the filter, i do ...

Code:

Session s = factory.openSession();

s.enableFilter("procurarFuncionarioPorTipo").setParameter("tipoFuncionario",TipoFuncionario.Vendedor.name()));

List employees =  s.createCriteria(Funcionario.class).list();

for(Object o: employees)
     System.out.println(o);



... and the for loop show all records of Funcionario in the database


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2009 1:02 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
I think if you look at Subclass in the hibernate core you'll find that by default only the filters from the superclass are added to the Persister, i.e. the filters from your subclass are ignored.

If you search the JIRA, I created an issue a few years back. I can't remember all of the details on why I needed it but I have a patch attached in the report that might help you out.

or you can just patch in the following
Code:
   public Map getFilterMap() {
      Map filterMap = getSuperclass().getFilterMap();
      filterMap.putAll(super.getFilterMap());
      return filterMap;
   }


I can't remember if I actually need it anymore, perhaps I'll re-evaluate why I needed it way back when.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2009 1:53 pm 
Newbie

Joined: Sat Nov 29, 2008 3:33 pm
Posts: 4
Interesting. But, what's the explication for this design choice or bug (i don't know) ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 4:18 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
This is the issue: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2394

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.