java. Lang. illegalargumentexception: undefined filter parameter [P1]

I'm trying to execute hibernate filter

This is my POJO course:

@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
}

This 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 here's my output:

Exception in thread "main" Java Lang. illegalargumentexception: undefined filter parameter [P1]

Solution

The error message in this case is a bit misleading Hibernate tries to tell you that the filter parameter is incorrectly configured

I encountered this problem when I used long for similar mapping The problem seems to be related to the type definition of paramdef For some reason, using the class name in the type parameter does not apply to long and string

If you specify it as a primitive by using lowercase "long" or "string", it maps the type correctly

@ParamDef(name="status",type="string")
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>