简体   繁体   English

如何在“movie_added_by”表中添加movie_id和user_id

[英]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_idmovie_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.

相关问题 如何检查我的应用程序中电影的 ID 是否存在于我的数据库中 - How to check if the ID of a movie in my App exist in my DB or not 如何将用户 ID 主键链接到另一个表中的 user_id 作为外键? - How can I make User ID primary key link to user_id in another table as a foreign key? 如何获取保存在 USER_ID 中的数据? - How to get the data saved in a USER_ID? 如何在实体更新中设置user_id? - How to set user_id on Entity update? 表“users”上的更新或删除违反了表“movie_list”上的外键约束“fk_owner_id” - update or delete on table “users” violates foreign key constraint “fk_owner_id” on table “movie_list” 应用程序未使用 user_id,并且表在用户表中也没有 user_id,但出现错误,因为列名 user_id 无效 - application is not using user_id and table also doesnt have user_id in users table, but getting error as The column name user_id is not valid 如何将“/ user / {user_id}”之类的网址映射到特定操作? - How to map URL like “/user/{user_id}” to a specific action? 如何将User_Id传递给android中的每个活动? - How to pass User_Id to every activity in android? 如何通过 user_id 对 Kafka 中的主题进行分区? - How to make a partition of a topic in Kafka by user_id? 如何在 LIKE_ID、POST_ID + USER_ID 中放入 Firebase - How to put in Firebase in the LIKE_ID, POST_ID + USER_ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM