2013-11-04 4 views
6

पर अन्य कस्टम फ़ंक्शंस http://docs.spring.io/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.custom-implementations स्प्रिंग प्रलेखन सभी रिपॉजिटरीज़ या एकल रिपॉजिटरीज़ में कस्टम कार्यक्षमताओं को जोड़ने के लिए उदाहरण देता है, दोनों नहीं।स्प्रिंग जेपीए सभी रिपॉजिटरीज़ को कस्टम कार्यक्षमता जोड़ते हैं और साथ ही साथ एक ही रिपोजिटरी

मान लीजिए कि मैं सभी रिपॉजिटरीज़ (कस्टम रिपोजिटरी फैक्टरी बीन का उपयोग करके) और कुछ अन्य को केवल एक ही रिपॉजिटरीज़ (डॉक्स कहते हैं कि एक कस्टम इंटरफेस और कस्टम इंपल) का उपयोग करने के लिए कुछ कस्टम फनक्स जोड़ना चाहते हैं; इसे कैसे प्राप्त किया जा सकता है?

कुछ उदाहरण कोड जहां मैंने सभी भंडारों में "setCurrentTenansInSession" विधि जोड़ा; अब मैं एक कस्टम विधि जोड़ना चाहता हूं, उदा। "newCustomMethod", ओना एकल भंडार (जो एक MyJpaRepository है, मेरे कस्टम भंडार कारखाने के लिए)। मैं यह कैसे करु?

कस्टम व्यवहार इंटरफ़ेस:

@NoRepositoryBean 
public interface MyJpaRepository<T, ID extends Serializable> extends JpaRepository<T, ID> { 
    public void setCurrentTenantInSession(Object object);  
} 

कस्टम व्यवहार कार्यान्वयन:

public class MultiTenantSimpleJpaRepository<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements MyJpaRepository<T, ID> { 
    public void setCurrentTenantInSession(Object object) { 
     //custom impl 
    } 
} 

कस्टम भंडार कारखाने सेम:

public class MultiTenantJpaRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends JpaRepositoryFactoryBean<T, S, ID> { 

    @Override 
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) { 
     return new MultiTenantJpaRepositoryFactory(entityManager); 
    } 
} 

और अंत में कस्टम भंडार कारखाना:

public class MultiTenantJpaRepositoryFactory extends JpaRepositoryFactory { 
    public MultiTenantJpaRepositoryFactory(EntityManager entityManager) { 
     super(entityManager); 
    } 

    @Override 
    protected JpaRepository<?, ?> getTargetRepository(RepositoryMetadata metadata, EntityManager entityManager) { 
     final JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType()); 

     final SimpleJpaRepository<?, ?> repo = new MultiTenantSimpleJpaRepository(entityInformation, entityManager); 

     repo.setLockMetadataProvider(LockModeRepositoryPostProcessor.INSTANCE.getLockMetadataProvider()); 
     return repo; 
    } 

    @Override 
    protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { 
     return MultiTenantSimpleJpaRepository.class; 
    } 
} 

उत्तर

11

आपको बस उल्लेख किए गए दस्तावेज़ पृष्ठ पर दृष्टिकोण को गठबंधन करने की आवश्यकता है। Car वह इकाई बनें जिसके लिए आप एक कस्टम भंडार चाहते हैं।

CommonCustomRepository तरीकों सभी रेपोस को जोड़ा गया परिभाषित करता है:

@NoRepositoryBean 
public interface CommonCustomRepository<T, ID extends Serializable> extends JpaRepository<T, ID> { 
    String getCustomValue(); 
} 

इस रेपो के लिए कार्यान्वयन: के लिए CarRepository

@NoRepositoryBean 
public interface CustomCarRepository { 

    public String getCustomCarValue(); 
} 

कस्टम कार- का कार्यान्वयन

public class CommonCustomRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements CommonCustomRepository<T, ID> { 

    public CommonCustomRepositoryImpl(Class<T> domainClass, EntityManager em) { 
     super(domainClass, em); 
    } 

    public CommonCustomRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, 
      EntityManager entityManager) { 
     super(entityInformation, entityManager); 
    } 

    @Override 
    public String getCustomValue() { 
     return "CustomValue"; 
    } 

} 

कस्टम तरीकों संबंधित विधियां

public class CarRepositoryImpl implements CustomCarRepository { 

    @PersistenceContext 
    private EntityManager em; 

    @Override 
    public String getCustomCarValue() { 
     return "CustomCarValue"; 
    } 
} 

CarRepository

public interface CarRepository extends CommonCustomRepository<Car, Long>, CustomCarRepository { 
} 

कस्टम रेपो कारखाने के लिए संयुक्त इंटरफेस, बस प्रलेखन

public class CustomRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable> extends 
    JpaRepositoryFactoryBean<R, T, I> { 

    @Override 
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) { 

     return new CustomRepositoryFactory(entityManager); 
    } 

    private static class CustomRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory { 

     private EntityManager entityManager; 

     public CustomRepositoryFactory(EntityManager entityManager) { 
      super(entityManager); 

      this.entityManager = entityManager; 
     } 

     @Override 
     protected Object getTargetRepository(RepositoryMetadata metadata) { 

      return new CommonCustomRepositoryImpl<T, I>((Class<T>) metadata.getDomainType(), entityManager); 
     } 

     @Override 
     protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { 

      // The RepositoryMetadata can be safely ignored, it is used by the JpaRepositoryFactory 
      // to check for QueryDslJpaRepository's which is out of scope. 
      return CommonCustomRepositoryImpl.class; 
     } 
    } 
} 

विन्यास के अंतिम बिट, बस डॉक्स में की तरह की तरह

<jpa:repositories base-package="com.example" factory-class="com.example.CustomRepositoryFactoryBean"/> 
+0

बहुत धन्यवाद kkamenev! – lincetto

+2

'वसंत-डेटा-कॉमन्स '1.11 के रूप में' jpa: repositories' के लिए' फैक्ट्री-क्लास 'विशेषता को बहिष्कृत करने के बजाय 'बेस-क्लास =" com.example.CommonCustomRepositoryImpl "निर्दिष्ट करें। बेस क्लास निर्दिष्ट करते समय कारखाने की आवश्यकता नहीं है। –

संबंधित मुद्दे