[英]Select entity from two tables in hibernate
I have two tables like this: 我有两个这样的表:
+------------+ +------------+
| Parent | | Child |
+------------+ +------------+
|child_id |------------>|id |
|id | |date |
|name | +------------+
+------------+
I need to extract objects of type Parent
depending on date
我需要根据
date
提取类型为Parent
对象
This is my query: 这是我的查询:
Query query = session.createQuery(
"SELECT p.child_id, p.id, p.name from Parent p, Child c
WHERE p.child_id = c.id
AND c.date between :date and CURRENT_DATE ");
query.setTimestamp("date",new Date(2012,1,1));
List<Parent> objList = query.list() // List<Object[]> instead of List<Parent>
I've been trying for hours already and I couldn't get any closer then this. 我已经尝试了好几个小时,然后就再也无法接近了。 Can you tell me what is wrong with my query or how can I achieve my goal?
您能否告诉我查询的问题或如何实现我的目标?
EDIT: I found a problem: new Date(2012,1,1)
was creating a date of year 3092, I fixed it by adding the date through java.sql.Date
编辑:我发现一个问题:
new Date(2012,1,1)
正在创建3092年的日期,我通过通过java.sql.Date
添加日期来修复它
The result is an List<Object[]>
, I know I could iterate each objects vector, and create a new Parent with values from objects, but isn't there a way I can get directly a Parent
object from query list? 结果是
List<Object[]>
,我知道我可以迭代每个对象向量,并使用对象中的值创建一个新的Parent,但是没有办法直接从查询列表中获取Parent
对象吗?
Time is cut off if you use this call: 如果使用此呼叫,时间将被切断:
query.setDate(...);
Instead, use the following call: 而是使用以下调用:
query.setTimestamp(...)
Try also to use Criteria Queries 也尝试使用条件查询
Such as: 如:
criteria.add(Expression.eq("date", new Date()));
If the query works without date restrictions, you can make some experiments by comparing the dates. 如果查询没有日期限制,则可以通过比较日期进行一些实验。 For example, as follows:
例如,如下:
...
Date customDate = new Date(2012, 1, 1);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
StringBuilder query = new StringBuilder("from Child c");
query.append(" where c.date >= '" + simpleDateFormat.format(customDate) + "'");
Query result = session.createQuery(query.toString());
List resultList = result.list();
...
query.setParameter("date",new Date(2012,1,1));
使用setParameter方法而不是setDate
Turns out there were two problems and this is the solution to each: 原来有两个问题,这是每个问题的解决方案:
java.util.Date(int year, int month, int day)
should be avoided as it turns out it messes up the date. 应当避免使用
java.util.Date(int year, int month, int day)
因为它弄乱了日期。 You can solve this problem by using java.util.Calendar
and set the date to it. 您可以使用
java.util.Calendar
并为其设置日期来解决此问题。
Then you need to convert it to java.sql.Date to match the query syntax 然后您需要将其转换为java.sql.Date以匹配查询语法
I managed to get a List directly from the query (without resorting to manually iterating the list of Objects[]
and creating Parent
entities myself) using this query: 我设法使用以下查询从查询中直接获取列表(无需手动迭代
Objects[]
列表并自己创建Parent
实体):
Query query = session.createSQLQuery("select p.* from myscheme.Parent p, myscheme.Child c
WHERE p.child_id = c.id AND c.date > :date ")
.addEntity(Parent.class);
query.setTimestamp("date",sqlDate);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.