`
sooxin
  • 浏览: 250627 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

hibernate 多字段联合查询

阅读更多
hibernate 可自动生成字段的find by(),但实际应用中需要对一个表的字段联合查询.查了一下,具体实现方法还就是一个动态生成hql的方法.

public static void main(String[] args) {
      
       Session session = null;
       Transaction tx = null;
       List list = null;
       Criteria criteria = null;

       try {

           session = HibernateSessionFactory.getSession();
           tx = session.beginTransaction();

           DetachedCriteria detachedCriteria = DetachedCriteria
                  .forClass(InfoTab.class);
          
          
           String sql=" 1=1 ";
          
           Integer pareaId = 0; // 父地区;
           Integer careaId = 0; // 子地区;
           Integer categoryId = 0; // 类别;
           String infoPrivider = "中介"; // 来源;
           String houseType= "地下室"; // 房屋类型;
           Integer hxBedRoom=0; // 室;
           Integer hxLivingRoom=0; // 厅;
          
           String hzHouseStatus="有房出租"; // 合租类型;
           String hzRequestSex="男"; // 性别要求;
           String fixUp="尚未"; // 装修程度;
           Integer lcHeightMolecuse=0; // 楼层;
           String orientation="东南"; // 朝向要求;
           Integer buildArea=2000; // 建筑面积;
           Integer useArea=80; // 使用面积;
           Integer rentalDigit=2000; // 租金/价格;
           String title= "出租"; // 标题;
          
           if(pareaId!=0)
           {
              sql+="pareaId=" + pareaId;
           }
 
           ......................................

         if(!title.equals(""))
           {
              sql+=" and title like '%" + title + "%'";
           }
           sql+=" order by id desc";
          
           System.out.println(sql);

           detachedCriteria.add(Restrictions.sqlRestriction(sql));

           criteria = detachedCriteria.getExecutableCriteria(session);

           list = criteria.list();
          
           for(int i=0;i<list.size();i++)
           {
              InfoTab infoTab = (InfoTab)list.get(i);
              System.out.println(infoTab.getTitle() +" "+ infoTab.getCategoryId() +" "+ infoTab.getPareaName() +" "+ infoTab.getCareaName() +" " + infoTab.getHouseType() +" " + infoTab.getInfoPrivider());
           }

           tx.commit();

       } catch (HibernateException he) {
           he.printStackTrace();
       }
    }
分享到:
评论

相关推荐

    hibernate中的外键查询实现

    专用于HIBERNATE中两个对象间的外键关联查询

    Hibernate 多数据库支持解决方案

    Hibernate多数据库支持解决方案 一、 去掉数据库级的外键关联 二、 如何动态加载.hbm.xml文件 三、 配置文件的加载 四、 大字段处理 五、 hibernate主键生成器(数值型主键操作方法) 六、 Oracle 转换 SQL Server ...

    Hibernate注解映射联合主键

    联合主键用Hibernate注解映射方式主要有三种: 第一、将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将该类注解为@Embeddable,最后在主类中(该类不包含联合主键...

    Hibernate+中文文档

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合...

    Hibernate3.1_学习源码

    案例目录: 01 01Hibernate_Handwork : 手工配置使用Hibernate,其中详细标了...07 07Hibernate_Mapping : Hibernate中的数据关联技术,是一个重点又是一个难点,演示了 一对一、多对一、一对多、多对多等几种情况。

    java hibernate使用注解来定义联合主键

    主要介绍了java hibernate使用注解来定义联合主键的相关资料,需要的朋友可以参考下

    Hibernate关联映射-one to one单向外键关联

    NULL 博文链接:https://zhouhaitao.iteye.com/blog/1171227

    hibernate3.2中文文档(chm格式)

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合...

    HibernateAPI中文版.chm

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合...

    Hibernate笔记 马士兵

    第1课 课程内容 6 第2课 Hibernate UML图 6 第3课 风格 7 第4课 资源 7 第5课 环境准备 7 ...三、 联合主键 24 1、xml方式 24 2、annotation方式 27 第14课 Hibernate核心开发接口(重点) 29 ........

    hibernate一对多例子

    hibernate一对多例子 一对多关联 通过外键 连接两个类对应的表,而没有中间集合表。 这个关系模型失去了一些Java集合的语义: 一个被包含的实体的实例只能被包含在一个集合的实例中 一个被包含的实体的实例只能...

    Hibernate 中文 html 帮助文档

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted ...

    Hibernate中文详细学习文档

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合...

    Hibernate中文API大全

    组件的属性可以是任意一种Hibernate类型(包括集合, 多对多关联, 以及其它组件等等)。嵌套组件不应该被当作一种特殊的应用(Nested components should not be considered an exotic usage)。 Hibernate倾向于支持...

    最全Hibernate 参考文档

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted ...

    Hibernate Annotation 唯一外键一对一双向关联

    NULL 博文链接:https://paladin1988.iteye.com/blog/1640340

    hibernate 体系结构与配置 参考文档(html)

    14. HQL: Hibernate查询语言 14.1. 大小写敏感性问题 14.2. from子句 14.3. 关联(Association)与连接(Join) 14.4. join 语法的形式 14.5. select子句 14.6. 聚集函数 14.7. 多态查询 14.8. where子句 14.9....

    hibernate 教程

    值集合和多对多关联(Collections of Values and Many-To-Many Associations) 6.4. 一对多关联(One-To-Many Associations) 6.5. 延迟初始化(延迟加载)(Lazy Initialization) 6.6. 集合排序(Sorted ...

    深入浅出Hibernate中文版 part1

    4.4 数据关联 4.5 Hibernate数据检索 4.6 HQL实用技术 4.7 自定义持久化实现 第5章 Hibernate高级特性 5.1 Hibernate持久化实现 5.2 Hibernate回调与拦截机制 5.3 Hibernate实用技术 第6章 Hibernate 实战...

    Hibernate教程

    7.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted...

Global site tag (gtag.js) - Google Analytics