问题描述:

I tried to integrate Spring 3 with Hibernate 4 and I found that Spring don't support HibernateDaoSupport and HibernateTemplate any more in Spring's Hibernate4 package.I have to use native Hibernate Session API.

So,here is my problem:

How can I findByExample(Object instance)(this is one method provided

by HibernateTemplate before) easily?

I have to splice sql by myself which is very painful.I have used mybatis before whose dynamic sql is very useful but I still don't think it is as easily as HibernateTemplate.

网友答案:

Hibernate itself have this funcionality, using the Example criterion. As you already have to use Hibernate's Session you could just do something like this:

List results = session.createCriteria(MyClass.class)
 .add( Example.create(object) )
 .list();

More info on Hibernate Documentation.

网友答案:

I would suggest you to take a look at Spring Data JPA Repository.

http://spring.io/blog/2011/02/10/getting-started-with-spring-data-jpa/

网友答案:

Try with Spring data, another alternative could be write an generic dao class and injects the hibernate session on it.

相关阅读:
Top