0

我有一个使用 Hibernate HQL 查询的类。我想将这些更改为 Hibernate Criteria 查询。然而,Hibernate Criteria 对我来说比 HQL 更难理解,即使有教程也是如此。有人可以告诉我其中一个查询如何使用 Hibernate Criteria/Filters 吗?

Configuration configuration = new Configuration();
        configuration.configure().setProperty("hibernate.show_sql", "false");
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(
                                  configuration.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry);

Session session =sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from TABLENAME WHERE IDObject1= :idObjectA" +
              "AND IDObject2= :idObjectB AND IDSettingOption = :idSettingOption");
query.setParameter("idObjectA", idObjectB);
query.setParameter("idObjectB", idObjectB);
query.setParameter("idSettingOption", idSettingOption);

List results = query.list();

提前致谢!

4

2 回答 2

0
Criteria criteria = session.createCriteria( TABLENAME.class );

criteria.add( Restrictions.eq( "IDObject1" , idObjectB) );
criteria.add( Restrictions.eq( "IDObject2" , idObjectB) );
criteria.add( Restrictions.eq( "IDSettingOption" ,idSettingOption) );

List results = criteria.list();

在这里您可以阅读有关 Criteria API 的信息,它有很多示例:
http ://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html

于 2012-10-26T09:14:48.117 回答
0
List results = session.createCriteria(Entity.class)
    .add(Restrictions.eq("IDObject1", idObjectA))
    .add(Restrictions.eq("IDObject2", idObjectB))
    .add(Restrictions.eq("idSettingOption", idSettingOption))
    .list();
于 2012-10-26T09:15:43.110 回答