带有@MappedSuperclass的Hibernate TABLE_PER_CLASS不会创建UNION查询 [英] Hibernate TABLE_PER_CLASS with @MappedSuperclass won't create UNION query
问题描述
我正在尝试创建一系列对象,这些对象都存储在单独的表中,但是所有这些表上都有一组共同的字段.我希望Hibernate对所有这些表都做一个UNION,但不要将超类包含在表中.
I am trying to create a series of objects that all are stored in separate tables, but that there is a set of fields in common on all these tables. I want Hibernate to do a UNION of all these tables, but NOT INCLUDE the superclass as a table.
当我用以下方法注释超类时:@MappedSuperclass@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
When I annotate the superclass with just: @MappedSuperclass @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
... hibernate将针对超类的(n)个子类型分别运行(n)个查询.
...hibernate will run (n) queries separately for the (n) subtypes of the superclass.
当我删除@MappedSuperclass而不是将@Entity放在超类上时,Hibernate会执行精美的UNION查询,但会将超类作为表包含在UNION中(超类没有表).
When I remove @MappedSuperclass and instead put @Entity on the super class, Hibernate WILL do the fancy UNION query, but includes the superclass as a table in the UNION (there is no table for the superclass).
如何在不将父类设为@Entity的情况下将所有子类(不是单独的查询)放到UNION中(因为它不是实体,所以没有用于它的表)?
How do I get it to UNION together all the subclasses (not separate queries) without making the parent class an @Entity (because it's not an entity, there's no table for it)?
推荐答案
我发现将父类标记为抽象"可以解决问题,Hibernate不再将其包含在UNION中,并且 did UNION所有子类.
I found that marking the parent class as "abstract" did the trick, Hibernate no longer included it in the UNION and did UNION together all the sub-classes.
这篇关于带有@MappedSuperclass的Hibernate TABLE_PER_CLASS不会创建UNION查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!