我試圖創建一個到多個關係的JPQL,即用戶和設備爲1個用戶可以有一個到多個設備。如果設備屬於用戶且設備名稱正確,我想查找設備的整個對象。JPQL爲一對多關係
如果它是一個SQL查詢的話,我可以做的查詢只對設備如下:
select * from DEVICE where USER_ID = 2 and DEVICENAME = "mypc";
其中USER_ID是在設備表的外鍵。但是我怎麼能爲用戶和設備表做JPQL查詢呢?下面是用戶和設備類
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
public int id;
public String username;
public String firstname;
public String lastname;
public String hashPassword;
@OneToMany(mappedBy = "user")
public List<Device> devices = new ArrayList<Device>();
}
@Entity
public class Device {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
public int id;
public String deviceName;
@ManyToOne
public User user;
String deviceOS;
String deviceType;
String attributes;
boolean standard;
}
還有,你試過嗎?你有沒有閱讀關於JPQL的文檔? http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#hql-implicit-join –