[code]
@Entity
class Sample {
@EmbeddedId
Key id;
}
[/code]
[code]
@Embeddable
class Key {
someProperty;
otherProperty;
}
[/code]
Everything works fine when I do:
[code]
select count(*) from Sample s
[/code]
But problems arise with a query like:
[code]
select count(s) from Sample s
[/code]
In this latter example, since Sample has a composite key, the generated sql looks like:
[code]
select count(sample_table.some_property, sample_table.other_property)
[/code]
On postgres, you'd receive an error like:
[quote]
ERROR: function count(<some_type>, <other_type>) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You may need to add explicit type casts.
Character: 11
[/quote]
Obviously, for this particular example, I can use the count(*) syntax and be just fine, but I can't do that in the original context this problem comes from. In that other context, this count is part of a larger select/groupby statement which would make the count(*) syntax ambiguous.
If I use count(s.id.someProperty) or count(s.id.otherProperty), it generates sql that is valid and runs, but it doesn't solve the problem because neither of those single properties uniquely identifies the row.
So my question: is there a way to count entities with composite primary keys in a database-agnostic way in HQL?
|