简体   繁体   English

休眠多个表上的一对多联接

[英]Hibernate one-to-many joins on multiple tables

So, I have a class called app. 所以,我有一堂课叫app。 app has a one-to-many relation with a class called user. 应用与一个名为user的类具有一对多关系。 user has a one-to-many relation with a class called login. 用户与一个名为login的类具有一对多关系。 Login contains fields that represent the time the user logged out, expressed as timestamps. 登录包含代表用户注销时间的字段,以时间戳记表示。 If I wanted to make a hibernate criteria query that returns the users that have signed on within the past x numbers of days, how would you do it? 如果我想进行休眠标准查询,以返回过去x天之内已登录的用户,您将如何处理? This is what I have so far, and it just returns all users regardless of when they logged on: 这是我到目前为止的内容,它只会返回所有用户,无论他们何时登录:

Calendar calendar = Calendar.getInstance();
java.util.Date now = calendar.getTime();
java.sql.Timestamp currentTimestamp = new  java.sql.Timestamp(now.getTime());
Calendar calendar2 = Calendar.getInstance();
calendar2.roll(Calendar.DAY_OF_MONTH, days*(-1));
java.util.Date past = calendar2.getTime();
java.sql.Timestamp pastTimestamp = new java.sql.Timestamp(past.getTime());    
Criteria LoginCriteria = session.createCriteria(Application.class, "App")
                    .add(Restrictions.eq("App.appID", appid))
                    .createAlias("App.users", "user")
                        .createAlias("user.logins", "lg")
                        .add(Restrictions.ge("lg.endTime", pastTimestamp ))
                        .add(Restrictions.le("lg.endTime", currentTimestamp));

I am using timestamps to measure time. 我正在使用时间戳来测量时间。 I can provide more info if needed. 如果需要,我可以提供更多信息。

Criteria LoginCriteria = session.createCriteria(User.class, "user")
                .add(Restrictions.eq("user.appId", "https://"+appid+".com"))
                .createAlias("user.logins", "lg")
                .add(Restrictions.ge("lg.startTime", pastTimestamp ))
                .add(Restrictions.le("lg.startTime", currentTimestamp));

Alternatively I think this should work, though I don't have your structure set up to test it out: 另外,我认为这应该可行,尽管我没有设置您的结构来进行测试:

Criteria LoginCriteria = session.createCriteria(User.class, "user")
      .add(Restrictions.eq("user.appId", "https://"+appid+".com"))
      .createCriteria("logins")
           .add(Restrictions.ge("startTime", pastTimestamp))
           .add(Restrictions.le("startTime", currentTimestamp));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM