Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

java - How to write JPQL SELECT with embedded id?

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].
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
SELECT m FROM Machine m WHERE m.machinePK.machineId = 10

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...