2017-07-06 75 views
0

我使用spring-data-jpa來訪問我的數據。我需要一種方法來分離一個對象並將其存儲爲一個新的數據庫行。我目前的方法是將detach方法添加到存儲庫,但爲此,我需要一個EntityManager。我還沒有找到一個(很好)獲得它的方法...任何想法?在單個存儲庫中使用entityManager

@Repository 
public interface InteractionRepository 
     extends JpaRepository<Interaction, Long>, 
       DetatchableItemRepository{} 


public interface DetatchableItemRepository { 
    void detach(Interaction clone); 
} 


public class DetatchableItemRepositoryImpl implements DetatchableItemRepository { 

    @Autowired 
    EntityManager em; 

    public void detach(Interaction clone) { 
     em.detach(clone); 
     clone.id=null; 
     em.persist(clone); 

    } 
} 

然而,春死與此錯誤:

  • 造成的:org.springframework.beans.factory.BeanCreationException:錯誤創建名稱爲豆 'interactionRepository':init方法的調用失敗;嵌套的異常是org.springframework.data.mapping.PropertyReferenceException:沒有找到類型交互的屬性分離!

  • 引起:org.springframework.data.mapping.PropertyReferenceException:找不到屬性分離類型交互!

回答

0

你用錯誤的名稱約定的定製庫,試試這個:

public interface DetatchableItemRepositoryCustom { 
     void detach(Interaction clone); 
    } 


public interface DetatchableItemRepository extends JpaRepository<Interaction, Long>, 
             DetatchableItemRepositoryCustom { 

} 

public class DetatchableItemRepositoryImpl implements DetatchableItemRepositoryCustom { 
} 

春天數據使用的名稱約定定製的倉庫,主要儲存庫(見有關名稱Adding custom behavior to single repositories

如果你。有一些SomeRepository,它擴展了一些基本的Spring數據存儲庫,並且想要添加自定義行爲,那麼它應該是這樣的:

interface SomeRepositoryCustom{ 
     someMethod(); 
    }  
    //XXXRepository - any base spring data repository 
    interface SomeRepository extends<T ,ID> extend XXXRepository , SomeRepositoryCustom { 
     ....... 
    } 

    public class ARepositoryImpl implement SomeRepositoryCustom{ 
     @Overide 
     someMethod(){ 
     .... 
    } 
相關問題