英文:
Remove Object in Map from Many-to-Many Relation
问题
我有一个实体 Mealplan,其中每个 weekday (枚举) 包含一顿饭 meal。这通过一个映射和一个多对多的关系来实现,如下所示:
@Entity
public class Mealplan {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToMany
@SortNatural
private Map<Weekday, Meal> mealsPerWeek;
(...)
}
这意味着在我的数据库中,mealsPerWeek 属性存储在一个额外的表中,名为 "mealplan_meals_per_week"。该表包含 mealplan_id、meal_per_week_id (mealID) 和 weekday_id。
现在,如果我删除一个 Mealplan,所有相关内容都会被删除,这正是我期望的。但是,如果我想删除一个 Meal,会出现以下 SQL 错误:
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Referentielle Integrität verletzt: "FKPIBLWWQG1HR2D5W7BGORA9XBB: PUBLIC.ESSENSPLAN_ESSEN_PRO_WOCHE FOREIGN KEY(ESSEN_PRO_WOCHE_ID) REFERENCES PUBLIC.ESSEN(ID) (1)"
Referential integrity constraint violation: "FKPIBLWWQG1HR2D5W7BGORA9XBB: PUBLIC.ESSENSPLAN_ESSEN_PRO_WOCHE FOREIGN KEY(ESSEN_PRO_WOCHE_ID) REFERENCES PUBLIC.ESSEN(ID) (1)"; SQL statement:
delete from essen where id=? [23503-200]
我期望的是,如果我删除一顿饭 Meal,则 mealplan 中的相应行会被删除,但其他内容保持不变。
注意:CasdadeType.REMOVE 不是一个选项,因为它也会删除包含我想要删除的饭菜的每个 Mealplan。
@Entity
public class Essen {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
}
英文:
I have an entity Mealplan, where each weekday (enum) contains a meal. This is realised with a map and a Many-to-Many relation like this:
@Entity
public class Mealplan {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToMany
@SortNatural
private Map<Weekday, Meal> mealsPerWeek;
(...)
}
This means, in my database the mealsPerWeek property is stored in an extra table. "mealplan_meals_per_week". This table contains the mealplan_id, the meal_per_week_id (mealID) and the weekday_id.
Now, if I remove a Mealplan, everything gets deleted as I am expecting it. But if I want to delete a Meal, following SQL error occurs:
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Referentielle Integrität verletzt: "FKPIBLWWQG1HR2D5W7BGORA9XBB: PUBLIC.ESSENSPLAN_ESSEN_PRO_WOCHE FOREIGN KEY(ESSEN_PRO_WOCHE_ID) REFERENCES PUBLIC.ESSEN(ID) (1)"
Referential integrity constraint violation: "FKPIBLWWQG1HR2D5W7BGORA9XBB: PUBLIC.ESSENSPLAN_ESSEN_PRO_WOCHE FOREIGN KEY(ESSEN_PRO_WOCHE_ID) REFERENCES PUBLIC.ESSEN(ID) (1)"; SQL statement:
delete from essen where id=? [23503-200]
I am expecting that If I delete a Meal, the line in mealplan gets deleted but everything else stays the same.
Note: CasdadeType.REMOVE is not an option, because it deletes every Mealplan too, where the meal i want to remove is in it.
@Entity
public class Essen {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
}
答案1
得分: 1
在mealplan_meals_per_week表中,meal_per_week_id列有一个对Meal的引用。因此,在尝试删除没有级联引用的Meal时无法解决。
所以首先删除mealplan_meals_per_week表中关于Meal的引用,然后再删除Meal。
由于您没有为mealplan_meals_per_week使用实体(Entity),您可以使用原生SQL来定义查询,使用nativeQuery。
@Query(
value = "DELETE FROM mealplan_meals_per_week m WHERE m.meal_per_week_id = ?1",
nativeQuery = true)
void deleteByMealId(Integer mealId);
英文:
In mealplan_meals_per_week table there is a reference of Meal in meal_per_week_id column. So where you are trying to delete Meal without cascade reference can't be resolved.
So first delete the references of Meal in mealplan_meals_per_week table and then delete Meal.
Since you are not using Entity for mealplan_meals_per_week, you can use native SQL to define query using nativeQuery.
@Query(
value = "DELETE FROM mealplan_meals_per_week m WHERE m.meal_per_week_id= ?1",
nativeQuery = true)
void deleteByMealId(Interger mealId);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论