I've got a shopping application with customers. Those customers have a cart containing products. Here's the (simplified) mapping with embeddables I used:
@Entity
class Customer {
@Embedded
private Cart cart;
// Constructors, setters, equals... omitted
}
@Embeddable
class Cart {
@ElementCollection
@JoinTable(name = ...)
private Set<Product> products = new HashSet<>();
...
}
@Embeddable
class Product {
private String name;
...
}
Now, I'm trying to get a customer's cart with HQL. I know I can't directly query embeddables, but nothing prevents me from selecting it as a field:
select c.cart from Customer c where c.id = 1
but this results in the following exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at org.hibernate.hql.ast.util.ColumnHelper.generateScalarColumns(ColumnHelper.java:62)
at org.hibernate.hql.ast.tree.DotNode.setScalarColumnText(DotNode.java:661)
at org.hibernate.hql.ast.tree.SelectClause.renderScalarSelects(SelectClause.java:359)
...
I could overcome this problem by just retrieving the customer and traverse its getCart() method, but I wonder if this issue is caused by an incorrect mapping or something like that. Thanks a lot!