Hibernate InheritanceType.JOINED Criteria Projection sum

Im trying to sum value's on simular tables from multiple child classes using Hibernate Inheritance.

Using "@Inheritance(strategy = InheritanceType.JOINED)" on my parent class, and have multiple subclasses extending this. My problem is when I use Criteria with "Projections.sum" to get a result, Hibernate does a funny join.

Its as if hibernate takes the first matching result and does a summation on it - ignoring the rest, im not even sure if this can be done.

Hope my code below will clarify my problem.

Animals - Parent class

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

    private Long animalId;
    private Long weight;

Cats - Child class

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

    private Long offspring;
    private String color;

Dogs - Child class

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

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

Birds - Child class

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

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

Fish - Child class

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

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

Criteria Query

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

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

Related SQL output when running query:

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

Any feedback will be appreciated.

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

上一篇: Hibernate在访问关联实体的ID时生成SQL查询

下一篇: Hibernate InheritanceType.JOINED标准投影和