2017-03-09 157 views
1

我有一個電影實體和一個流派實體。如何插入一個電影與它的名稱給出的流派實體?休眠OneToMany與連接表

例如,流派表

+----+--------+ 
| id | name | 
+----+--------+ 
| 1 | Action | 
| 2 | Drama | 
+----+--------+ 

電影臺

+----+----------+ 
| id | name | 
+----+----------+ 
| 1 | Movie 1 | 
| 2 | Movie 2 | 
+----+----------+ 

加入表

+----+----------+-----------+ 
| id | movie_id | genre_id | 
+----+----------+-----------+ 
| 1 |  1 |   1 | 
| 2 |  1 |   1 | 
| 3 |  1 |   2 | 
+----+----------+-----------+ 

類型實體

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; 
    } 

} 

電影實體

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; 
    } 

} 

控制器

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(); 
     } 

    } 
} 

當我添加一個電影,我想在HTML中選擇標籤和休眠添加流派連接表中不插入流派表和連接表。

如何識別這些類型,讓hibernate不會再插入它們?

最後,我想要轉到一個頁面,輸入一個電影名稱並選擇適用於該電影並提交的多個流派。

回答

1

首先,關聯是一個ManyToMany。顯然有幾部相同類型的電影。

其次,選擇框沒有理由使用流派的名稱作爲其選項的值。它應該使用ID代替,並使用名稱作爲選項的標籤。

因此,它歸結爲以下問題:給出選定流派的ID,如何創建一個電影與選定的流派。而且,anser很簡單:使用ID將數據從數據庫中取出(使用Session.get()Session.load()EntityManager.find()或)。然後將這些流派實例存儲在創建的電影的流派列表中。