类在添加到集合时不保留新的关联对象



我正在制作一个餐厅管理应用程序,但我在保持用户关联时遇到问题。对于厨师类,与菜肴类有固定的关联,以便特定菜肴可以与某个厨师相关联。

我创建了一个将菜肴与厨师相关联的方法,当我尝试在我的 REST 客户端上调用它时,该方法似乎有效,它返回带有更新信息的厨师对象的 JSON,但是当我调用 get chef 方法时,JSON 不再显示添加的菜肴项

这是厨师课以及与菜肴对象相关的所有内容

@Table(name="chef")
public class Chef {
//Chef Attributes
@Id private String username;
private String firstName;
private String lastName;
private String email;
private String password;
private String address;
private String bio;
private Boolean delivery;
private String photoURL;

// @OneToOne
// private ChefMenu menu;

@Transient
@OneToMany(mappedBy = "chef", cascade = CascadeType.ALL)
@JsonIgnoreProperties("chef")
private Set<Dish> menuItems;
public Set<Dish> getMenuItems() {
if (this.menuItems == null) {
this.menuItems = new HashSet<Dish>();
}
return this.menuItems;
}

这是菜肴类,其中包含与厨师类相关的所有内容

@Entity
@Table(name="dish")
public class Dish {
//Dish Attributes
@Id private String dishName;
private String cuisine;
private double price;
private String maxQuantity;
private String dietaryRestriction;
private String mealIngredients;
private String cookingTime;
@ManyToOne
@JoinColumn(name = "chef")
@JsonIgnoreProperties({"menuItems","orders","firstName", "lastName", "email", "bio", "password", "address", "delivery", "photoURL"})
private Chef chef;
public void setChef(Chef val) { this.chef = val; }
public Chef getChef() {
return this.chef;
}

这是用于从存储库向厨师添加新菜肴的方法

@Transactional
public Chef addDishToMenu(Chef c, Dish d) {
c.addDish(d);
entityManager.merge(c);
return c;
}

最后是来自控制器类的代码:

@PostMapping("dish/create/{dishName}/{cuisine}/{price}/{maxQuantity}/{dietaryRestriction}/{mealIngredients}/{cookingTime}")
public ResponseEntity createDish(String username,
@PathVariable("dishName") String dishName, @PathVariable("cuisine") String cuisine,
@PathVariable("price") String price, @PathVariable("maxQuantity") String maxQuantity,
@PathVariable("dietaryRestriction") String dietaryRestriction,
@PathVariable("mealIngredients") String mealIngredients, @PathVariable("cookingTime") String cookingTime)
{
Dish d = new Dish();
Double p = Double.parseDouble(price);
//int mQ = Integer.parseInt(maxQuantity);
try {
d = foodRepository.createDish(dishName, cuisine, p, maxQuantity, dietaryRestriction,
mealIngredients, cookingTime);
} catch (InvalidInputException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Response(false, e.getMessage()));
}
return ResponseEntity.status(HttpStatus.OK).body(d);
}

@PostMapping("dish/add/{username}/{dishName}")
public ResponseEntity addDishToMenu(@PathVariable("username") String username, @PathVariable("dishName") String dishName) throws NullObjectException {
Chef c = new Chef();
Dish d = new Dish();
c= chefRepository.getChef(username);
d = foodRepository.getSpecificDish(dishName);
c = foodRepository.addDishToMenu(c, d);
return ResponseEntity.status(HttpStatus.OK).body(c);
}
@GetMapping("/get/{username}")
public ResponseEntity getChef(@PathVariable("username") String username) {
// List<AppUser> user;
Chef chef = new Chef();
try {
chef = chefRepository.getChef(username);
// user = userRepository.getAppUserQuery(username);
} catch (NullObjectException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Response(false, e.getMessage()));
}
return ResponseEntity.status(HttpStatus.OK).body(chef);// user.get(0));
}

因此,当我调用我的 Rest 客户端向厨师添加一道菜时,我得到以下响应:

{
"username": "Favreau4",
"firstName": "Jon",
"lastName": "Favreau",
"email": "chefFavreau@email.com",
"password": "j+9UECq/PLA=$I+HsXngf/b82+rMtPQQO",
"address": null,
"bio": null,
"delivery": null,
"photoURL": null,
"menuItems": [
{
"dishName": "Big sandwich",
"cuisine": "american",
"price": 5,
"maxQuantity": "3",
"dietaryRestriction": "bacon",
"mealIngredients": "bacon,lettuce,tomato,bread",
"cookingTime": "10mins"
}
],
"order": [],
}

但是当我使用 getChef REST 调用时,我得到这个:

{
"username": "Favreau4",
"firstName": "Jon",
"lastName": "Favreau",
"email": "chefFavreau@email.com",
"password": "j+9UECq/PLA=$I+HsXngf/b82+rMtPQQO",
"address": null,
"bio": null,
"delivery": null,
"photoURL": null,
"menuItems": [],
"order": [],
}

有谁知道如何解决这个问题?

你知道@Transient注释吗?瞬态用于将变量标记为不可抗拒。因此,您的菜单项不会持久化或保存在数据库中。

最新更新