[英]Unidirectional ManyToMany mapping using Spring JPA
In my project I would like to have ManyToMany unidirectional mapping. 在我的项目中,我希望有ManyToMany单向映射。 To do so I've created User and Recipe entities and repositories for them (using Spring JPA repositories).
为此,我已经为它们创建了用户和配方实体以及存储库(使用Spring JPA存储库)。 Now I'd like to populate my database with some sample entries.
现在,我想用一些示例条目填充数据库。 Once I start spring-boot application, error occurs:
一旦启动spring-boot应用程序,就会发生错误:
org.hibernate.PersistentObjectException: detached entity passed to persist: com.example.demo.database.Recipe
org.hibernate.PersistentObjectException:分离的实体传递给 持久化:com.example.demo.database.Recipe
Here are my User and Recipe classes: 这是我的用户和食谱类:
-- User -- -用户-
@Data
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String email;
@NotNull
@Size(min = 8)
private String password;
@ManyToMany(cascade = CascadeType.ALL)
private Set<Recipe> favouriteRecipes = new HashSet<>();
private User() {}
public User(String email, String password) {
this.email = email;
this.password = password;
}
}
-- Recipe -- - 食谱 -
@Data
@Entity
public class Recipe {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String title;
private Recipe() {}
public Recipe(String title) {
this.title = title;
}
}
and here is how I add rows to database: 这是我如何向数据库添加行:
@Component
public class DatabaseInitializer implements CommandLineRunner {
@Autowired
private UserRepository userRepository;
@Override
public void run(String... args) throws Exception {
User u1 = new User("rick.astley@gmail.com", "admin123");
User u2 = new User("john.doe@gmail.com", "admin123");
Recipe r1 = new Recipe("Chicken curry");
Recipe r2 = new Recipe("Spaghetti bolognese");
u1.getFavouriteRecipes().add(r1);
u1.getFavouriteRecipes().add(r2);
u2.getFavouriteRecipes().add(r1);
u2.getFavouriteRecipes().add(r2);
userRepository.save(u1);
userRepository.save(u2);
}
}
I've tried to follow this tutorial but there the guy uses hibernate instead of Spring repositories so that's why I'm stuck here. 我尝试按照本教程进行操作,但是该人员使用的是hibernate而不是Spring存储库,所以这就是我被困在这里的原因。 I really don't know where I am wrong.
我真的不知道我哪里错了。
Any help would be appreciated :) 任何帮助,将不胜感激 :)
When you're saving User u2
then Hibernate is trying to save Recipes r1 and r2
again(as it has already saved it while saving User u1
). 当您保存
User u2
Hibernate会尝试再次保存Recipes r1 and r2
(因为在保存User u1
已经保存了它)。 This is happening because @ManyToMany
annotation present in User class as User
is appearing to be the parent of Recipe
which is not the case as Recipies
could be common between Users
. 之所以发生这种情况,是因为
@ManyToMany
批注在User类中以User
形式出现,似乎是Recipe
的父级,而事实并非如此,因为Recipies
在Users
之间可能很常见。
Solution: 解:
cascade
from annotation. cascade
。 & &
favoriteRecipe
collection. favoriteRecipe
食谱集合中。 Following links could be helpful: 以下链接可能会有所帮助:
A beginner's guide to JPA and Hibernate Cascade Types JPA和Hibernate级联类型的初学者指南
How to persist @ManyToMany relation - duplicate entry or detached entity 如何保持@ManyToMany关系-重复的条目或分离的实体
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.