-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Selecting a Collection Property in HQL causes SQLException
PostPosted: Tue Mar 30, 2010 6:35 am 
Newbie

Joined: Tue Mar 30, 2010 6:16 am
Posts: 1
Hello,

I'm having trouble selecting a collection property in HQL. I wrote a special Object which should be created by hibernate and be filled with several properties of the queried entity. This does not work if any of the properties I select is a collection. In the resulting SQL-Query Hibernate will include a dot without any name before or after it which is of course illegal.

Here is an example:

The query Part:

Code:
String queryString = "select new net.portrix.cms.data.NameAndAddress (p.name, p.addresses) from  Person as p";
Query query = session.createQuery(queryString);
return query.list();


The resulting exception looks like this
Code:
Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select person0_.name as col_0_0_, . as col_1_0_ from Person person0_ inner join Address addresses1_ on person0_.id=addresses1_.personId]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

And the SQL as formatted by the oracle SQLDeveloper:
Code:
SELECT person0_.name AS col_0_0_,
  .                  AS col_1_0_
FROM Person person0_
INNER JOIN Address addresses1_
ON person0_.id=addresses1_.personId


The corresponding classes look like this (Getters/Setters/Constructors are omitted as far as possible, but available in my code.)

Code:
@Entity
@SequenceGenerator(
name = "person_SEQ_ID",
sequenceName = "person_seq", allocationSize = 1)
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "person_SEQ_ID")
    long id;
    @Column
    String name;
    @OneToMany
    @JoinColumn(name = "personId")
    Collection<Address> addresses;

    ...
} /* ----------------- End of class Person ----------------- */


@Entity
@SequenceGenerator(
name = "address_SEQ_ID",
sequenceName = "contract_seq", allocationSize = 1)
public class Address {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "address_SEQ_ID")
    long id;
    @Column
    String street;
    @Column
    String city;

...
} /* ----------- end of class Addresss ---------------- */


public class NameAndAddress {
    Collection<Address> addresses;
    String name;

    public NameAndAddress(String name, Collection<Address> addresses) {
        this.name = name;
        this.addresses = addresses;

    }

...

} /* ---------------- end of class NameAndAddress ------------------ */



Is hibernate unable to do such queries? If so why is there not propper error message being created? Is that a bug?
If I remove the collection (p.addresses) in the select, it will work.

Thanks in advance for any assistance.
P.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.