Reputation: 133
I have a Movie Entity and a genre entity.How can I insert a movie with the genre entity given by it's name?
For example,Genre table
+----+--------+
| id | name |
+----+--------+
| 1 | Action |
| 2 | Drama |
+----+--------+
Movie table
+----+----------+
| id | name |
+----+----------+
| 1 | Movie 1 |
| 2 | Movie 2 |
+----+----------+
Join table
+----+----------+-----------+
| id | movie_id | genre_id |
+----+----------+-----------+
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 2 |
+----+----------+-----------+
Genre Entity
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name = "GENRE")
public class GenreEntity {
@Id
@Column(name = "id")
@GeneratedValue(generator="increment2")
@GenericGenerator(name="increment2", strategy = "increment")
private int id;
@Column(unique = true,name="name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public GenreEntity(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Movie entity
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name = "MOVIE")
public class MovieEntity {
@Id
@Column(name = "id")
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
private int id;
@Column(name = "RELEASE_DATE")
private Date releaseDate;
@Column(name = "name")
private String name;
@Column(name = "rating")
private double rating;
@Column(name = "length")
private int length;
@Column(name = "casting")
private String casting;
@Column(name = "director")
private String director;
@Column(name = "description")
private String description;
@Column(name = "writer")
private String writer;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(
name = "CATEGORY",
joinColumns = @JoinColumn(name = "movie_id")
)
private List<GenreEntity> genre = new ArrayList<>();
public List<GenreEntity> getGenre() {
return genre;
}
public MovieEntity(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getRating() {
return rating;
}
public void setRating(double rating) {
this.rating = rating;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public String getCasting() {
return casting;
}
public void setCasting(String casting) {
this.casting = casting;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
}
Controller
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import eu.ubis.fiimdb.db.entity.GenreEntity;
import eu.ubis.fiimdb.db.entity.MovieEntity;
@WebServlet("/addmovie")
public class AddMovie extends HttpServlet {
private String message;
public void init() throws ServletException
{
// Do required initialization
message = "Hello World";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// Set response content type
response.setContentType("text/html");
SessionFactory factory = new Configuration()
.configure("/META-INF/hibernate.cfg.xml")
.buildSessionFactory();
Session session=factory.getCurrentSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
MovieEntity a = new MovieEntity();
GenreEntity b = new GenreEntity();
GenreEntity c = new GenreEntity();
b.setName("Alex");
a.setName("teeefasd");
c.setName("HGFHFG");
a.getGenre().add(b);
a.getGenre().add(c);
session.persist(a);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}
When I add a movie,I want to have a select tag in html and hibernate to add the genre in the join table not to insert into the genre table and also the join table.
How to identify those genre so hibernate doesn't insert them again?
In the end I want to go to a page,type a movie name and select multiple genres that apply to that movie and submit.
Upvotes: 1
Views: 133
Reputation: 691635
First, the association is a ManyToMany. There are obviously several movies of the same genre.
Second, there is really no reason for the select box to use the name of the genres as the values of its options. It should use IDs instead, and use the names as the labels of the options.
So it boils down to the following question: given IDs of selected genres, how to create a movie with the selected genres. And the anser is simple: use the IDs to get the genres out of the database (using Session.get()
or Session.load()
, or EntityManager.find()
or EntityManager.getReference()
). Then store those Genre instances in the list of genres of the created Movie.
Upvotes: 1