2016-11-17 63 views
0

無法爲我給此問題的查詢創建正確的itnerface。Spring Boot findByIdIn內部列表

我有這樣的實體:

public class TwoEntity extends BaseEntity implements Serializable { 

private static final long serialVersionUID = 1L; 

/* 
* ATTRIBUTE 
*/ 
private String groupName; 

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
private List<OneEntity> oneList; 

而且渣滓Repositoy:

public interface TwoRepository extends CrudRepository<TwoEntity, Long> { 

TwoEntityfindById(Long id); 

TwoEntityfindByGroupName(String groupName); 

List<TwoEntity> findBy????(OneEntity oe); 

我的目標是讓所有TwoEntities其中OneEntitie是TwoEntity列表中選擇一個元素。 我使用Spring引導和Hibernate來實現這一點。我不能從我的數據庫中刪除OneEntity對象,因爲TwoEntity在列表中有OneEntity作爲ForeignKey。

有沒有辦法讓它與接口中給定的工具一起工作? 一個可用的關鍵字列表可以在這裏找到:spring docs for crud

/E

我想我有一個錯誤的架構。目前,我在這些實體之間有一個單向關係。我想我必須使這些實體雙向並刪除他們與oneList.setList(null) manuelle。

但我不是100%確定。打開輸入。

回答

1

您可以使用此:

List<TwoEntity> findByOneList_Id(Long oneEntityId) 

但是你需要從JpaRepository

public interface TwoRepository extends JpaRepository<TwoEntity, Long> { 

擴展的方法將被轉換爲

twoEntity.oneList.id 

這裏官方文檔

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions

+0

噢,謝謝。在Hibernate中測試了這個語法,它像一個魅力。非常感謝你。 – Offset