映射和管理实体的问题,Hibernate
Problem with mapping and managing entity, Hibernate
我有两个实体:菜肴和配料。我想在菜肴中添加配料。
当我使用 @ManyToMany
关系时它起作用(我添加、删除、获取所有含有 table 成分的菜肴 - 我的端点有效),但现在我想在交叉 [= 中添加额外的列41=] DishIngredient
.
所以我所做的是:
- 删除
@ManyToMany
,添加 @OneToMany
/ @ManyToOne
- 添加交叉table作为实体(java class)并添加额外字段(作为我在数据库中的额外列)
现在,当我想要通过 id 获取所有菜肴或单个菜肴时,我得到错误:
2020-06-25 17:01:25.995 ERROR 8528 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : BŁĄD: column ingredient1_.id does not exist
Pozycja: 406
2020-06-25 17:01:26.000 WARN 8528 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: could not extract ResultSet; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not extract ResultSet (through reference chain: java.util.ArrayList[0]->.springbootdemo.dish.domain.Dish["dishIngredient"])]
我的代码在这里,在分支上:https://github.com/rhquee/MealsOrganizerApp/tree/mapping_many_to_many_extra_column
我的数据库是:
我该如何管理这个交叉table(交叉class)?
更改休眠配置
spring.jpa.hibernate.ddl-auto=none
来自
spring.jpa.hibernate.ddl-auto=create-drop
它会在您每次构建应用程序时创建模式并销毁以前的数据。
对于初始项目构建来说,这是一个很好的选择。
我认为最好的方法是通过 Hibernate 创建数据库模式。首先将spring.jpa.hibernate.ddl-auto=none
改为spring.jpa.hibernate.ddl-auto=update
或create-drop
。然后在 dish
和 ingredient
列上删除模型 DishIngredient
中的 @id
注释,并将 ReferencedColumn
添加到 @JoinColumn
。您还需要将回购和服务中的所有基本方法添加到模型 DishIngredient
.
我有两个实体:菜肴和配料。我想在菜肴中添加配料。
当我使用 @ManyToMany
关系时它起作用(我添加、删除、获取所有含有 table 成分的菜肴 - 我的端点有效),但现在我想在交叉 [= 中添加额外的列41=] DishIngredient
.
所以我所做的是:
- 删除
@ManyToMany
,添加@OneToMany
/@ManyToOne
- 添加交叉table作为实体(java class)并添加额外字段(作为我在数据库中的额外列)
现在,当我想要通过 id 获取所有菜肴或单个菜肴时,我得到错误:
2020-06-25 17:01:25.995 ERROR 8528 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : BŁĄD: column ingredient1_.id does not exist
Pozycja: 406
2020-06-25 17:01:26.000 WARN 8528 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: could not extract ResultSet; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not extract ResultSet (through reference chain: java.util.ArrayList[0]->.springbootdemo.dish.domain.Dish["dishIngredient"])]
我的代码在这里,在分支上:https://github.com/rhquee/MealsOrganizerApp/tree/mapping_many_to_many_extra_column
我的数据库是:
我该如何管理这个交叉table(交叉class)?
更改休眠配置
spring.jpa.hibernate.ddl-auto=none
来自
spring.jpa.hibernate.ddl-auto=create-drop
它会在您每次构建应用程序时创建模式并销毁以前的数据。 对于初始项目构建来说,这是一个很好的选择。
我认为最好的方法是通过 Hibernate 创建数据库模式。首先将spring.jpa.hibernate.ddl-auto=none
改为spring.jpa.hibernate.ddl-auto=update
或create-drop
。然后在 dish
和 ingredient
列上删除模型 DishIngredient
中的 @id
注释,并将 ReferencedColumn
添加到 @JoinColumn
。您还需要将回购和服务中的所有基本方法添加到模型 DishIngredient
.