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]

2 Answers 2

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")
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.