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.