facebook

hibernate: Grabbing all rows from a table with a DAO

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #268946 Reply

    wasss
    Member

    Hi,
    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.

    #268975 Reply

    Riyad Kalla
    Member

    You 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.

    #269009 Reply

    Steve Prior
    Member

    This 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);

    #269010 Reply

    Steve Prior
    Member

    If 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 use

    GenericDAO<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);
    }
    }

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: hibernate: Grabbing all rows from a table with a DAO

You must be logged in to post in the forum log in