- This topic has 3 replies, 3 voices, and was last updated 18 years ago by
Steve Prior.
-
AuthorPosts
-
wasssMemberHi,
I’ve been doing some tutorials for Hibernate using myeclipse (5.1.1 is the version I’m using), and they are really good.However, after I did a simple java example of grabbing some table data, inserting a new record, then re-grabbing that data to see the new value added, I came up with a question.
“Is it possible to grab all the rows from a table? ”
All the methods generated by the hibernate tools in myeclipse seem to expect one to grab specific entries or entries that satisfy a certain criteria. (which is probably the most popular usage)
So, I went into the DAO class and made a simple new method, with the query string
String queryString = "from [tableName] ";
and that does the trick.
Is there a way to autogenerate this method, or am I the only one who has wanted to do this?
I sifted thru the forums a bit but found nothing of the sort.
Riyad KallaMemberYou did the right thing, there is no way to auto-generate this yet, but it’s a pending enhancement request. I’ll add your vote to the issue.
Steve PriorMemberThis will retrieve all records (note that order is NOT guaranteed). Setting nothing on the example object allows everything to match.
YourClass foo = new YourClass();
List myList = dao.findByExample(foo);
Steve PriorMemberIf you’re considering expanding the DAO template, let me share what I came up with which I’m pretty happy with.
This is a JDK1.5+ specific abstract generic DAO which has worked very well for me so far. The only thing I don’t like is that I have to pass the object class as the first parameter for findById(), but I didn’t see any other way to accomplish this.
This abstract DAO can sit in a common library someplace, then you bind it into a specific hibernate session factory with:public class GenericDAO<POJO> extends AbstractGenericDAO<POJO> {
public Session getSession() {
return BaseDAO.getSession();
}
}Then if you still need an object specific DAO you extend your GenericDAO (the example uses my Bookmark class):
public class BookmarkDAO extends GenericDAO<Bookmark>{
…
}This structure means that you only need to create an object specific DAO when you have specific custom queries for that class.
For simple find by id’s I useGenericDAO<Bookmark> bookmarkDAO = new GenericDAO<Bookmark>();
Bookmark bookmark = bookmarkDAO.findById(Bookmark.class, 1);It may be code bloat, but I find findByCriterion and findOneByCriterion to be VERY useful.
Note that a benefit of this code is that you avoid using @SuppressWarnings(“unchecked”) in
your user code or even in your custom DAO code.Enjoy
—————————- snip ——————————–
package com.geekster.hibernate.utils;import java.io.Serializable;
import java.util.Collection;
import java.util.List;import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;public abstract class AbstractGenericDAO<POJO> {
public abstract Session getSession();
public Criteria createCriteria(Class c){
return getSession().createCriteria(c);
}public void save(POJO transientInstance) {
getSession().saveOrUpdate(transientInstance);
}public void delete(POJO persistentInstance) {
getSession().delete(persistentInstance);
}@SuppressWarnings(“unchecked”)
public POJO findById( Class c, Serializable id) {
return (POJO) getSession().get(c, id);
}public List<POJO> findByExample(POJO instance) {
return findByCriterion(instance.getClass(), Example.create(instance));
}public POJO findOneExample(POJO instance) {
return findOneByCriterion(instance.getClass(), Example.create(instance));
}@SuppressWarnings(“unchecked”)
public List<POJO> findByCriterion( Class c, Criterion… criterion){
Criteria crit = createCriteria(c);
for (Criterion cr : criterion){
crit.add(cr);
}
return crit.list();
}@SuppressWarnings(“unchecked”)
public POJO findOneByCriterion(Class c, Criterion… criterion){
Criteria crit = createCriteria(c);
for (Criterion cr : criterion){
crit.add(cr);
}
return (POJO) crit.uniqueResult();
}@SuppressWarnings(“unchecked”)
public List<POJO> findByCriterion(Class c, Collection<Criterion> criterion, Collection<Order> orders){
Criteria crit = createCriteria(c);
if (criterion!=null){
for (Criterion cr : criterion){
crit.add(cr);
}
}
if (orders!=null){
for (Order order : orders){
crit.addOrder(order);
}
}
return crit.list();
}@SuppressWarnings(“unchecked”)
public POJO findOneByCriterion(Class c, Collection<Criterion> criterion){
Criteria crit = createCriteria(c);
if (criterion!=null){
for (Criterion cr : criterion){
crit.add(cr);
}
}
return (POJO) crit.uniqueResult();
}@SuppressWarnings(“unchecked”)
public POJO merge(POJO detachedInstance) {
return (POJO) getSession().merge(detachedInstance);
}public void attachDirty(POJO instance) {
getSession().saveOrUpdate(instance);
}public void attachClean(POJO instance) {
getSession().lock(instance, LockMode.NONE);
}
} -
AuthorPosts