hibernatefilternhibernate-mapping

No such filter configured [shoesFilter]


I saw some questions, but did not find any answers which help me.

@Filters({
        @Filter(name = "shoesFilter", condition = ":minShoeSize <= shoeSize and :maxAge >= shoeSize"),
        @Filter(name = "shoesFilter", condition = ":minNumber <= number")
})
@Entity
@Table(name = "Shoes",catalog = "shoues")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Shoes {
    @Id
    @Column(name = "id")
    private Integer id;

    @Column(name = "shoe")
    private Integer shoeSize;

}

And I have defined in hbm.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <filter-def name="shoesFilter">
        <filter-param name="maxShoeSize" type="integer"/>
        <filter-param name="minShoeSize" type="integer"/>
        <filter-param name="minNumber" type="integer"/>
    </filter-def>
</hibernate-mapping>

After that, I call my code

public static void main(String[] args) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    System.out.println("enable filter");
    Filter filter = session.enableFilter("shoesFilter");
    filter.setParameter("maxShoeSize", 40);
    filter.setParameter("minShoeSize", 22);
    filter.setParameter("minNumber", 27);
    session.beginTransaction();
    List<Shoes> result = session.createQuery("from Shoes").list();
    for (Shoes s : result) {
        System.out.println("sad" + s.getShoeSize() + "fds" + s.getId());
    }
    System.out.println("--Apply where clause--");
    System.out.println("--Disable Filter--");
    session.disableFilter("shoesFilter");

    session.close();
}

But when I run the code, I get the following error.

Exception in thread "main" org.hibernate.HibernateException: No such filter configured [shoesFilter]
  at org.hibernate.internal.SessionFactoryImpl.getFilterDefinition(SessionFactoryImpl.java:982)
  at org.hibernate.engine.spi.LoadQueryInfluencers.enableFilter(LoadQueryInfluencers.java:134)
  at org.hibernate.internal.SessionImpl.enableFilter(SessionImpl.java:2327)
  at com.example.hibernateperformance.HibernatePerformanceApplication.main(HibernatePerformanceApplication.java:25)

I've seen a lot of questions and answers on this topic, but those answers didn't help me. Are there any missing items that you see?


Solution

  • I realized where ı went wrong.As you can see aboce in main .I wrote

    HibernateUtil.getSessionFactory().openSession();
    

    like this inside

    @Component
    public class HibernateUtil {
        private static SessionFactory sessionFactory ;
        static {
            Configuration configuration = new Configuration().configure("shoes.hbm.xml");
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            sessionFactory = configuration.buildSessionFactory(builder.build());
        }
        public static SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    }
    

    here ->Configuration().configure("shoes.hbm.xml"); I had not given the file path inside the configure before and so it could not see where the filter was defined.