1

enter image description here

I am trying to join three tables with my model class.Here is my model 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() {
}
}

And 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(){
} 
}

And 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(){
}
}

My repository containing following code,

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

My controller action is,

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

And my view fleethome is like,

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

I am trying to join Privillages and Actions with my main model Users. Users-Privillages have one to many. And also Users - Actions also have one to many. When I joined Users with Privillages it working good. I successfully joined two table.

Now I also need to join Actions class with Users. I am trying to displaying one column from each Model classes. When I implemented the procedure that I follow previously for joining Users-Privillages is not working here, when I added one more table.

I am getting the error like,

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

How can I join the additional one table with my previous join?

halfer
  • 19,824
  • 17
  • 99
  • 186
Mr.DevEng
  • 2,651
  • 14
  • 57
  • 115

1 Answers1

1

You probably can't do that without model entity changes. If i got you right, you want to get your entity class with multiple related collections initialized from db. But this can't work as is in your case because MultipleBagFetchException: cannot simultaneously fetch multiple bags. It is basically the same problem of multiple collections with fetch = FetchType.EAGER. Easy fix would be to change Collection<Privillages> to Set<Privillages> or same for Actions if you can. More info


As for Exception evaluating SpringEL expression: "message.pid.username" the actual reason is that you are trying to work with joinData as if it is some database table record, but instead you should work with it like you would with java classes. Because you already got Set<User> joinData from hibernate. Can try something like

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

If you want same output like in the image you provided, you can try:

<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>
varren
  • 14,551
  • 2
  • 41
  • 72
  • Ok, I will explore more on this basis. I am new to spring and spring data JPA,I am trying to join multiple,as you already understood.I will read more about this Collection , Set, And will try on those. And thank you for your response sir. – Mr.DevEng Oct 11 '17 at 14:25
  • I tried the same with set. But this time I got a error in view page that - "There was an unexpected error (type=Internal Server Error, status=500). Exception evaluating SpringEL expression: "message.pid.username" (fleethome:52)". I also edited my question after my updation of code. How I can fix this issue?? Can you look on my code and error if you have time?? – Mr.DevEng Oct 11 '17 at 14:55
  • @MJacob Try `model.addAttribute("joinData",userRepo.findByUsername()); return "fleethome"` instead of creation of a new model. Or put `viewObj.addAttribute("joinData",userRepo.findByUsername());` one line lower instead. I think you need to set `joinData` on` viewObj` model you returning – varren Oct 11 '17 at 14:58
  • Ok..I will try.But My problem is still there , that the error mentioned in comment also in editted question. What method I need to follow to solve this issue?? did you see my error? – Mr.DevEng Oct 11 '17 at 15:01
  • @MJacob your error is part of the thymeleaf and says that you cant use `message.pid.username` like you use it because `joinData` is `Set` so `message` is `Users` and there is no `pid` field in `Users` – varren Oct 11 '17 at 15:08
  • Yaa. already tried like {message.username},{message.pname},{message.actname} which are the atributes in those three tables. But that set again showing "Exception evaluating SpringEL expression: "message.pname". How i can handle these displaying properties in view page?? – Mr.DevEng Oct 11 '17 at 15:12
  • Currently I only accessing username ,that is column of my main model. How I can handle the remaining? – Mr.DevEng Oct 11 '17 at 15:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156523/discussion-between-mjacob-and-varren). – Mr.DevEng Oct 12 '17 at 07:07
  • I updated with my new code.And can you look on my updates? It is working only for the set basis. I need to display the all data as mentioned in the image on top.Currently I used native query.Can you look on updated code? What I need to follow for getting in the given structure? – Mr.DevEng Oct 12 '17 at 12:21