简体   繁体   中英

How to add movie_id and user_id in “movie_added_by” table

I have already a user 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.

Here user model needs to map one to many to movie_added_by and similarly, the movie will be mapped to movie_added_by.

For better understanding, you can refer to the DB diagram.

在此处输入图像描述

I really don't know how can I do by using hibernate annotation

The user model is like this:

@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:

@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. There are 2 ways of doing it (with intermediary table created explicitly or by 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. Couple of other notes here:

a) you might want to change the id variable type from Integer to Long in case your entities grow;

b) If you have annotated a column with @Id, you don't have to use unique=true and nullable = false in the column annotation;

c) remember about implementing no-args constructor;

d) remember to exclude relationship fileds from the equals(), hashCode() and the toString() methods;

There is another way, where you explicitly create a model for the table keeping relationships. 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;
}



The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM