java.lang.IllegalArgumentException: Undefined filter parameter [p1]

4k Views Asked by At

I am trying to execute Hibernate Filter.

Here is my POJO class:

@Entity
@Table(name="flight")
@FilterDef(name="f1",parameters=@ParamDef(name="status",type="String"))
@Filter(name="f1",condition="status=:p1")
public class Flight 
{
    @Id
    @Column(name="flightno")
    private int flightNumber;

    @Column(name="src", length=10)
    private String source; 

    @Column(name="dest",length=10)
    private String destination;

    @Column(name="status",length=10)
    private String status;
//setter & getters
}

And here is my Main class code :

public static void main(String[] args)
{   
       //code for getting SessionFactory Object
        Session session=factory.openSession();
        Transaction tx=session.beginTransaction();

    Query query=session.createQuery("from Flight f");
    Filter filter=session.enableFilter("f1");
    filter.setParameter("p1","DELAYED");
    List list=query.list();
    Iterator itr=list.iterator();
    while(itr.hasNext())
    {
        Flight f=(Flight)itr.next();
        System.out.println("FLIGHT NO:"+f.getFlightNumber());
        System.out.println("SOURCE :"+f.getSource());
        System.out.println("DESTINATION :"+f.getDestination());
        System.out.println("STATUS :"+f.getStatus());

        session.close();
    }

But i am the output like this:

Exception in thread "main" java.lang.IllegalArgumentException: Undefined filter parameter [p1]

2

There are 2 best solutions below

0
Noah Solomon On

The error message in this case is somewhat misleading. Hibernate is trying to tell you that the filter parameter is misconfigured.

I ran into this problem when I had a similar mapping with a Long. The issue appears to be with the ParamDef's type definitions. For some reason using the class name in the type parameter doesn't work for Long and String.

It does correctly map the type if you specify it as a "primitive" by using lowercase "long" or "string"

@ParamDef(name="status",type="string")
1
Jams Fan On

i think you need also modify p1 to status as u define @ParamDef(name="status",type="String")

@Filter(name="f1",condition="status=:status")