我正在尝试从Spring Data JPA中的多个表中获取数据。
User.java
true
Classes.java
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button class="navbar-toggle" data-toggle="collapse" data-target=".navHeaderCollapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="#" class="col-lg-4 navbar-brand">BySofie</a>
</div>
<div class="collapse navbar-collapse navHeaderCollapse">
<ul class="col-lg-5 nav navbar-right navbar-nav">
<li class="active"><a>START</a></li>
<li><a>OM MIG</a></li>
<li><a>KOMPETENCER</a></li>
<li><a>PORTFOLIO</a></li>
<li><a>KONTAKT</a></li>
</ul>
</div>
</div>
</nav> <!--end navbar-->
<section class="container-fluid"> <!--main section-->
<div class="row">
<div class="col-lg-12">
<img class="img-responsive" src="images/background_flower.png"/>
</div>
</div>
</section> <!--end main section-->
这些实体生成名为&#39; user_classes&#39;和字段如下class_id和user_id
我正在尝试显示属于该特定用户的类的名称。 由于我的模型设计如上所示,我试图像这样做
@Entity
@Table(name = "user")
public class User{
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
private String username;
private String password;
//Getters and setters generated
@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
private List<Classes> classes;
我在数据库中的表格看起来像这样
所以班级和用户确实存在。
但我面临这个错误
@Entity
public class Classes {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
private String name;
@Lob
private String description;
@ManyToMany(mappedBy = "classes", fetch = FetchType.EAGER, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
private List<User> user;
我不确定我做错了什么?
答案 0 :(得分:0)
错误是拼写错误。我已将用户对象Thymeleaf视图传递为
<tbody th:each="users : ${users}">
我试图将其作为
来实现<tr th:each="classes : ${user.classes}">
<td th:text="${classes.name}"></td>
</tr>
部分${user.classes}
不起作用。当我将用户对象编写为users
或类似${users.classes}
时,一切正常!