Hibernate InheritanceType.JOINED标准投影和

我试图使用Hibernate Inheritance在来自多个子类的同类表上对值进行求和。

在我的父类上使用“@Inheritance(strategy = InheritanceType.JOINED)”,并有多个子类来扩展它。 我的问题是,当我使用Criteria和“Projections.sum”来获得结果时,Hibernate做了一个有趣的连接。

它就好像hibernate采用第一个匹配结果并对它进行求和 - 忽略其余部分,我甚至不知道这是否可以完成。

希望我的代码能够澄清我的问题。

动物 - 父类

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "animals")
public class Animals extends BaseObject{

    private Long animalId;
    private Long weight;

猫 - 儿童班

@Entity
@Table(name = "cats")
@PrimaryKeyJoinColumn(name="animal_id")
public class Cats extends Animals{

    private Long offspring;
    private String color;

狗 - 儿童班

@Entity
@Table(name = "dogs")
@PrimaryKeyJoinColumn(name="animal_id")
public class Dogs extends Animals{

    private Long offspring;
    private String color;
    private int eggsLayed;

鸟 - 儿童班

@Entity
@Table(name = "birds")
@PrimaryKeyJoinColumn(name="animal_id")
public class Birds extends Animals {

    private Long offspring;
    private String color;
    private int eggsLayed;

鱼 - 儿童班

@Entity
@Table(name = "fish")
@PrimaryKeyJoinColumn(name="animal_id")
public class Fish extends Animals {

    private Long offspring;
    private String color;
    private int eggsLayed;

标准查询

    ProjectionList projList = Projections.projectionList()
            .add(Projections.sum("eggsLayed"));

    Criteria crit = getSession()
            .createCriteria(Animals.class)
            .setProjection(projList);

运行查询时的相关SQL输出:

Hibernate: 
    select
        sum(this_3_.eggs_layed) as y0_ 
    from
        animals this_ 
    left outer join
        cats this_1_ 
            on this_.animal_id=this_1_.animal_id 
    left outer join
        dogs this_2_ 
            on this_.animal_id=this_2_.animal_id 
    left outer join
        birds this_3_ 
            on this_.animal_id=this_3_.animal_id 
    left outer join
        fish this_4_ 
            on this_.animal_id=this_4_.animal_id

任何反馈将不胜感激。

链接地址: http://www.djcxy.com/p/36997.html

上一篇: Hibernate InheritanceType.JOINED Criteria Projection sum

下一篇: Hibernate Query Association with Criteria: 'and' on children?