0

I have a problem with hibernate query, that filters by columns from multiple entities inherited from common parent.

Here is example:

abstract parent:

@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;
}

children:

@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...
}

ccampaign:

@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;
}

I want all items (services+products) returned for that campaign.

I have query

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

now what hibernate does is it returns all related products,but not single service.

there are assigned services to campaign.

when i run query without specifiing camp.id then query will return services .

can somone help solve this?.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MarekM
  • 1,426
  • 17
  • 14

1 Answers1

0

Using of Hibernate filter will solver your problem, see here See the questions answer of this annotation to filter results of a @OneToMany association

Community
  • 1
  • 1
Krushna
  • 5,059
  • 5
  • 32
  • 49
  • this is not what I need. I want someway to query this entities in a Query. filters only adds AND or OR conditions to HQL. i need fix that existing query. – MarekM Apr 15 '13 at 09:03