关键字相等的函数是自动生成的:
public List findByProperty(String propertyName, Object value) {
log.debug("finding Courseware instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Courseware as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}现在我想把他改为关键字模糊查询,就是包含关键字的都查询到,请问应该怎么改
还有,我在查询的时候,只要是查询有中文的就无法得到结果集,但如果查询的是英文和数字就可以,为什么呢?

解决方案 »

  1.   

    在网上找过一下,那个hibernate的配置我都配好了:
    <prop key="hibernate.query.factory_class">
    org.hibernate.hql.classic.ClassicQueryTranslatorFactory
    </prop>
    <prop key="connection.characterEncoding">utf8</prop>
      

  2.   

    尝试过用网上的另一种方法:
    public List<Resource> findByProperty(String propertyName, Object value) {
    log.debug("finding Resource instance with property: " + propertyName
    + ", value: " + value);
    try {
    Session session=getSessionFactory().getCurrentSession();
    Transaction trans=session.beginTransaction();
    String queryString = "from Resource as model where model."+propertyName+"like"+ value;
    Query query = session.createQuery(queryString);
    query.setString("value", "%"+value+"%");
    List result=query.list();
    for(int i=0;i<result.size();i++){
    Resource resource=(Resource)result.get(i);
    Integer resourceId=resource.getResourceId();
    String resourceName=resource.getResourceName();
    String resourceType=resource.getResourceType();
    String location=resource.getLocation();
    String assort=resource.getAssort();
    String discription=resource.getDiscription();
    }
    trans.commit();
    session.close();
    return result;
    } catch (RuntimeException re) {
    log.error("find by property name failed", re);
    throw re;
    }
    }但这个却会抛到异常,不知道哪里错了,纠结了我两天两夜了,求大神们帮帮我,不然我后面的功能都没心情做下去了。。
      

  3.   

    在你之前的例子里的value前台加上% 不就好了么。
      

  4.   


    String queryString = "from Resource as model where model."+propertyName+" like ?";
    Query query = session.createQuery(queryString);
    query.setString(0, "%"+value+"%");貌似楼主like 前面少个空格,楼主我的这样你试试
      

  5.   

    public List findByProperty(String propertyName, Object value) {
    log.debug("finding Courseware instance with property: " + propertyName
    + ", value: " + value);
    try {
    String queryString = "from Courseware as model where model."
    + propertyName + "= ?";
    return getHibernateTemplate().find(queryString, "%"+value+"%");
    } catch (RuntimeException re) {
    log.error("find by property name failed", re);
    throw re;
    }
    }
      

  6.   

    现在暂时不管相等还是模糊查询,是我那个查询中文乱码,在网上找了两天,大家的解决方法都是在Hibernate加上<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
    或者是<prop key="connection.characterEncoding">utf8</prop>但是我加了之后一样不行,5楼的方法我头一天就试过了。我都不知道怎么可以解决中文无法查询的问题了
      

  7.   

    还有,我看到别人可以在某个窗口看到HQL翻译成SQL之后的语句,这样才知道中文有没有乱码,请问在MyEclipse里怎样调出那个窗口,如图
      

  8.   

    图在这个网址里:http://dl.javaeye.com/upload/attachment/43456/76701e3b-1105-3092-8cd8-9a2333989629.bmp
      

  9.   

    你说的是hql调试吧?右击工程MyEclipse-->Open HQL Editor