Hibernate HQL - include columns from PK into query

Jan Vladimir Mostert

I've used Spring Roo to reverse engineer an existing database in order to get hibernate modelling on the existing database, in the cases where composite primary keys exist, it generates XyzClass and XyzClassPK.

If I want to do an HQL query on some of the columns that are part of the composite primary key, I get an error, let's say the composite primary key consists of primaryKey1 and primaryKey2:

String hql = "select o from XyxClass o where primaryKey1 = :key1 and nonPrimaryKey = :key2";
    TypedQuery<XyzClass> query = entityManager().createQuery(hql, XyzClass.class);
    query.setParameter(......
    query.setParameter(......
    query.getResultList()

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'primaryKey1' in 'where clause' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2293) at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96) at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:56)

These are the annotations generated by ROO:

@RooJavaBean
@RooToString
@RooJpaActiveRecord(identifierType = XyzClassPK.class, versionField = "", table = "XYZ_CLASS")
@RooDbManaged(automaticallyDelete = true)
public class XyzClass {

And the Primary Key:

@RooIdentifier(dbManaged = true)
public final class XyzClassPK {

    @Column(name = "PRIMARY_KEY1", nullable = false)
    private Integer primaryKey1;

    @Column(name = "PRIMARY_KEY2", nullable = false)
    private Integer primaryKey2;

My HQL is obviously incorrect, how do I include composite primary keys in my hql query?

Julien

Your HQL should look like this:

String hql = "select o from XyxClass o where o.xyz.primaryKey1 = :key1 and o.nonPrimaryKey = :key2";

Where xyz is the name of your Id field in XyxClass (I can't see it in your code..) Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

hibernate HQL Query select

From Dev

Hibernate: create HQL Query with Attribute values from Sets

From Dev

Hibernate : HQL query to directly compare timestamp from database value

From Dev

Hibernate HQL query get data from table associated with another table

From Dev

Hibernate: create HQL Query with Attribute values from Sets

From Dev

HQL Query fails in Hibernate MySQL

From Dev

Hibernate HQL : no entity found for query

From Dev

Simple HQL Query and Criteria in Hibernate

From Dev

HQL Query fails in Hibernate MySQL

From Dev

Hibernate HQL : no entity found for query

From Dev

Query Hibernate or HQL not return result

From Dev

Hibernate and Oracle DATE column using HQL query

From Dev

How to query join one to many in hibernate HQL

From Dev

HQL equivalent query to criteria query in hibernate many to many relationship?

From Dev

HQL equivalent query to criteria query in hibernate many to many relationship?

From Dev

Can we concatenate two properties in Hibernate HQL query?

From Dev

Hibernate: HQL join query on association doesnt result as expected

From Dev

Will HQL query use Hibernate second-level cache

From Dev

While running Hibernate Criteria, HQL query is not created in given order

From Dev

Hibernate hql, execute multiple update statements in same query

From Dev

Hibernate hql, execute multiple update statements in same query

From Dev

Hibernate LEFT JOIN HQL query not counting correctly with condition

From Dev

How to use parameterized query for LIKE operator in Hibernate HQL?

From Dev

Will HQL query use Hibernate second-level cache

From Dev

Hibernate: HQL join query on association doesnt result as expected

From Dev

How to get the last value of a column using HQL(Hibernate Query Language)

From Dev

HQL Query to get fields from @ElementCollection

From Dev

Django - Query to retrieve pk from other model

From Dev

SQL injection drop/delete from Hibernate HQL to MySQL

Related Related

  1. 1

    hibernate HQL Query select

  2. 2

    Hibernate: create HQL Query with Attribute values from Sets

  3. 3

    Hibernate : HQL query to directly compare timestamp from database value

  4. 4

    Hibernate HQL query get data from table associated with another table

  5. 5

    Hibernate: create HQL Query with Attribute values from Sets

  6. 6

    HQL Query fails in Hibernate MySQL

  7. 7

    Hibernate HQL : no entity found for query

  8. 8

    Simple HQL Query and Criteria in Hibernate

  9. 9

    HQL Query fails in Hibernate MySQL

  10. 10

    Hibernate HQL : no entity found for query

  11. 11

    Query Hibernate or HQL not return result

  12. 12

    Hibernate and Oracle DATE column using HQL query

  13. 13

    How to query join one to many in hibernate HQL

  14. 14

    HQL equivalent query to criteria query in hibernate many to many relationship?

  15. 15

    HQL equivalent query to criteria query in hibernate many to many relationship?

  16. 16

    Can we concatenate two properties in Hibernate HQL query?

  17. 17

    Hibernate: HQL join query on association doesnt result as expected

  18. 18

    Will HQL query use Hibernate second-level cache

  19. 19

    While running Hibernate Criteria, HQL query is not created in given order

  20. 20

    Hibernate hql, execute multiple update statements in same query

  21. 21

    Hibernate hql, execute multiple update statements in same query

  22. 22

    Hibernate LEFT JOIN HQL query not counting correctly with condition

  23. 23

    How to use parameterized query for LIKE operator in Hibernate HQL?

  24. 24

    Will HQL query use Hibernate second-level cache

  25. 25

    Hibernate: HQL join query on association doesnt result as expected

  26. 26

    How to get the last value of a column using HQL(Hibernate Query Language)

  27. 27

    HQL Query to get fields from @ElementCollection

  28. 28

    Django - Query to retrieve pk from other model

  29. 29

    SQL injection drop/delete from Hibernate HQL to MySQL

HotTag

Archive