Best practice to get JPAQueryFactory

4,007 views
Skip to first unread message

fasfsfgs

unread,
Jun 6, 2015, 9:06:37 PM6/6/15
to Querydsl on behalf of Timo Westkämper
Hello Querydsl people!

I've been using an old version of Querydsl for a couple years and now I'm finally upgrading my environment. One of the updates I'm doing is Querydsl.

I have already successfully done the upgrade and after reading the docs, Querydsl suggests using JPAQueryFactory to get JPAQuery instances.

My questions is: What is the best practice to do this inside my DAOs?

I'm using Spring to provide me the entity manager.

  @PersistenceContext
  private EntityManager entityManager;

Thanks for any ideas you guys can give me.

fasfsfgs

unread,
Jun 6, 2015, 9:11:11 PM6/6/15
to Querydsl on behalf of Timo Westkämper
Oh. I'm using Querydsl 4.0.0 btw.

timowest

unread,
Jun 7, 2015, 3:49:15 PM6/7/15
to quer...@googlegroups.com
You can inject a single JPAQueryFactory instance and use the following constructor to create it

@Bean
public JPAQueryFactory jpaQueryFactory() {
    return new JPAQueryFactory(entityManager);
}

Timo


On Sunday, June 7, 2015 at 4:11:11 AM UTC+3, fasfsfgs wrote:
Oh. I'm using Querydsl 4.0.0 btw.

fasfsfgs

unread,
Jun 7, 2015, 6:47:25 PM6/7/15
to Querydsl on behalf of timowest
I couldn't make that work.
From what I'm seeing here, entityManager is not a spring bean I can just inject anywhere.

When I do what you said, I get a "No qualifying bean of type ..." exception from Spring. The Spring way (actually I think it's the JPA way) of getting EntityManager is thru @PersistenceContext. I may be wrong since I'm very new to this whole JPA specification.

I'm already using Querydsl, but with new JPAQuery(em) but I want to use it the recommended way.
I'll find some time to make a small project to show this example and post it here.

Thanks for the help so far. =)

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

timowest

unread,
Jun 8, 2015, 1:40:02 PM6/8/15
to quer...@googlegroups.com
How did you inject the EntityManager into the JPAQueryFactory? Also could you provide the full error message?

Did you try something like 

@Configuration
public class QuerydslConfiguration {

    @PersistenceContext
    private EntityManager entityManager;

    @Bean
    public JPAQueryFactory jpaQueryFactory() {
        return new JPAQueryFactory(entityManager);
    }
}

On Monday, June 8, 2015 at 1:47:25 AM UTC+3, fasfsfgs wrote:
I couldn't make that work.
From what I'm seeing here, entityManager is not a spring bean I can just inject anywhere.

When I do what you said, I get a "No qualifying bean of type ..." exception from Spring. The Spring way (actually I think it's the JPA way) of getting EntityManager is thru @PersistenceContext. I may be wrong since I'm very new to this whole JPA specification.

I'm already using Querydsl, but with new JPAQuery(em) but I want to use it the recommended way.
I'll find some time to make a small project to show this example and post it here.

Thanks for the help so far. =)
On Sun, Jun 7, 2015 at 4:49 PM, timowest via Querydsl <querydsl+APn2wQefkk7kI02e0xPnNp6yrNOiXUEZ44_gIyQiFakJHtII_UP_1p5@googlegroups.com> wrote:
You can inject a single JPAQueryFactory instance and use the following constructor to create it

@Bean
public JPAQueryFactory jpaQueryFactory() {
    return new JPAQueryFactory(entityManager);
}

Timo

On Sunday, June 7, 2015 at 4:11:11 AM UTC+3, fasfsfgs wrote:
Oh. I'm using Querydsl 4.0.0 btw.

On Sat, Jun 6, 2015 at 10:06 PM, fasfsfgs wrote:
Hello Querydsl people!

I've been using an old version of Querydsl for a couple years and now I'm finally upgrading my environment. One of the updates I'm doing is Querydsl.

I have already successfully done the upgrade and after reading the docs, Querydsl suggests using JPAQueryFactory to get JPAQuery instances.

