2017-10-11 89 views
1

enter image description here我想加入三個表我的模型class.Here是我的模型類, Users.java如何使用一對多協會在多個表

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

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
private Integer id; 
public String username; 
public String password; 
public Integer privid; 

@OneToMany(cascade = CascadeType.ALL, mappedBy = "pid") 
private Set<Privillages> priviJoin; 

@OneToMany(cascade = CascadeType.ALL, mappedBy = "actid") 
private Set<Actions> actionJoin; 



public Integer getId() { 
    return id; 
} 

public void setId(Integer id) { 
    this.id = id; 
} 
@Column(name = "username") 
public String getUsername() { 
    return username; 
} 
public void setUsername(String username) { 
    this.username = username; 
} 
@Column(name = "password") 
public String getPassword() { 
    return password; 
} 
public void setPassword(String password) { 
    this.password = password; 
} 

@Column(name = "privid") 
public Integer getPrivid() { 
    return privid; 
} 
public void setPrivid(Integer privid) { 
    this.privid = privid; 
} 


public Set<Privillages> getPriviJoin() { 
    return priviJoin; 
} 

public void setPriviJoin(Set<Privillages> priviJoin) { 
    this.priviJoin = priviJoin; 
} 

public Set<Actions> getActionJoin() { 
    return actionJoin; 
} 

public void setActionJoin(Set<Actions> actionJoin) { 
    this.actionJoin = actionJoin; 
} 

public Users() { 
} 
} 

而且Privillages.java,

@Entity 
@Table(name = "privillages") 
public class Privillages implements Serializable { 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
public Integer id; 

@Column(name = "pname") 
public String pname; 


@ManyToOne(optional = false) 
@JoinColumn(name = "pid", referencedColumnName = "privid") 
public Users pid; 

public Integer getId() { 
    return id; 
} 
public void setId(Integer id) { 
    this.id = id; 
} 


public String getPname() { 
    return pname; 
} 
public void setPname(String pname) { 
    this.pname = pname; 
} 

public Users getPid() { 
    return pid; 
} 
public void setPid(Users pid) { 
    this.pid = pid; 
} 

public Privillages(){ 
} 
} 

而且Actions.java

@Entity 
@Table(name = "actions") 
public class Actions implements Serializable { 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
public Integer id; 


@Column(name = "actname") 
public String actname; 

@ManyToOne(optional = false) 
@JoinColumn(name = "actid", referencedColumnName = "privid") 
public Users actid; 



public Integer getId() { 
    return id; 
} 

public void setId(Integer id) { 
    this.id = id; 
} 

public String getActname() { 
    return actname; 
} 

public void setActname(String actname) { 
    this.actname = actname; 
} 

public Users getActid() { 
    return actid; 
} 

public void setActid(Users actid) { 
    this.actid = actid; 
} 
public Actions(){ 
} 
} 

包含我的倉庫下面的代碼,

@Query(value = "SELECT u.*,p.*,a.* FROM users u " 
      + "INNER JOIN privillages p ON u.privid = p.pid " 
      + "INNER JOIN actions a ON u.privid = a.actid", 
nativeQuery=true) 
Set<Users> findByUsername(); 

我的控制器動作,

@RequestMapping(value = "/joinResult", method = RequestMethod.GET) 
    public ModelAndView joinResultShow(Model model) 
     { 
      model.addAttribute("joinData",userRepo.findByUsername()); 
      ModelAndView viewObj = new ModelAndView("fleethome"); 
      return viewObj; 
     } 

而且我認爲fleethome很喜歡,

<table> 
     <th> Username </th> 
     <th> Privillage </th> 
     <th> Action </th> 
      <tr th:each="message : ${joinData}"> 

       <td th:text="${message.username}"></td> 
       <td><span th:each="privi : ${message.priviJoin}" 
       th:text="${privi.pname}"></span></td> 
       <td><span th:each="action : ${message.actionJoin}" 
       th:text="${action.actname}"></span></td> 
      </tr> 
    </table> 

我想加盟Privillages和動作與我主力機型的用戶。 Users-Privillages有一對多。還有用戶 - 行動也有一對多。當我用Privillages加入用戶時,它運行良好。我成功加入了兩張桌子。

現在我還需要與用戶一起加入Actions類。我試圖從每個Model類中顯示一列。當我執行我之前加入用戶的過程時,Privillages在這裏不起作用,當我添加了一個表時。

我越來越喜歡錯誤,

There was an unexpected error (type=Internal Server Error, status=500). 
Exception evaluating SpringEL expression: "message.pname" (fleethome:65) 

誰能幫助我加入了額外的一個表,我以前參加。??

回答

1

如果沒有模型實體更改,您可能無法做到這一點。 如果我對你有用,你想讓你的實體類與從db初始化的多個相關集合。但是這不能像你的情況那樣工作,因爲MultipleBagFetchException: cannot simultaneously fetch multiple bags。這與fetch = FetchType.EAGER的多個收集基本上是相同的問題。如果可以的話,易於修復的方法是將Collection<Privillages>更改爲Set<Privillages>ActionsMore info


至於Exception evaluating SpringEL expression: "message.pid.username"實際的原因是,你正試圖與joinData工作,就好像它是一些數據庫表中的記錄,而是你應該就像你使用的Java類使用它。因爲你已經從休眠狀態獲得了Set<User> joinData。可以嘗試像

<tr th:each="message : ${joinData}"> 

    <td th:text="${message.username}"></td> 
    <td><span th:each="privi : ${message.priviJoin}" 
       th:text="${privi.pname}"></span></td> 
    <td><span th:each="action : ${message.actionJoin}" 
       th:text="${action.actname}"></span></td> 

</tr> 

如果你想要一個像你所提供的圖像相同的輸出,你可以試試:

<div th:remove="tag" th:each="message : ${joinData}"> 
    <div th:remove="tag" th:each="privi : ${message.priviJoin}"> 
     <div th:remove="tag" th:each="action : ${message.actionJoin}"> 
      <tr> 
       <td th:text="${message.username}"></td> 
       <td th:text="${privi.pname}"></td> 
       <td th:text="${action.actname}"></td> 
      </tr> 
     </div> 
    </div> 
</div> 
+0

好吧,我將探討更多在此基礎上。我對春季和春季的數據是新的JPA,我想加入多個,因爲你已經明白了。我將閱讀更多關於這個集合,集合,並將嘗試這些。並感謝您的迴應先生。 – Jacob

+0

我嘗試了同樣的設置。但是這一次,我在瀏覽頁面中看到一個錯誤 - 「出現了一個意外錯誤(type = Internal Server Error,status = 500) 異常評估SpringEL表達式:」message.pid.username「(fleethome:52)」。我在更新代碼後編輯了我的問題。我如何解決這個問題?如果你有時間,你能看看我的代碼和錯誤嗎? – Jacob

+0

@MJacob試試'model.addAttribute(「joinData」,userRepo.findByUsername()); 返回「fleethome」'而不是創建新模型。或者把'viewObj.addAttribute(「joinData」,userRepo.findByUsername());'代替一行。我認爲你需要在'viewObj'模型上設置'joinData',你返回 – varren