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