0

我有一个休眠查询的问题,它按从共同父级继承的多个实体的列进行过滤。

这是示例:

抽象父:

@Entity
@Inheritance (strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AItem 
{
        /** The id. */
        @Id
        @Column (name = "pk_id")
        private Long id;

        /** The date of creational. */
        @Column (name = "c_date_created", nullable = true, columnDefinition = "timestamp with time zone")
        private Calendar dateCreated;

        /** The date of change. */
        @Column (name = "c_date_changed", nullable = true, columnDefinition = "timestamp with time zone")
        private Calendar dateChanged;

        /** Name of item. */
        @Column (name = "c_name", nullable = false)
        private String name;


        /** type of item. */
        @Column (name = "c_type", nullable = false)
        @Enumerated (EnumType.STRING)
        EItemType type;
}

孩子们:

@Entity
@Table (name = "t_product", schema = "am")
public class CProduct extends AItem
{

        @ManyToMany (fetch = FetchType.LAZY, mappedBy = "products")
        private Set<CCampaign> campaigns;

    ...some more attributes...
}
@Entity
@Table (name = "t_service", schema = "am")
public class CService extends AItem
{

        @ManyToMany (fetch = FetchType.LAZY, mappedBy = "services")
        private Set<CCampaign> campaigns;

    ...some more attributes...
}

活动:

@Entity
@Table (name = "t_campaign", schema = "am")
public class CCampaign
{
        /** The id. */
        @Id
        @SequenceGenerator (name = "T_CAMPAIGN_PKID_GENERATOR", sequenceName = "AM.T_CAMPAIGN_PK_ID_SEQ")
        @GeneratedValue (strategy = GenerationType.AUTO, generator = "T_CAMPAIGN_PKID_GENERATOR")
        @Column (name = "pk_id")
        private Long id;

        /**
        * Products available in the campaign.
        */
        @ManyToMany (fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.MERGE})
        @JoinTable (name = "t_x_campaign_product", schema = "am", joinColumns = {@JoinColumn (name = "pk_campaign", nullable = false, updatable = false)}, inverseJoinColumns = {@JoinColumn (name = "pk_item", nullable = false, updatable = false)})
        private Set<CProduct> products;

        /**
        * services available in the campaign.
        */
        @ManyToMany (fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.MERGE})
        @JoinTable (name = "t_x_campaign_service", schema = "am", joinColumns = {@JoinColumn (name = "pk_campaign", nullable = false, updatable = false)}, inverseJoinColumns = {@JoinColumn (name = "pk_item", nullable = false, updatable = false)})
        private Set<CService> services;
}

我希望为该活动返回所有项目(服务+产品)。

我有疑问

select distinct item from AItem item left join item.campaigns camp where camp.id = 5

现在hibernate所做的是它返回所有相关的产品,而不是单一的服务。

有为活动分配的服务。

当我在没有指定 camp.id 的情况下运行查询时,查询将返回服务。

有人可以帮助解决这个问题吗?

4

1 回答 1

0

使用 Hibernate 过滤器将解决您的问题,请参见此处查看此 注释的问题答案以过滤 @OneToMany 关联的结果

于 2013-04-15T08:37:48.867 回答