如何编写带有嵌入id的JPQL SELECT?

发布于 2024-10-11 22:15:07 字数 1319 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狂之美人 2024-10-18 22:15:07
SELECT m FROM Machine m WHERE m.machinePK.machineId = 10
SELECT m FROM Machine m WHERE m.machinePK.machineId = 10
却一份温柔 2024-10-18 22:15:07

使用 Hibernate 4.1 和 JPA 2.0 进行测试

进行以下更改才能工作:

Class Machine.java

public class Machine {
   @EmbeddedId
   protected MachinePK machinePK;

   //getter & setters...
}

Class MachinePK.java

@Embeddable
public class MachinePK {
    @Column(name = "machineId")
    private String machineId;

    @Column(name = "workId")
    private String workId;

    //getter & setters...
}

...以及JPQL 查询传递所有列名称:

Query query = em.createQuery("SELECT c.machinePK.machineId, c.machinePK.workId, "
                + "FROM Machine c WHERE c.machinePK.machineId=?");
query.setParameter(1, "10");

Collection results = query.getResultList();

Object[] obj = null;
List<MachinePK> objPKList = new ArrayList<MachinePK>();
MachinePK objPK = null;
Iterator it = results.iterator();
while(it.hasNext()){
    obj = (Object[]) it.next();
    objPK = new MachinePK();
    objPK.setMachineId((String)obj[0]);
    objPK.setWorkId((String)obj[1]);
    objPKList.add(objPK);
    System.out.println(objPK.getMachineId());
}

Tested with Hibernate 4.1 and JPA 2.0

Make the following changes in order to work:

Class Machine.java

public class Machine {
   @EmbeddedId
   protected MachinePK machinePK;

   //getter & setters...
}

Class MachinePK.java

@Embeddable
public class MachinePK {
    @Column(name = "machineId")
    private String machineId;

    @Column(name = "workId")
    private String workId;

    //getter & setters...
}

...and for the JPQL query pass all column names:

Query query = em.createQuery("SELECT c.machinePK.machineId, c.machinePK.workId, "
                + "FROM Machine c WHERE c.machinePK.machineId=?");
query.setParameter(1, "10");

Collection results = query.getResultList();

Object[] obj = null;
List<MachinePK> objPKList = new ArrayList<MachinePK>();
MachinePK objPK = null;
Iterator it = results.iterator();
while(it.hasNext()){
    obj = (Object[]) it.next();
    objPK = new MachinePK();
    objPK.setMachineId((String)obj[0]);
    objPK.setWorkId((String)obj[1]);
    objPKList.add(objPK);
    System.out.println(objPK.getMachineId());
}
简单 2024-10-18 22:15:07

如果使用注释 @Query,您可以使用元素 nativeQuery = true。

If you use annotation @Query, you can use element nativeQuery = true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文