如何在多个中使用 OneToMany Association table

How to use OneToMany Association in more than one table

我正在尝试将三个 table 与我的模型连接起来 class.Here 是我的模型 classes.

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

而我的看法是,

<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 and Actions 加入我的主要模型用户。用户-权限是一对多的。还有用户 - 操作也有一对多。当我加入 Users with Privillages 时,它运行良好。我成功加入了两个table.

现在我还需要与用户一起加入操作 class。我正在尝试显示每个模型 classes 中的一列。当我实施我之前遵循的加入 Users-Privillages 的过程时,我又添加了一个 table.

我收到类似

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

我怎样才能加入另一个 table 与我以前加入的人?

如果不更改模型实体,您可能无法做到这一点。 如果我没看错,您希望您的实体 class 具有从 db 初始化的多个相关集合。但这不能像你的情况那样工作,因为 MultipleBagFetchException: cannot simultaneously fetch multiple bags。和fetch = FetchType.EAGER的多集合基本是一样的问题。如果可以的话,简单的解决方法是将 Collection<Privillages> 更改为 Set<Privillages> 或将 Actions 更改为相同。 More info


至于 Exception evaluating SpringEL expression: "message.pid.username" 的实际原因是您正在尝试使用 joinData 就好像它是一些数据库 table 记录一样,但您应该像您一样使用它会用 java classes。因为你已经从休眠中得到了 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>