私は最初のSpringBootプロジェクトを進めていて、この問題に遭遇しました。spring-data-jpaでthymeleafを使用して@ManyToOneの関係を表示
私はthymeleafを使って正しく表示するために@ManyToOne関係を取得しようとしており、これをガイドするのに役立つものは何も見つかりません。プロジェクトは、これは、それが今で表示する方法です実行された場合:
私の代わりに外部キーの値の欄に実際の組織名を取得したいと思います。
誰かがthymeleafを通じて組織名を取得してアクセスする方法を教えてもらえますか?
お時間をいただきありがとうございました。 エド
ここ関連するコードスニペットは... ContactRepositoryリポジトリクラスから
抜粋:Contactオブジェクトクラスから
@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;
.
.
.
ウェブコントローラから
抜粋:(thymeleaf付き)
@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ページ:すべての
<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