gpt4 book ai didi

java - 如何在双向多对多映射中添加数据?(这不是添加数据的正常过程)

转载 作者:行者123 更新时间:2023-12-02 00:50:09 25 4
gpt4 key购买 nike

public class Movies {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int movie_id;

@Column(unique = true, length = 255)
private String movie_name;

private String movie_language;

@ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
private Set<Actors> actors;

private String movie_genre;
//Getter-Setters
}

public class Actors {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int actor_id;

private String actor_name;

private String actor_industry;

@ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL,mappedBy = "actors")
Set<Movies> movies;

//Getter-Setters
}

/*This is where my logic resides*/

我有两个实体电影和 Actor ,它们具有多对多映射。 如果我添加一部带有 Actor 的电影,我可以在数据库中看到它,并带有映射电影和 Actor 的附加表。

现在假设如果我输入另一部具有相同 Actor 的新电影,它会给我一个错误, Actor 已经存在(这里 Actor 中的 actor_name 和电影中的 movie_name 是唯一的),

如何在数据库中添加电影,以便它应该在电影表中输入新电影,但如果 Actor 已经存在于类型转换中,它应该简单地将新电影与现有 Actor 映射。

例如:首先输入这个

{
"movie_name":"Titanic",
"movie_language":"English",
"release_year":"1997",
"movie_genre":"Romance",
"actors":[
{
"actor_name":"Leonardo",
"actor_industry":"Hollywood"
}
]
}

现在我想输入第二部电影:

{
"movie_name":"Once upon a time in hollywood",
"movie_language":"English",
"release_year":"2019",
"movie_genre":"Drama",
"actors":[
{
"actor_name":"Lenardo",
"actor_industry":"Hollywood"
},
{
"actor_name":"Brad",
"actor_industry":"Hollywood"
}
]
}

:::::这是我添加新电影的方式:::::::

public void addMovie(Movies movie) {
Movies tempMovie = movieRepository.findByName(movie.getMovie_name());
if (tempMovie != null) {
Set<Actors> actors = new HashSet<Actors>();
actors = movie.getActors();
for (Actors actors2 : actors) {
tempMovie.getActors().add(actors2);
}
movie = tempMovie;
movieRepository.save(movie);
} else {
System.out.println("############################In else");
Set<Actors> actors = movie.getActors();
for (Actors actors2 : actors) {
System.out.println(actors2.getActor_name());
Actors actorTemp = actorRepository.findByName(actors2.getActor_name());
// System.out.println("Id::::"+actorTemp.getActor_id());
if (actorTemp != null) {
System.out.println("Actor is there " + actors2.getActor_name());
System.out.println("Before" + actorTemp.getMovies());
actorTemp.getMovies().add(movie);
System.out.println("After:" + actorTemp.getMovies());
movie.getActors().add(actorTemp);
System.out.println("Added");
movieRepository.save(movie); //If I remove this line,then there's no error,but also no changes in DB.....without this line my new movie won't be persisted.
} else {
System.out.println("Actor is new");
movieRepository.save(movie);
}
}
}

}

最佳答案

我认为您正在寻找类似桥接表或连接表之类的内容,其中包含电影和 Actor 的标识符。这个blog很好地解释了您需要为此场景实现什么。为了避免由于数据库中的重复记录而出现错误,请熟悉 JPA 的合并与持久之间的区别(我假设您由于注释而使用 JPA)。我希望这对您有所帮助,祝您编码愉快!

关于java - 如何在双向多对多映射中添加数据?(这不是添加数据的正常过程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57866208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com