facebook

Consideration for added "findBy" RE gen for find b

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

    Douglas M Hurst
    Participant

    The code below works just fine but I had to create my own method in the DAO that I wish were generated by the Hibernate RE.

    If there’s a different way to perform “LIKE” queries with code that’s currently generated, maybe you could point one out.

    These are the possibles for the column “test”

    primary database count test
    primary database content test
    primary file count test
    primary file content test
    primary queue monitor test

    I’m attempting to recover those that have the string LIKE “database” in them by invoking Example class criteria.

    In my main class

    // Retrieve all the items from the table monitor_schedule by criteria
    MonitorSchedule ms = new MonitorSchedule();
    ms.setTest(“database”); // SET THE “LIKE” PARTIAL CRITERIA
    List<MonitorSchedule> dbList = persistenceLayer.findMonitorScheduleByCriteria(ms);
    Iterator<MonitorSchedule> dbIt = dbList.iterator();
    while(dbIt.hasNext()) {
    ms = dbIt.next();
    System.out.println(ms.getTest());
    }

    In my persistence layer class

    public List<MonitorSchedule> findMonitorScheduleByCriteria(MonitorSchedule ms) {
    Example example = Example.create(ms);
    example.enableLike(MatchMode.ANYWHERE); // this is for LIKE
    example.ignoreCase();

    DetachedCriteria c = DetachedCriteria.forClass(MonitorSchedule.class).add(example);
    return monitorScheduleDAO.findByCriteria(c);}

    In the DAO class I added this method that I wish were generated by an RE.

    // HURST ADDED
    public List findByCriteria(DetachedCriteria c) {
    log.debug(“finding MonitorSchedule instance by criteria”);
    try {
    List results = getHibernateTemplate().findByCriteria(c);
    log.debug(“find by example successful, result size: ” + results.size());
    return results;
    } catch (RuntimeException re) {
    log.error(“find by example failed”, re);
    throw re;
    }
    }
    // END HURST ADDED

    Again, I welcome comments.

    #287189 Reply

    Douglas M Hurst
    Participant

    Just FYI, The daohome.vm could be modified thus…

    #if($jdk5)
    public ${pojo.importType(“java.util.List”)}<${declarationName}> findByCriteria(DetachedCriteria instance) {
    #else
    public ${pojo.importType(“java.util.List”)} findByCriteria(DetachedCriteria instance) {
    #end
    log.debug(“finding ${declarationName} instance by criteria”);
    try {
    #if (!$hibernateDaoSupport)
    #if($jdk5)
    ${pojo.importType(“java.util.List”)}<${declarationName}> results = (List<${declarationName}>) ${currSession}
    #else
    ${pojo.importType(“java.util.List”)} results = ${criSession}
    #end
    .createCriteria(“$clazz.entityName”)
    #if($jdk5)
    .add( $pojo.staticImport(“org.hibernate.criterion.Example”, “create”)(instance) )
    #else
    .add(${pojo.importType(“org.hibernate.criterion.Example”)}.create(instance))
    #end
    .list();
    #else
    #if($jdk5)
    ${pojo.importType(“java.util.List”)}<${declarationName}> results = (List<${declarationName}>) getHibernateTemplate().findByCriteria(instance);
    #else
    ${pojo.importType(“java.util.List”)} results = getHibernateTemplate().findByCriteria(instance);
    #end
    #end
    log.debug(“find by criteria successful, result size: ” + results.size());
    return results;
    } catch (RuntimeException re) {
    log.error(“find by criteria failed”, re);
    throw re;
    }
    }

    … which would produce…

    public List findByCriteria(DetachedCriteria instance) {
    log.debug(“finding MonitorSchedule instance by criteria”);
    try {
    List results = getHibernateTemplate().findByCriteria(instance);
    log.debug(“find by criteria successful, result size: ” + results.size());
    return results;
    } catch (RuntimeException re) {
    log.error(“find by criteria failed”, re);
    throw re;
    }
    }

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: Consideration for added "findBy" RE gen for find b

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