简体   繁体   English

如何使用Hibernate或JPA动态生成实体集合?

[英]How can i generate dynamically collections of entities with hibernate or jpa?

i have the following two jpa hibernate entities, 我有以下两个JPA休眠实体,

@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 最后,我会尽可能地选择jpa,但在查看公式时,我愿意使用休眠特定的解决方案

5 years ago, but for people reading this nowadays : 5年前,但对于今天阅读此书的人来说:

Near @Formula , you should add : @ElementCollection(targetClass = X.class) where X is the type of object of the collection. @Formula附近,您应该添加: @ElementCollection(targetClass = X.class)其中X是集合对象的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM