我有以下两个jpa hibernate实体,
@Entity
public class Product {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name="uuid", strategy="uuid2")
private String id;
@ManyToOne
private Type type;
@ManyToOne
private Attribute attribute;
}
和
@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;
}
我正在尝试在产品系列中生成类型和属性字段,作为家庭产品的类型和属性的集合。 (注意,类型和属性类本身就是实体)
不允许使用以下公式,因为我得到以下内容
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
这似乎表明将公式的结果映射到一组实体的问题
这可能与公式有关吗?如果是这样的话?
如果没有,有没有一种标准的方法来做这种事情?如果不是,你会建议做什么更好的方法。
最后,我希望尽可能使用jpa,但正如我正在研究公式我已经开放使用hibernate特定的解决方案
答案 0 :(得分:0)
5年前,但对于今天读此书的人来说:
在@Formula
附近,您应该添加:
@ElementCollection(targetClass = X.class)
,其中X是集合对象的类型。