How to write JPQL SELECT with embedded id?
Asked Answered
P

3

54

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].
Periodical answered 13/1, 2011 at 4:53 Comment(0)
D
96
SELECT m FROM Machine m WHERE m.machinePK.machineId = 10
Dicephalous answered 13/1, 2011 at 6:7 Comment(4)
How to convert that to JPA Criteria?Herbst
SELECT m FROM Machine m WHERE m.machinePK.machineId = :criteriaGuidepost
if using HQL could you also do SELECT m FROM Machine m WHERE m.machinePK in (:listOfKeys) ? #30096176Godgiven
i have a situation where the fields in the embeddable class is also other user defined classes and i have to evaluate a field in that user defined class. any idea how to do that? @Query("SELECT e from ElectionContestant e where e.primaryKey.contestant.contestId=?#{0} AND e.primaryKey.election.elecId=?#{1}")Apomict
B
6

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());
}
Benzoyl answered 19/7, 2016 at 14:21 Comment(0)
P
0

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

Parham answered 15/11, 2016 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.