My questions is: What is the best practice to do this inside my DAOs?

I'm using Spring to provide me the entity manager.

  @PersistenceContext
  private EntityManager entityManager;

Thanks for any ideas you guys can give me.


--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+unsubscribe@googlegroups.com.

fasfsfgs

unread,
Jun 14, 2015, 10:31:03 AM6/14/15
to Querydsl on behalf of timowest
Thanks for the ideas Timo.
I managed to put it all together with your help.

The problem is that we are discussing like JPAQueryFactory needs an EntityManager, which is injectable the way you said. But in reality it needs a Provider<EntityManager>.
To use Provider, EntityManager has to be a bean, which like I said, I don't think it is.

The following code works:

@Configuration
public class QuerydslConfig {
 
  @PersistenceContext
  private EntityManager em;
 
  @Bean
  public JPAQueryFactory getJPAQueryFactory() {
    Provider<EntityManager> provider = new Provider<EntityManager>() {
      @Override
      public EntityManager get() {
        return em;
      }
    };
 
    return new JPAQueryFactory(provider);
  }
 
}

Is this what you recommend? I'm now failing to understand why JPAQueryFactory needs a Provider instead of the EntityManager itself, since I think em is already aware of its context.

If I use:
@PersistenceContext
private Provider<EntityManager> em;
I get the following error:
Caused by: java.lang.IllegalStateException: Specified field type [interface javax.inject.Provider] is incompatible with resource type [javax.persistence.EntityManager]

If I use:
@Inject
private Provider<EntityManager> em;
I get the following error:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.


On Mon, Jun 8, 2015 at 2:40 PM, timowest via Querydsl <querydsl+APn2wQefkk7kI02e0xPnNp6...@googlegroups.com> wrote:
How did you inject the EntityManager into the JPAQueryFactory? Also could you provide the full error message?

Did you try something like 

@Configuration
public class QuerydslConfiguration {

    @PersistenceContext
    private EntityManager entityManager;

    @Bean
    public JPAQueryFactory jpaQueryFactory() {
        return new JPAQueryFactory(entityManager);
    }
}

On Monday, June 8, 2015 at 1:47:25 AM UTC+3, fasfsfgs wrote:
I couldn't make that work.
From what I'm seeing here, entityManager is not a spring bean I can just inject anywhere.

When I do what you said, I get a "No qualifying bean of type ..." exception from Spring. The Spring way (actually I think it's the JPA way) of getting EntityManager is thru @PersistenceContext. I may be wrong since I'm very new to this whole JPA specification.

I'm already using Querydsl, but with new JPAQuery(em) but I want to use it the recommended way.
I'll find some time to make a small project to show this example and post it here.

Thanks for the help so far. =)
On Sun, Jun 7, 2015 at 4:49 PM, timowest via Querydsl <querydsl+APn2wQefkk7kI02e0xPnNp6...@googlegroups.com> wrote:
You can inject a single JPAQueryFactory instance and use the following constructor to create it

@Bean
public JPAQueryFactory jpaQueryFactory() {
    return new JPAQueryFactory(entityManager);
}

Timo

On Sunday, June 7, 2015 at 4:11:11 AM UTC+3, fasfsfgs wrote:
Oh. I'm using Querydsl 4.0.0 btw.

On Sat, Jun 6, 2015 at 10:06 PM, fasfsfgs wrote:
Hello Querydsl people!

I've been using an old version of Querydsl for a couple years and now I'm finally upgrading my environment. One of the updates I'm doing is Querydsl.

I have already successfully done the upgrade and after reading the docs, Querydsl suggests using JPAQueryFactory to get JPAQuery instances.

My questions is: What is the best practice to do this inside my DAOs?

I'm using Spring to provide me the entity manager.

  @PersistenceContext
  private EntityManager entityManager;

Thanks for any ideas you guys can give me.


--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.

fasfsfgs

unread,
Jun 14, 2015, 3:16:32 PM6/14/15
to Querydsl on behalf of timowest
Never-mind Timo!

I just saw 4.0.1 "fixes" this and enables us to build the factory with EntityManager instead of a provider.
Nice!

Thanks!
Reply all
Reply to author
Forward
0 new messages