简体   繁体   中英

Select in Hibernate Query language with join

I have two tables : session_start and session_end .

Columns of session_start table are

session_id 
user_id

Columns of session_end table are

session_end_id
session_id 
session_met

Now from user_id , i want to get all session_met for user . What can be the hibernate query for it?

class SessionStart {

  @Id
  @GeneratedValue
  @Column  
  private pid;

  @ManyToOne(fetch = FetchType.LAZY)
  private User user; 

}

class SessionEnd {

  @Id
  @GeneratedValue
  @Column  
  private pid;

  @OneToOne(fetch = FetchType.LAZY)
  private SessionStart start;

  @Column
  private String met;

}

HQL

select end.met 
  from SessionEnd end inner join SessionStart start 
  where start.user.pid = :userPid

But better, in my opinion, of course, to have one table for the session start and end.

class Session {

  @Id
  @GeneratedValue
  @Column  
  private pid;

  @ManyToOne(fetch = FetchType.LAZY)
  private User user;

  @Column
  private String met; 

}

We need your hibernate mappings to create a HQL for it. Otherwise it will be plain SQL like:

select e.session_met
       from session_start s
       join session_end e on s.session_id = e.session_id
where  s.user_id = ?

for HQL you need to join on property, like

join s.property to session_end on session_start object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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