2017-06-16 87 views
0

陷入試圖獲取從的最新記錄加入 我有以下類JPA @OneToMany按日期獲取最新的記錄從加入

作者

@Entity 
    @Table(name = "author") 
    public class Author { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int id; 
    @Column(name = "name") 
    private String name; 
    @OneToMany 
    @JoinColumn(name = "author_id", referencedColumnName = "id") 
    @OrderBy("id Desc") 
    private List<Book> books; 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public List<Book> getBooks() { 
     return books; 
    } 
    public void setBooks(List<Book> books) { 
     this.books = books; 
    } 
    } 

@Entity 
    @Table(name = "book") 
    public class Book { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int id; 
    @Column(name = "author_id") 
    private Integer authorId; 
    @Column(name = "date_published") 
    private Date datePublished; 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public Integer getAuthorId() { 
     return authorId; 
    } 
    public void setAuthorId(Integer authorId) { 
     this.authorId = authorId; 
    } 
    public Date getDatePublished() { 
     return datePublished; 
    } 
    public void setDatePublished(Date datePublished) { 
     this.datePublished = datePublished; 
    } 
    } 

存儲庫

@Repository 
    public interface AuthorRepository extends 
    JpaRepository<Author, Long> { 

     public Page<Author> findALL(int id, Pageable pageable); 

    } 

目前的結果

{ 
     "id": 1, 
     "name": "James", 
     "books":[ 
     { 
     "id": 1, 
     "name": "book1", 
     "datePublished": '12/12/2012' 

     }, 
     { 
     "id": 1, 
     "name": "book2", 
     "datePublished": '01/02/2013' 
     }] 
    }, 

    { 
     "id": 2, 
     "name": "Tim", 
     "books":[ 
     { 
     "id": 5, 
     "name": "book5", 
     "datePublished": '12/12/2014' 

     },{ 
      "id": 6, 
      "name": "book6", 
      "datePublished": '01/02/2015' 

     }] 
    } 

預期結果

{ 
     "id": 1, 
     "name": "James", 
     "books":[ 
     { 
     "id": 1, 
     "name": "book2", 
     "datePublished": '01/02/2013' 
     }] 
    }, 
    { 
    "id": 2, 
    "name": "Tim", 
    "books":[ 
    { 
     "id": 6, 
     "name": "book6", 
     "datePublished": '01/02/2015' 

    }] 
    } 

從這個的作者名單正在與他們各自所有的書回來。

問題是JPA如何幫助我根據發佈日期從集合中選擇最新的書。

+0

您能詳細解釋一下您的問題嗎?「JPA如何幫助我根據發佈日期從集合中選擇最新的書籍?」? –

+0

只是增加了當前和預期的結果。希望能夠說明一點。可能如果它有可能爲每個作者的書籍集合做一個最大日期 –

+0

因此,基本上,你試圖從每本用戶的書籍列表中獲得最新書籍(單本書)? –

回答

1

如果您使用休眠,您可以使用@JoinFormula來實現這一點,以按日期映射最新記錄。例如:

@ManyToOne(fetch = FetchType.LAZY) 
@JoinFormula("(" + 
    "SELECT b.id " + 
    "FROM book b " + 
    "WHERE b.author_id = id " + 
    "ORDER BY b.date_published DESC " + 
    "LIMIT 1" + 
")") 
private Book latestBook; 
+0

謝謝我參考這個網站https://vladmihalcea.com/2017/02/16/how-to-map-the-latest-child-of-a-parent-entity-using-hibernate-joinformula/ –

+0

由於延遲加載而出現以下錯誤「找不到類org.hibernate.proxy.pojo.javassist.Javassist的序列化程序」,但被以下問題協助:https://stackoverflow.com/questions/24994440/no-serializer-found - 用於類-ORG-休眠 - 代理POJO的了Javassist-了Javassist。我不知道它有什麼作用? –

0

,如果你想獲得最後一本書的每個作者,您可以添加瞬變場得到它:

@Entity 
    @Table(name = "author") 
    public class Author { 
    ....... 
    @Transient 
    private Book lastBook; 

    @PostLoad 
    private void setLastBook() { 
    if(books==null || books,isEmpty()) 
    this.lastBook=null; 
    }else{ 
    this.lastBook=books.get(0); 
    } 
} 

或使其一一對應,並通過與@PostPersist和註解同樣的方法將其保存在數據庫@PostUpdate。它會自動保存在數據庫中的最後一本書