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