如何编写带有嵌入id的JPQL SELECT?
我正在使用 Toplink Essentials (JPA) + GlassFish v3 + NetBean 6.9
我有一张带有复合主键的表:
table (machine)
----------------
|PK machineId |
|PK workId |
| |
|______________|
我创建了 2 个实体类,第一个用于实体本身,第二个是 PK 类。
public class Machine {
@EmbeddedId
protected MachinePK machinePK;
//getter setters of fields..
}
public class MachinePK {
@Column(name = "machineId")
private String machineId;
@Column(name = "workId")
private String workId;
}
现在.. 如何使用 JPQL 和 WHERE 编写 SELECT 子句???
这会失败。
SELECT m FROM Machine m WHERE m.machineId = 10
http://www.mail-archive.com/[email protected]/msg03073.html
根据网页,添加“val”?不,它也失败了。
SELECT m FROM Machine m WHERE m.machineId.val = 10
在这两种情况下,错误都是:
Exception Description: Error compiling the query
[SELECT m FROM Machine m WHERE m.machineId.val = 10],
line 1, column 30: unknown state or association field
[MachineId] of class [entity.Machine].
I'm using Toplink essentials (JPA) + GlassFish v3 + NetBean 6.9
I have one table with composite primary key:
table (machine)
----------------
|PK machineId |
|PK workId |
| |
|______________|
I created 2 entity classes one for entity itself and second is PK class.
public class Machine {
@EmbeddedId
protected MachinePK machinePK;
//getter setters of fields..
}
public class MachinePK {
@Column(name = "machineId")
private String machineId;
@Column(name = "workId")
private String workId;
}
Now.. how do I write SELECT clause with JPQL with WHERE???
This fails.
SELECT m FROM Machine m WHERE m.machineId = 10
http://www.mail-archive.com/[email protected]/msg03073.html
According to the web page, add "val"? No it fails too.
SELECT m FROM Machine m WHERE m.machineId.val = 10
In both case, the error is:
Exception Description: Error compiling the query
[SELECT m FROM Machine m WHERE m.machineId.val = 10],
line 1, column 30: unknown state or association field
[MachineId] of class [entity.Machine].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Hibernate 4.1 和 JPA 2.0 进行测试
进行以下更改才能工作:
Class Machine.java
Class MachinePK.java
...以及JPQL 查询传递所有列名称:
Tested with Hibernate 4.1 and JPA 2.0
Make the following changes in order to work:
Class Machine.java
Class MachinePK.java
...and for the JPQL query pass all column names:
如果使用注释 @Query,您可以使用元素 nativeQuery = true。
If you use annotation @Query, you can use element nativeQuery = true.