簡體   English   中英

使用 Spring MVC、JPA 和 Hibernate 添加多對多

[英]Add ManyToMany with Spring MVC, JPA and Hibernate

在我的 Spring MVC 項目中,我創建了兩個連接為 ManyToMany 的類,如下所示:

用戶( ID 、余額、姓氏和名字)-> 租賃( UserIDMovieID )<- 電影( ID 、標題、導演和長度)

閱讀了如何做到這一點,我最終得到了這兩個類的代碼:

@Entity
@Table(name = "users")
public class Users implements Serializable {

@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "Rentals",
           joinColumns = { @JoinColumn(name = "user_id") },
           inverseJoinColumns = { @JoinColumn(name = "movie_id") } )
private Set<Movies> movies = new HashSet<>();

//--------------------------

@Entity
@Table(name = "movies")
public class Movies implements Serializable {

@ManyToMany(fetch = FetchType.LAZY, 
            cascade = { CascadeType.PERSIST, CascadeType.MERGE }, 
            mappedBy = "movies")
private Set<Users> users = new HashSet<>();

不僅如此,我還在我的屬性中添加了hibernate.ddl-auto = createhibernate.hbm2ddl.auto=create-drop ,以便在我的 Z399BD1EE587245ECAC6F39BEAA99 數據庫中自動創建和更新表。

問題是這樣來的。 我的 MVC 必須能夠創建、編輯、更新和刪除任何電影、用戶和租借。 我知道這可以替換為:選擇用戶,選擇電影,將電影添加到用戶的電影集並將用戶添加到電影的用戶集並保存。 刪除或編輯也是如此。

但是如何在我的 JSP 中解決這個問題? 以下是我在使用具有用戶和電影對象的 Rentals class 時的做法:

<form:form action="${addAction}" modelAttribute="rental">
<table><tr><td>
            <form:label path="user.usrid">
                <spring:message text="ID User"/>
            </form:label>
        </td><td>
            <form:select path="user.usrid">
                <form:options items="${listUsers}" itemValue="usrid" itemLabel="fullName"/>
            </form:select>
        </td></tr><tr><td>
            <form:label path="movie.movid">
                <spring:message text="ID Movie"/>
            </form:label>
        </td><td>
            <form:select path="movie.movid">
                <form:options items="${listMovies}" itemValue="movid" itemLabel="title"/>
            </form:select>
        </td></tr>

    <tr><td colspan="2">
        <input type="submit" value="<spring:message text="Add Rental"/>" />
    </td></tr>
</table></form:form>

controller 有這些方法:

@RequestMapping(value = "/rentals", method = RequestMethod.GET)
public String listRentals(Model model) {
    model.addAttribute("rental", new Rentals());
    model.addAttribute("listUsers", this.murService.getUsers());
    model.addAttribute("listMovies", this.murService.getMovies());
    model.addAttribute("listRentals", this.murService.getRentals());
    return "rental";
}

@RequestMapping(value = "/rentals/add", method = RequestMethod.POST)
public String addRental(@ModelAttribute("rental") Rentals rnt) {

    if (!checkIfExists(rnt)) {
        this.murService.addRental(rnt);
    } else {
        System.out.println("Couldn't be added, rent already exists.");
    }

    return "redirect:/rentals";
}

我將 ModelAttribute 用於 Rentals object,但現在我沒有那個 class,ModelAttribute 應該指向什么? 如果我認為它可能是正確的,我如何發送兩個 ModelAttributes,一個作為用戶,一個作為電影? 或者有什么方法可以做我需要的?

沒有理由你不能繼續使用Rental class,你只需要在你的數據庫方面考慮更多。

1) UserMovie是實體。

2) Rental是您的視圖對象的一部分。

你想列出所有的租金嗎? 閱讀您的整個數據庫,並為每個用戶創建每部電影的租借。 將其傳遞給您的 model。 你想創建一個新的出租嗎? Select 用戶在您從數據庫中租借的電影並將電影添加到movies集中。

因此,現在 Rental 是視圖對象的一部分,不需要持久化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM