2017-03-01 15 views
0

私は最初のSpringBootプロジェクトを進めていて、この問題に遭遇しました。spring-data-jpaでthymeleafを使用して@ManyToOneの関係を表示

私はthymeleafを使って正しく表示するために@ManyToOne関係を取得しようとしており、これをガイドするのに役立つものは何も見つかりません。プロジェクトは、これは、それが今で表示する方法です実行された場合:

enter image description here

私の代わりに外部キーの値の欄に実際の組織名を取得したいと思います。

誰かが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}" />,&nbsp;<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>

答えて

0

まず:

  1. コントローラーで、Reposiを使用しないでくださいサービスを利用する。
  2. @コントローラごとに@Controllerまたは@RestControllerアノテーションのみを使用してください。
  3. リポジトリレイヤで@Transnationalを使用しないでください。サービスレイヤで使用してください。
  4. Spring Data JPAを使用している場合は、ネイティブSQLを使用しないでください。定義済みのメソッドの一部(https://docs.spring.io/spring-data/jpa/docs/current/reference/html/)を使用できない場合は、特定のクエリにJPQLを使用してください。
  5. lazyプロパティをオーバーライドする場合は、join fetchを使用します。 - How does the FetchMode work in Spring Data JPA

基本的には、私はSpringBootチュートリアルをお勧めします。 - https://www.youtube.com/watch?v=msXL2oDexqw&list=PLqq-6Pq4lTTbx8p2oCgcAQGQyqN8XeA1x

+0

ご意見ありがとうございます。途中で素晴らしいチュートリアル。私は最初から始めて、それがこの問題を解決するかどうかを見ていきます。この時点までもう一度更新すると更新されます。 – eLowe

関連する問題