0

I have a Many-to-many mapping of 2 Tables. Its fine actually in normal case,But I have a case to not show some element from one table Based of conditional flag.

But I am not getting the required Output.

Here are Codes.

@Entity

@Table(name = "Grocery") public class GroceryItemEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long grocery_id;

private String name;
private String description;

private double price;
private boolean isActive;
private Date createdDate;
private Date lastModifiedBy;

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "store_id")
@JsonIgnoreProperties(value = "groceryItem", allowSetters = true)
private StoreEntity store;




@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(name = "KeyWord_Grocery", joinColumns = @JoinColumn(name = "grocery_id"), inverseJoinColumns = @JoinColumn(name = "keyword_id"))
private Set<KeyWordEntity> keyWord;

@Entity public class KeyWordEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long keyword_id;

private String name;
private String description;

@ManyToMany(fetch = FetchType.LAZY,mappedBy = "keyWord",cascade =
     {CascadeType.PERSIST, CascadeType.MERGE})
private Set<GroceryItemEntity> groceryItem;

Its working fine in normal case. But This method not returning Desire output

//This Method will is used for search 
@Query("SELECT Ke FROM KeyWordEntity Ke  JOIN Ke.groceryItem Ge   WHERE Ke.keyword_id in :id AND Ge.isActive is true ")
public  List<KeyWordEntity> findByIds(@Param("id") List<Long> id);

This is the Hibernate generated SQL

Hibernate: select keywordent0_.keyword_id as keyword_1_2_, keywordent0_.description as descript2_2_, keywordent0_.name as name3_2_ from key_word_entity keywordent0_ inner join key_word_grocery groceryite1_ on keywordent0_.keyword_id=groceryite1_.keyword_id inner join grocery groceryite2_ on groceryite1_.grocery_id=groceryite2_.grocery_id where (keywordent0_.keyword_id in (?)) and groceryite2_.is_active=1
Hibernate: select groceryite0_.keyword_id as keyword_2_1_0_, groceryite0_.grocery_id as grocery_1_1_0_, groceryite1_.grocery_id as grocery_1_0_1_, groceryite1_.created_date as created_2_0_1_, groceryite1_.description as descript3_0_1_, groceryite1_.is_active as is_activ4_0_1_, groceryite1_.last_modified_by as last_mod5_0_1_, groceryite1_.name as name6_0_1_, groceryite1_.price as price7_0_1_, groceryite1_.store_id as store_id8_0_1_ from key_word_grocery groceryite0_ inner join grocery groceryite1_ on groceryite0_.grocery_id=groceryite1_.grocery_id where groceryite0_.keyword_id=?
Hibernate: select storeentit0_.store_id as store_id1_3_0_, storeentit0_.created_date as created_2_3_0_, storeentit0_.is247 as is3_3_0_, storeentit0_.is_active as is_activ4_3_0_, storeentit0_.last_modified_by as last_mod5_3_0_, storeentit0_.lattitude as lattitud6_3_0_, storeentit0_.longitude as longitud7_3_0_, storeentit0_.store_description as store_de8_3_0_, storeentit0_.store_name as store_na9_3_0_ from store storeentit0_ where storeentit0_.store_id=?

I want the Restrict the Grocery Table Data where isActive False(0) in Database ,But Its not working.ALl Data are comeing.

Pritam Kumar
  • 77
  • 12
  • Please Help on this.Am I putting Wrong configuration or else. – Pritam Kumar Jun 01 '18 at 19:34
  • I Tried with Hibernate Filter,with Custom Repository ,But Get Run-time exception. I follow Following Link.https://stackoverflow.com/questions/30430187/filters-for-spring-data-jpa# – Pritam Kumar Jun 01 '18 at 19:39

0 Answers0