Expand my Community achievements bar.

Injecting a QueryBuilder vs adapting from a resource resolver?

Avatar

Level 5

When using the querybuilder, what's the difference if any, between injecting he querybuilder services, versus adapting from a resource resolver?

Since you must call createQuery() with a Session, I think the result will likely be the same for both of these cases. Is there any particular version that considered "best practice?"

@Model(adaptables = SlingHttpServletRequest.class) public class MyClass { @OSGiService QueryBuilder qb; @Inject SlingHttpServletRequest request; public void myMethod() { //...build out predicates ResourceResolver resourceResolver = request.getResourceResolver(); Session session = resourceResolver.adaptTo(Session.class); Query query = qb.createQuery(pg,session); } }

vs

@Model(adaptables = SlingHttpServletRequest.class) public class MyClass { @Inject SlingHttpServletRequest request; public void myMethod() { //...build out predicates ResourceResolver resourceResolver = request.getResourceResolver(); Session session = resourceResolver.adaptTo(Session.class); QueryBuilder qb = resourceResolver.adaptTo(Querybuilder.class); Query query = qb.createQuery(pg,session); } }

Thanks

2 Replies

Avatar

Level 10

You are correct - they both give you access to the JCR in order to use Query Builder.

Dependency Injection is a standard when using OSGi. 

Here is a good community article on it. 

https://www.credera.com/blog/custom-application-development/dependency-injection-part-1-need/

 I do not think there is a performance gain either way. 

Avatar

Level 10

Hi,

Pls let us know, if your issue has been solved.

Thanks.