我正在尝试使用sping数据JPA实现一对多示例。我是数据JPA.My Model的新手,1-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 Collection<Privillages> priviJoin;
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 Collection<Privillages> getPriviJoin() {
return priviJoin;
}
public void setPriviJoin(Privillages priviJoin) {
this.priviJoin = (Collection<Privillages>) priviJoin;
}
public Users() {
}
@Override
public String toString() {
return String.format("Users[id=%d, username='%s', password='%s']", id,
username, password);
}
}
和,Privillages.java
@Entity
@Table(name = "privillages")
public class Privillages implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer id;
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;
}
@Column(name = "pname")
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
@Column(name = "pid")
public Users getPid() {
return pid;
}
public void setPid(Users pid) {
this.pid = pid;
}
public Privillages(){
}
}
我的存储库是,
public interface UsersRepository extends CrudRepository<Users, Integer>
{
@Query(value ="SELECT u.*,p.* FROM users u INNER JOIN privillages p ON
u.privid=p.pid", nativeQuery=true)
List<Users> findByUsername();
}
我的控制器功能是,
@RequestMapping(value = "/joinResult", method = RequestMethod.GET)
public ModelAndView joinResultShow(Model model)
{
List<Users> use = new ArrayList<Users>();
use = (List<Users>) userRepo.findByUsername();
model.addAttribute("joinData",use);
ModelAndView viewObj = new ModelAndView("fleethome");
return viewObj;
}
并显示如,
<div th:each= "user: ${joinData}">
<span th:text="${user.username}">
</span>
<span th:text="${user.pname}">
</span>
</div>
得到错误,
“出现意外错误(type = Internal Server Error,status = 500)。 评估SpringEL表达式的异常:“user.pname”(fleethome:50)“
而且我也尝试过非本机查询,例如
@Query("SELECT u.username,p.pname FROM Users u INNER JOIN Privillages p ON
u.privid=p.pid")
但是那也显示了“期望加入的路径![SELECT u.username,p.pname FROM com.central.model.Users u INNER JOIN Privillages p ON u.privid = p.pid]”
答案 0 :(得分:1)
用户可以拥有许多权限......您似乎无法一直只获得一个权限。
坚持使用jpa查询并将其写成如下:
@Query("SELECT u FROM Users u inner join fetch u.priviJoin")
然后在您的视图中,您必须添加另一个循环:
<div th:each= "user: ${joinData}">
<span th:text="${user.username}">
</span>
<div th:each="priv: ${user.privJoin}">
<span th:text="${priv.pname}">
</span>
</div>
</div>