[英]How to add movie_id and user_id in “movie_added_by” table
I have already a user model.我已经有一个用户 model。 Now I have created a movie model, my requirement is that whenever any existing user is going to add any movie , at that time user_id and movie_id will be store in the movie_added_by table.
现在我创建了一个电影model,我的要求是每当任何现有用户要添加任何电影时,那时user_id和movie_id将存储在movie_added_by表中。
Here user model needs to map one to many to movie_added_by and similarly, the movie will be mapped to movie_added_by.在这里,用户model 需要将 map 一对多地映射到movie_added_by ,同样,电影将映射到movie_added_by。
For better understanding, you can refer to the DB diagram.为了更好的理解,可以参考DB图。
I really don't know how can I do by using hibernate annotation我真的不知道如何使用hibernate 注释
The user model is like this:用户 model 是这样的:
@Getter
@Setter
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", unique = true, nullable = false)
private Integer user_id;
private String name;
}
The movie model is like this:电影model是这样的:
@Getter
@Setter
public class Movie implements Serializable
{
private static final long serialVersionUID = -6790693372846798580L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "movie_id", unique = true, nullable = false)
private Integer movie_id;
private String movie_name;
}
You probably want to create a @ManyToMany relationship between the entities.您可能希望在实体之间创建 @ManyToMany 关系。 There are 2 ways of doing it (with intermediary table created explicitly or by Hibernate.
有两种方法(使用显式创建的中间表或由 Hibernate 创建的中间表。
In simple approach your entities would look as following:在简单的方法中,您的实体将如下所示:
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", unique = true, nullable = false)
private Integer user_id;
private String name;
@ManyToMany(cascade = CascadeType.Persist)
@JoinTable(name="user_movie",
joinColumns = {@JoinColumn(name="user_id")},
inverseJoinColumns = {@JoinColumn(name="movie_id)})
private Set<Movie> movies = new HashSet<>();
}
public class Movie implements Serializable
{
private static final long serialVersionUID = -6790693372846798580L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "movie_id", unique = true, nullable = false)
private Integer movie_id;
private String movie_name;
@ManyToMany(cascade = CascadeType.Persist, mappedBy = "movies" //field from the user class responsible for mapping)
private Set<User> users = new HashSet<>()
}
So basically here you tell Hibernate to create an intermediary table and keep there correlated id's of those 2 entities.所以基本上在这里你告诉 Hibernate 创建一个中间表并将这两个实体的相关 ID 保留在那里。 Couple of other notes here:
这里还有一些其他注意事项:
a) you might want to change the id variable type from Integer to Long in case your entities grow; a) 您可能希望将 id 变量类型从 Integer 更改为 Long 以防您的实体增长;
b) If you have annotated a column with @Id, you don't have to use unique=true and nullable = false in the column annotation; b) 如果你已经用@Id注解了一个列,你不必在列注解中使用unique=true和nullable=false;
c) remember about implementing no-args constructor; c) 记住实现无参数构造函数;
d) remember to exclude relationship fileds from the equals(), hashCode() and the toString() methods; d) 记得从 equals()、hashCode() 和 toString() 方法中排除关系文件;
There is another way, where you explicitly create a model for the table keeping relationships.还有另一种方法,您可以为表保持关系显式创建 model。 This might become handy, when it turns out that You need to keep more data in the 'relationship table'.
当事实证明您需要在“关系表”中保留更多数据时,这可能会变得很方便。 In that case, Your entities would look as following:
在这种情况下,您的实体将如下所示:
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", unique = true, nullable = false)
private Integer user_id;
private String name;
@OnetToMany(cascade = CascadeType.PERSIST, mappedBy = "user")
private Set<AddedMovie> addedMovies = new HashSet<>()
}
public class Movie implements Serializable
{
private static final long serialVersionUID = -6790693372846798580L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "movie_id", unique = true, nullable = false)
private Integer movie_id;
private String movie_name;
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "movie")
private Set<AddedMovie> moviesAddedByUser = new HashSet<>();
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
public class AddedMovie{
@Id
@GeneratedValue
private Long id;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "user_id")
private User user;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "movie_id")
private Movie movie;
// sine this entity has now its own lifecycle, you can add more fields here
private Integer rating;
private LocalDateTime movieAddedOn;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.