Java 类在添加到集合时不持久化新的关联对象
我正在制作一个餐厅管理应用程序,但我在坚持用户关联方面遇到了问题。对于Chef类,有一组与Dish类的关联,以便特定的菜肴可以与特定的厨师关联 我创建了一个将菜肴与厨师关联的方法,当我尝试在我的REST客户端上调用它时,该方法似乎有效,它返回一个带有更新信息的厨师对象的JSON,但是当我调用get chef方法时,JSON不再显示添加的菜肴项 这是chef类以及与Dish对象相关的所有内容Java 类在添加到集合时不持久化新的关联对象,java,json,spring-boot,jpa,persistence,Java,Json,Spring Boot,Jpa,Persistence,我正在制作一个餐厅管理应用程序,但我在坚持用户关联方面遇到了问题。对于Chef类,有一组与Dish类的关联,以便特定的菜肴可以与特定的厨师关联 我创建了一个将菜肴与厨师关联的方法,当我尝试在我的REST客户端上调用它时,该方法似乎有效,它返回一个带有更新信息的厨师对象的JSON,但是当我调用get chef方法时,JSON不再显示添加的菜肴项 这是chef类以及与Dish对象相关的所有内容 @Table(name="chef") public class Chef { //Chef A
@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;
}
以下是用于从存储库向厨师添加新菜的方法
@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));
}
但是,当我使用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 annotation吗?瞬态用于将变量标记为不可预测。因此,您的菜单项没有得到持久化或保存在数据库中。是的,谢谢,我去掉了临时注释,它开始出现,我想我对@transient注释的作用有错误的想法。非常感谢。
{
"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": [],
}
{
"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": [],
}