I'm beginner with JPA and Spring. Also my first question in here. So, sorry for my mistakes. I'm practicing with simple scenario for beginning. I have two entities as Product and Category with bi-directional many-to-one/one-to-many association.
Category class :
@Entity
@Table(name = "categories")
@NoArgsConstructor
@AllArgsConstructor
@Data
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
public class Category implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name", length = 50, nullable = false)
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "category")
private List<Product> products = new ArrayList<Product>();
}
Product class :
@Table(name = "products")
@NoArgsConstructor
@AllArgsConstructor
@Data
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
public class Product implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "title", nullable = false, length = 50)
private String title;
@Column(name = "price", precision = 4, scale = 2, nullable = false)
private Double price;
@Column(name = "quantity", nullable = false)
private int quantity;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", foreignKey = @ForeignKey(name = "FK_product_category"))
private Category category;
}
Both of CategoryRepository and ProductRepository are implementing from JpaRepository. There are few method signatures with @Query annotations.
Services using repositories with @Autowired. There aren't any business logic.
CategoryService class :
@Transactional
public class CategoryService implements IRepositoryService<Category, Long> {
private ICategoryRepository categoryRepository;
@Autowired
public CategoryService(ICategoryRepository categoryRepository) {
this.categoryRepository = categoryRepository;
}
@Override
public List<Category> findAllOrderById() {
return categoryRepository.findAllOrderById();
}
@Override
public Category findById(Long id) {
return categoryRepository.findById(id).orElseThrow(EntityNotFoundException::new);
}
@Override
public List<Category> findByForeignKey(Long categoryId) {
return null;
}
@Override
public void add(Category category) {
category.getProducts().forEach(product -> product.setCategory(category));
categoryRepository.save(category);
}
@Override
public void update(Category category) {
categoryRepository.save(category);
}
@Override
public void deleteById(Long id) {
categoryRepository.deleteById(id);
}
ProductService class :
@Transactional
public class ProductService implements IRepositoryService<Product, Long>{
@Autowired
private IProductRepository productRepository;
@Override
public List<Product> findAllOrderById() {
return productRepository.findAllOrderById();
}
@Override
public Product findById(Long id) {
return productRepository.findById(id).orElseThrow(EntityNotFoundException::new);
}
@Override
public List<Product> findByForeignKey(Long categoryId) {
return productRepository.findByCategoryId(categoryId);
}
@Override
public void add(Product entity) {
productRepository.save(entity);
}
@Override
public void update(Product entity) {
productRepository.save(entity);
}
@Override
public void deleteById(Long id) {
productRepository.deleteById(id);
}
}
At final, both entities have seperate restcontroller classes. I'm adding a category from Postman with only name, so product is null (This part is normal). But when I add a product from Postman or another frontend app, product's category is not set (I'm setting category_id with json). But in database, products table's category_id column values are null. json string
Also I've problem with lazy fetch type and json problem. But main problem comes first.
Thank you for any help.