Class RepositoryContribution
- java.lang.Object
-
- com.broadleafcommerce.data.tracking.core.RepositoryContribution
-
- All Implemented Interfaces:
org.springframework.core.Ordered
public class RepositoryContribution extends Object implements org.springframework.core.Ordered
Beans of this type allow for contribution of repository fragments to out-of-the-box framework repositories. The system will pick up beans of this type and attempt to match thebaseRepositoryInterfacewith an existing repository base interface type. If found, theconcreteFragmentsandqueryFragmentswill be added to the repositories list of fragments and will be available to call at runtime.
Note, since these fragments are not directly part of the base repository interface, usage requires casting the repository instance to the desired fragment interface in order to exercise its API. For example:((MyFragmentInterface) productRepository).myMethod(..);Note, if overriding a method that already exists in the repository, it is not necessary to cast to your fragment interface. A call to the original method signature without the cast will still result in your fragment implementation being called.
UsewithBaseRepositoryInterface(Class)to match for exact repository classes, or match for castable interfaces (e.g.TrackableRepository). UsewithBaseRepositoryInterfaceMatchRegex(String)to match using a regex pattern against repository fully qualified classnames. Note, you may set either or both of these match parameters to fine tune how repositories are chosen (e.g. match allTrackableRepositoryrepositories in a particular package).
For cases where multiple repositories intentionally match for the same fragment, it is often useful to determine domain type information at runtime so that the correct type can be queried in your fragment. TheRepositoryContextcan be retrieved in your concrete method implementation (e.g.RepositoryContext.getContext()) and inspected to determine the correct domain type for the current repository. In turn, this information can be used to influence query construction, usually via JPA criteria. For example:... RepositoryContext context = RepositoryContext.getContext(); ClassdomainType = (Class ) context.getMetadata().getDomainType(); CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery criteria = criteriaBuilder.createQuery(domainType); Root entity = criteria.from(domainType); ... RepositoryContributioninstances implementOrdered. Use this feature to give precedence to one or more instances. This feature is useful in cases where you have provided a more general override, but in some cases you want to prefer a more specific override of the same method signature.
-
-
Constructor Summary
Constructors Constructor Description RepositoryContribution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleancanEqual(Object other)booleanequals(Object o)Class<?>getBaseRepositoryInterface()PatterngetBaseRepositoryInterfaceMatchRegex()Map<Class<Object>,Object>getConcreteFragments()intgetOrder()List<Class<?>>getQueryFragments()inthashCode()voidsetBaseRepositoryInterface(Class<?> baseRepositoryInterface)voidsetBaseRepositoryInterfaceMatchRegex(Pattern baseRepositoryInterfaceMatchRegex)voidsetConcreteFragments(Map<Class<?>,?> concreteFragments)voidsetOrder(int order)voidsetQueryFragments(List<Class<?>> queryFragments)StringtoString()RepositoryContributionwithBaseRepositoryInterface(Class<?> baseRepositoryInterface)Provide an interface that is equal to, or that the existing repository can be cast to.RepositoryContributionwithBaseRepositoryInterfaceMatchRegex(String regex)Provide a regex pattern that will match one or more fully qualified classnames of existing repositories.RepositoryContributionwithConcreteFragments(Map<Class<?>,?> fragments)Pass a map of relationships between fragment interface and implementation.RepositoryContributionwithOrder(int order)Set a precedence value for this contribution to cause it to be favored (or not) over others when considering the same method signature.RepositoryContributionwithQueryFragments(List<Class<?>> fragments)Pass a list of fragment interfaces whose method signatures inform dynamic query construction.
-
-
-
Method Detail
-
withConcreteFragments
public RepositoryContribution withConcreteFragments(Map<Class<?>,?> fragments)
Pass a map of relationships between fragment interface and implementation. The implementation is usually another bean with the appropriate EntityManager (and any other dependencies) already injected. Note, a method signature from a framework repository fragment for this same repository may be used and implemented here in order to provide an override of the framework functionality.- Type Parameters:
T- The interface type- Parameters:
fragments- Map of relationships between fragment interface and implementation- Returns:
- The RepositoryContribution instance with the concrete elements set
-
withQueryFragments
public RepositoryContribution withQueryFragments(List<Class<?>> fragments)
Pass a list of fragment interfaces whose method signatures inform dynamic query construction. These interface have no implementation counterpart. See https://www.baeldung.com/spring-data-derived-queries for more information on this topic.- Type Parameters:
T- The interface type- Parameters:
fragments- List of fragment interfaces whose method signatures inform dynamic query construction- Returns:
- The RepositoryContribution instance with the query method interface elements set
-
withBaseRepositoryInterface
public RepositoryContribution withBaseRepositoryInterface(Class<?> baseRepositoryInterface)
Provide an interface that is equal to, or that the existing repository can be cast to. If a repository is detected that matches either of these criteria, it is determined to be a match for this particular check.- Parameters:
baseRepositoryInterface- Interface that is equal to, or that the existing repository can be cast to- Returns:
- The RepositoryContribution instance with the match interface set
-
withBaseRepositoryInterfaceMatchRegex
public RepositoryContribution withBaseRepositoryInterfaceMatchRegex(String regex)
Provide a regex pattern that will match one or more fully qualified classnames of existing repositories. If a repository is detected whose fully qualified name matches the pattern, it is determined to be a match for this particular check.- Parameters:
regex- Regex pattern that will match one or more fully qualified classnames of existing repositories- Returns:
- The RepositoryContribution instance with the match regex set
-
withOrder
public RepositoryContribution withOrder(int order)
Set a precedence value for this contribution to cause it to be favored (or not) over others when considering the same method signature.- Parameters:
order- The order value for sorting RepositoryContribution instances. Lower values win. The default is 0.- Returns:
- The RepositoryContribution instance with the order set
-
getBaseRepositoryInterface
public Class<?> getBaseRepositoryInterface()
-
getBaseRepositoryInterfaceMatchRegex
public Pattern getBaseRepositoryInterfaceMatchRegex()
-
getOrder
public int getOrder()
- Specified by:
getOrderin interfaceorg.springframework.core.Ordered
-
setBaseRepositoryInterface
public void setBaseRepositoryInterface(Class<?> baseRepositoryInterface)
-
setBaseRepositoryInterfaceMatchRegex
public void setBaseRepositoryInterfaceMatchRegex(Pattern baseRepositoryInterfaceMatchRegex)
-
setOrder
public void setOrder(int order)
-
canEqual
protected boolean canEqual(Object other)
-
-