当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How can I findByExample(Object instance) easily without HibernateTemplate?

问题描述:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: