JPA Mapper
One to Many
Class Category
@Entity(name = "Category")
public class Category {
private short id;
private String name;
@OneToMany(mappedBy = "category")
private List<Product> products;
}
Class Product
@Entity(name = "Product")
public class Product {
@Id
private long id;
private String title;
@ManyToOne
@JoinColumn(name = "CategoryId")
private Category category;
}
JPA where and pagination
public interface ProductRepository extends PagingAndSortingRepository<Product, Long>, CrudRepository<Product, Long>{
List<Product> findAllBySubCategoryId(short subCategoryId, Pageable pageable);
List<Product> findAllBySubCategoryAndIdNot(SubCategory subCategory, long id, Pageable pageable);
@Query(nativeQuery = true, value = "SELECT p.* FROM Product AS p JOIN SubCategory AS s ON p.SubCategoryId = s.SubCategoryId AND s.CategoryId = :id")
List<Product> findAllByCategory(short id, Pageable pageable);
}