未能通过链式关联呈现值

时间:2015-06-30 03:00:52

标签: jpa spring-boot

我有一个带JPA和Thymeleaf的Spring Boot应用程序。我无法在nomination.nominee.employee.name模板中显示等效的th

请注意,如果我使用NomineeController来提名被提名者,则员工姓名将通过${nominee.employeePrettyName}在其视图中显示。

这是POJO代码:

//I am working with a NominationDraft Type instance of this class
public abstract class Nomination {
  @OneToMany
  private List<Nominee> nominees;

  public List<Nominee> getNominees() {
      return nominees;
  }
}

public class Nominee {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  @ManyToOne
  private Nomination nomination;

  @ManyToOne
  private Employee employee;

  public Employee getEmployee() {
      return employee;
  }

  public String getEmployeePrettyName() { return employee.getPrettyName(); }

}

以下是应该将Nominee与提名相关联的sql插入:

insert into EMPLOYEE (NNUMBER, FIRST_NAME, LAST_NAME, EMAIL, MANAGER_ID) values ('1234567', 'Nominee', 'Employee', 'Nominee.Employee@someaddress.com', NULL);

insert into NOMINATION (USER_ID, NOMINATION_TYPE) values (1, 'Draft');

insert into NOMINEE (NOMINATION_ID, EMPLOYEE_ID) values (1, 1)

以下是有问题的控制器方法:

@RequestMapping(value="/nomination/{id}")
public String nomination(@PathVariable("id") Long id, Model model) {
    Nomination nomination = nominationService.findById(id);
    model.addAttribute("nomination", nomination);
    model.addAttribute("nominees", nomination.getNominees());
    return "nomination";
}

这是视图文件:

<h1>Viewing a Nomination</h1>
<table>
<tr th:each="nominee : ${nominees}">
    <td th:text="${nominee.employeePrettyName}">Pretty Name</td>
</tr>
</table>

这是在发出请求时生成的sql(提名也与用户相关联,但我认为这超出了我的问题范围):

Hibernate: select nomination0_.id as id2_3_, nomination0_.user_id as user_id3_3_, nomination0_.nomination_type as nominati1_3_ from nomination nomination0_ where nomination0_.id=?
Hibernate: select user0_.id as id1_7_0_, user0_.employee_id as employee2_7_0_, employee1_.id as id1_0_1_, employee1_.email as email2_0_1_, employee1_.first_name as first_na3_0_1_, employee1_.last_name as last_nam4_0_1_, employee1_.manager_id as manager_6_0_1_, employee1_.nnumber as nnumber5_0_1_, directrepo2_.manager_id as manager_6_0_2_, directrepo2_.id as id1_0_2_, directrepo2_.id as id1_0_3_, directrepo2_.email as email2_0_3_, directrepo2_.first_name as first_na3_0_3_, directrepo2_.last_name as last_nam4_0_3_, directrepo2_.manager_id as manager_6_0_3_, directrepo2_.nnumber as nnumber5_0_3_ from user user0_ left outer join employee employee1_ on user0_.employee_id=employee1_.id left outer join employee directrepo2_ on employee1_.id=directrepo2_.manager_id where user0_.id=?
Hibernate: select nominees0_.nomination_id as nominati1_3_0_, nominees0_.nominees_id as nominees2_4_0_, nominee1_.id as id1_5_1_, nominee1_.employee_id as employee2_5_1_, nominee1_.nomination_id as nominati3_5_1_, employee2_.id as id1_0_2_, employee2_.email as email2_0_2_, employee2_.first_name as first_na3_0_2_, employee2_.last_name as last_nam4_0_2_, employee2_.manager_id as manager_6_0_2_, employee2_.nnumber as nnumber5_0_2_, employee3_.id as id1_0_3_, employee3_.email as email2_0_3_, employee3_.first_name as first_na3_0_3_, employee3_.last_name as last_nam4_0_3_, employee3_.manager_id as manager_6_0_3_, employee3_.nnumber as nnumber5_0_3_, nomination4_.id as id2_3_4_, nomination4_.user_id as user_id3_3_4_, nomination4_.nomination_type as nominati1_3_4_, user5_.id as id1_7_5_, user5_.employee_id as employee2_7_5_, employee6_.id as id1_0_6_, employee6_.email as email2_0_6_, employee6_.first_name as first_na3_0_6_, employee6_.last_name as last_nam4_0_6_, employee6_.manager_id as manager_6_0_6_, employee6_.nnumber as nnumber5_0_6_ from nomination_nominees nominees0_ inner join nominee nominee1_ on nominees0_.nominees_id=nominee1_.id left outer join employee employee2_ on nominee1_.employee_id=employee2_.id left outer join employee employee3_ on employee2_.manager_id=employee3_.id left outer join nomination nomination4_ on nominee1_.nomination_id=nomination4_.id left outer join user user5_ on nomination4_.user_id=user5_.id left outer join employee employee6_ on user5_.employee_id=employee6_.id where nominees0_.nomination_id=?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果提名有很多被提名者,你是不是应该为被提名者编制索引以便能够达到你想要的人?

像这样:nomination.nominee[i].employee.name其中i是您要显示的被提名人的索引。

参考:http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#variables