Reputation: 7084
I have 2 entities:
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString(exclude = "products")
@Entity
@Table(name = "categories")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String categoryName;
private String categoryType;
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
private List<Product> products;
}
and second entity:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String productName;
private String productDescription;
@ManyToOne()
private Category category;
}
And I have 2 repository for each entity:
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
}
@Repository
public interface CategoryRepository extends JpaRepository<Category, Long> {
}
Then I send this JSON to cintroller and I heed save all Product:
{
"products": [{
"productName": "product1",
"productDescription": "productDescription1",
"category": {
"categoryName": "catName1",
"categoryType": "type1"
}
},
{
"productName": "product2",
"productDescription": "productDescription2",
"category": {
"categoryName": "catName1",
"categoryType": "type1"
}
},
{
"productName": "product3",
"productDescription": "productDescription3",
"category": {
"categoryName": "catName2",
"categoryType": "type2"
}
}]
}
And Then I need save it:
@PostMapping(value = "test")
public Category test(@RequestBody() RequestTest products) {
List<Product> productList = products.getProducts();
for (Product product : productList) {
//save all
}
}
category not have id and when I get this JSON I need create all products and set category, but I have not category id and if I do next:
for (Product product : productList) {
Category category = product.getCategory();
Category save = categoryRepository.save(category);
product.setCategory(save);
productRepository.save(product);
}
I create new ctegore for each product and I get this:
49 catName1 type1
51 catName1 type1
53 catName2 type2
cat_id
50 productDescription1 product1 49
52 productDescription2 product2 51
54 productDescription3 product3 53
but I need:
49 catName1 type1
51 catName2 type2
cat_id
50 productDescription1 product1 49
52 productDescription2 product2 49
54 productDescription3 product3 51
Upvotes: 0
Views: 194
Reputation: 589
First insert all the unique categories and check for same exist in database. Make sure categories should be unique.
Upvotes: 0
Reputation: 26522
In my opinion the best way would be to keep track of saved categories by introducing a Map.
Assuming that categoryName
is a unique column then you could do something following:
Map<String,Category> categoryMap = new HashMap<>();
for (Product product : productList) {
String categoryName = product.getCategory().getCategoryName();
Category category = categoryMap.get(categoryName);
if(category == null){
category = categoryRepository.save(product.getCategory());
categoryMap.put(categoryName,category);
}
product.setCategory(save);
productRepository.save(product);
}
Upvotes: 1
Reputation: 823
You might want to find first if the category object already exists.
You could create a query that searches if a category exists by the same name and type. If it exists, use that object, which will have the ID too, or else go on with your current code.
Upvotes: 1