Reputation: 369
i have the following two jpa hibernate entities,
@Entity
public class Product {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name="uuid", strategy="uuid2")
private String id;
@ManyToOne
private Type type;
@ManyToOne
private Attribute attribute;
}
and
@Entity
public class ProductFamily {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name="uuid", strategy="uuid2")
private String id;
@ManyToMany
@Formula("("
+ " SELECT t.id "
+ " FROM Type t "
+ " WHERE t.id IN ( "
+ " SELECT p.type_id "
+ " FROM product p"
+ " WHERE p.family_id = id"
+ " ) "
+ " order by t.value asc "
+ " )")
private Set<Type> types;
@ManyToMany()
@Formula("("
+ " SELECT a.id "
+ " FROM Attribute a "
+ " WHERE a.id IN ( "
+ " SELECT p.attribute_id "
+ " FROM product p"
+ " WHERE p.family_id = id"
+ " ) "
+ " order by a.value asc "
+ " )")
private Set<Attribute> attributes;
@OneToMany(mappedBy="family")
@LazyCollection(LazyCollectionOption.FALSE)
private Set<Product> products;
}
i am trying to generate the types and attributes fields in the product family as sets of the types and attributes of the family's products. (note, the type and attribute classes are themselves entities)
the following formula are not allowed as i get the following
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1225)
... 51 more
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: ProductFamily, for columns: [org.hibernate.mapping.Formula( ( SELECT t.id FROM Type t WHERE t.id IN ( SELECT p.type_id FROM product p WHERE p.family_id = id ) ) )]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:336)
... 56 more
which seems to indicate a problem with mapping the result of the formula to a set of entities
is this possible with formulae? if so how?
if not, is there a standard way to do this sort of thing? if not what would you recommend as a better way to do it.
lastly, i would prefer jpa wherever possible, but as I'm looking at formula already i am open to using hibernate specific solutions
Upvotes: 2
Views: 1751
Reputation: 206
5 years ago, but for people reading this nowadays :
Near @Formula
, you should add :
@ElementCollection(targetClass = X.class)
where X is the type of object of the collection.
Upvotes: 3