Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]. 
like image 403
Meow Avatar asked Jan 13 '11 04:01

Meow


People also ask

What is embedded ID?

The EmbeddedId annotation is applied to a persistent field or property of an entity class or mapped superclass to denote a composite primary key that is an embeddable class. The embeddable class must be annotated as Embeddable .

How do I create a JPQL query?

Creating Queries in JPQLQuery createQuery(String name) - The createQuery() method of EntityManager interface is used to create an instance of Query interface for executing JPQL statement.

Is JPQL simpler than SQL?

JPQL syntax is very similar to the syntax of SQL. Having SQL like syntax is an advantage because SQL is a simple structured query language and many developers are using it in applications. SQL works directly against relational database tables, records and fields, whereas JPQL works with Java classes and instances.

Can embeddable class be used as primary key?

Introduction to Composite Primary Keys and Primary Key Classes. The EJB 3.0 specification allows you to define a primary key class as a @Embeddable and use it as the primary key of your Entity bean. One or more properties can be used as members of the primary key for that particular table.


2 Answers

SELECT m FROM Machine m WHERE m.machinePK.machineId = 10 
like image 128
Adeel Ansari Avatar answered Sep 21 '22 14:09

Adeel Ansari


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()); } 
like image 20
Marco Avatar answered Sep 20 '22 14:09

Marco