Injecting a QueryBuilder vs adapting from a resource resolver? | Community
Skip to main content
Level 4
June 23, 2016

Injecting a QueryBuilder vs adapting from a resource resolver?

  • June 23, 2016
  • 1 reply
  • 3343 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

smacdonald2008
Level 10
June 23, 2016

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. 

Ratna_Kumar
Level 10
June 27, 2016

Hi,

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

Thanks.