---
title: "hibernate 时间条件查询oracle问题"
author: "flypeace"
date: "2010-08-17T18:44:26Z"
tags: []
categories: ["企业架构"]
slug: "hibernate-oracle"
draft: false
lastmod: "2011-07-01T15:19:32Z"
id: 739526
url: "https://host.iteye.com/blog/739526"
---

用

```
		res = (List)super.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)

					throws HibernateException, SQLException {

				// TODO Auto-generated method stub

                 String sql = "select p.* ,q.* from TABLE1 q  right join  TABLE2 p on q.FIELD1 = p.FIELD1 
                                    where p.update_date>?";

				SQLQuery query = session.createSQLQuery(sql);

				List _res = null;

				if(t!=null)
				{
					_res = query.addEntity("p", ENTITY1.class).addEntity("q", ENTITY2.class)
					.setDate(0,t).list();

				}

				return _res;

			}});
```

  以上当数据库中字段update\_time格式是：yyyy-MM-dd HH:mm:ss，采用以上方式查询 update\_time 大于某个时间的记录貌似查询条件不起作用，为了尽快解决问题，不纠结于SLQQuery中怎么实现的，后来换了一种方式：

 

    将语句修改为：

```
java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dt = format.format(date );
String sql = "select p.* ,q.* from TABLE1 q  right join  
TABLE2 p on q.FIELD1 = p.FIELD1  where 
p.update_date>to_date('"+dt+"','yyyy-mm-dd hh24:mi:ss')";
```

 

将查询条件去掉：

 

```
_res = query.addEntity("p", ENTITY1.class).addEntity("q", ENTITY2.class).list();
```

 

问题暂时得到解决，但是对于hibernate 跨数据表的DAO有点疑虑。