0
我正在通过我的第一个SpringBoot项目工作并遇到此问题。使用thymeleaf与spring-data-jpa显示@ManyToOne关系
我想获得一个@ManyToOne关系,以正确显示使用百里香,并找不到任何东西来帮助指导我完成此操作。当项目运行,这是它的方式现在显示:
我想在列,而不是外键的值来获得实际的组织名称。
有人可以帮助我如何获得组织名称,并通过thymeleaf访问?
感谢您花时间查看您的建议。 埃德
下面是相关的代码片段...从ContactRepository仓储类
摘录:
@Transactional
@Repository
public interface ContactRepository extends PagingAndSortingRepository<Contact, Long> {
Contact findByContactId(Long contactId);
@Query(value = "SELECT * FROM con.contacts c, con.organizations o WHERE c.organization_fk = o.organization_id ORDER BY c.last_name ASC", nativeQuery = true)
Iterable<Contact> findAllContacts();
.
.
.
从联系对象类摘录:
@Entity
@Table(name = "contacts", schema = "con", catalog = "cis")
public class Contact implements Serializable {
private Long contactId;
private String firstName;
private String lastName;
private String email;
private String title;
private Long organizationFk;
private String phone;
@ManyToOne(fetch = FetchType.LAZY , optional = true)
@JoinColumn(name="organization_fk", insertable = false, updatable = false, nullable = false)
private Organization organization;
//I assume that some kind of getter and setter needs to be implemented here to get the organization data???
.
.
.
从组织对象类摘录:
@Entity
@Table(name = "organizations", schema = "con", catalog = "cis")
public class Organization implements Serializable {
private static final Long serialVersionUID = 2016L;
private Long organizationId;
private String name;
private String email;
private String address;
private String phone;
@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="organization")
private List<Contact> contacts;
.
.
.
从网络控制器
摘录:
@Configuration
@ComponentScan
@EnableAutoConfiguration
@Controller
public class ContactsController {
@Autowired
private ContactRepository contactRepository;
@RequestMapping(value = "/" + APP_CONTEXT + "/" + OBJECT_NAME + "s/a/{ancestors}", method = RequestMethod.GET)
public String getCollection(Model model, HttpServletRequest request, @PathVariable Integer ancestors) {
Iterable<Contact> contacts;
try {
contacts = contactRepository.findAllContacts();
model.addAttribute("Set", contacts);
.
.
.
摘自接触html页面(与thymeleaf):
<h2>Contacts</h2>
<br/>
<fieldset class="rowL100">
<legend>Contacts Listing</legend>
<table>
<thead>
<tr>
<th>Name</th>
<th>Organization</th>
<th>Title</th>
<th>Contact Info.</th>
</tr>
</thead>
<tbody>
<tr th:each="Item : ${Set}">
<td>
<a th:attr="id='sort-' + ${Item.contactId}" th:href="@{'/' + ${NS.pageAppContext} + '/' + ${NS.pageObjectType} + 's/' + ${Item.contactId} + '/id/a/' + ${session.ancestors}}"><span th:text="${Item.lastName}" />, <span th:text="${Item.firstName}" /></a>
</td>
<td><span class="pointer" th:text="${Item.organizationFk}" /></td>
<td><span class="pointer" th:text="${#strings.abbreviate(Item.title,33)}" /></td>
<td>
<div style="text-align: center;"><span class="pointer" th:text="${Item.email}" /><br/><span class="pointer" th:text="${Item.phone}" /></div>
</td>
</tr>
</tbody>
</table>
</fieldset>
感谢您的意见和建议。顺便说一下,很棒的教程。我要从头开始,看看是否能解决这个问题。当再次回到这一点时会提供更新。 – eLowe