@Entity
public class Product {
@Id
@Column(unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
@OneToOne
private Brand brand;
@OneToOne(fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.CASCADE)
private Category category;
}
@Entity
public class Category {
@Id
@Column(unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotBlank(message = "Please enter category name!")
@Length(max = 50, message = "Maximum size exceeded!")
private String name;
@OneToOne
private Category parent;
}
Is possible to create recursive query that will loop trough all category parents of product?
Let's say I'm in category For Woman, it will loop trough all categories parents and if one of it matches For Woman it will return products of it..
Basically, what i want is something like this(query of it)..
List<ProductOption> productOptions = new ArrayList<>();
Category mainCategory = categoryRepository.findById(id);
for(Category category : categoryRepository.findAll()){
Category cat = category;
while(cat!=null){
if(cat==mainCategory){
productOptions.addAll(productOptionRepository.findAllByProduct_CategoryId(category.getId()));
}
cat=cat.getParent();
}
}
