java.lang.IllegalArgumentException: Undefined filter parameter [p1]
Asked Answered
T

2

8

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]

Turbofan answered 31/7, 2016 at 20:48 Comment(0)
S
15

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")
Siren answered 2/11, 2016 at 19:49 Comment(0)
N
0

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

@Filter(name="f1",condition="status=:status")
Neurogenic answered 16/9, 2022 at 3:11 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Brunabrunch

© 2022 - 2024 — McMap. All rights reserved.