I'm using jpa and I have the following entity:
@Entity
@Table(name="favorites_folders")
public class FavoritesFolder {
private static final long serialVersionUID = 1L;
@Id
private String id;
@NotNull
@Size(min = 1, max = 50)
public String name;
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(
name="favorites_products",
joinColumns=@JoinColumn(name="folder_id")
)
@Column(name="product_id")
@NotNull
private Set<String> productsIds = new HashSet<String>();
}
What I want to do is to get a set of FavoritesFolder
entities that contains the string "favorite-id" in their productsIds
member set.
Does anyone know how can it be done in criteria api?
Update:
I'm thinking the following sql should do the trick but I'm not sure how to do it in either JPQL
or Criteria API
:
select * from favorites_folders join favorites_products on favorites_folders.id = favorites_products.folder_id where favorites_products.product_id = 'favorite-id'
To get a set of FavoritesFolder entities that contains the string "favorite-id" in their productsIds member set using criteria api you should do the following:
More information on Collections in JPQL and Criteria Queries.
Just another way using IN
Then you can write a Criteria query like
This is my work around that works. I'm using Springboot 1.5.9. I don't have time to identify the root cause. What I know is such nested property been ignored when get through
JacksonMappingAwareSortTranslator
. So what I did to workaround this is not to use Sort object created by resolvers. Here's my code inKotlin
. Without doing this, thepageable.sort
isnull
and sorting does not work. And my code will create a newPageRequest
object that has non-nullsort
that works.