Default template of JPAQueryFactory differs from one provided by JPAQuery #398

Closed
TuomasKiviaho opened this Issue Apr 17, 2013 · 6 comments

Comments

Projects
None yet
2 participants
@TuomasKiviaho
Contributor

TuomasKiviaho commented Apr 17, 2013

#72 started for some reason to haunt me again and I figured out that JPAQueryFactory and JPAQuery constructors are not coherent on what templates to use as default. JPAQueryFactory always resolutes to JPQL standard template whereas JPAQuery is smart enough to seek advice from entity manager.

I think that the best solution would be to postpone choosing of default template until entity manager is available. As a side note I also noticed that JPAQuery#clone is still forgetting to tag along the chosen template (mentioned in the referenced issue).

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Apr 17, 2013

Member

As a side note I also noticed that JPAQuery#clone is still forgetting to tag along the chosen template (mentioned in the referenced issue).

clone takes the JPQLTemplates variant that fits best the given EntityManager

JPAQuery q = new JPAQuery(entityManager, JPAProvider.getTemplates(entityManager), getMetadata().clone());

Since clone is meant to be used for detached queries, this IMHO makes sense.

Here is the commit where this change happened 69ea056

Member

timowest commented Apr 17, 2013

As a side note I also noticed that JPAQuery#clone is still forgetting to tag along the chosen template (mentioned in the referenced issue).

clone takes the JPQLTemplates variant that fits best the given EntityManager

JPAQuery q = new JPAQuery(entityManager, JPAProvider.getTemplates(entityManager), getMetadata().clone());

Since clone is meant to be used for detached queries, this IMHO makes sense.

Here is the commit where this change happened 69ea056

@TuomasKiviaho

This comment has been minimized.

Show comment
Hide comment
@TuomasKiviaho

TuomasKiviaho Apr 18, 2013

Contributor

Thanks for the clarification,

I didn't think the cloning issue though I guess, sorry about that. Now there's only the main issue and here's a patch for it.

diff --git src/main/java/com/mysema/query/jpa/impl/JPAQueryFactory.java src/main/java/com/mysema/query/jpa/impl/JPAQueryFactory.java
index 06b835d..e33ed05 100644
--- src/main/java/com/mysema/query/jpa/impl/JPAQueryFactory.java
+++ src/main/java/com/mysema/query/jpa/impl/JPAQueryFactory.java
@@ -34,17 +34,17 @@
     private final Provider<EntityManager> entityManager;

     public JPAQueryFactory(Provider<EntityManager> entityManager) {
-        this(JPQLTemplates.DEFAULT, entityManager);
+        this.entityManager = entityManager;
     }

     public JPAQueryFactory(JPQLTemplates templates, Provider<EntityManager> entityManager) {
-        this.entityManager = entityManager;
+        this(entityManager);
         this.templates = templates;
     }

     @Override
     public JPADeleteClause delete(EntityPath<?> path) {
-        return new JPADeleteClause(entityManager.get(), path, templates);
+        return templates == null ? new JPADeleteClause(entityManager.get(), path) : new JPADeleteClause(entityManager.get(), path, templates);
     }

     @Override
@@ -54,12 +54,12 @@

     @Override
     public JPAUpdateClause update(EntityPath<?> path) {
-        return new JPAUpdateClause(entityManager.get(), path, templates);
+        return templates == null ? new JPAUpdateClause(entityManager.get(), path) : new JPAUpdateClause(entityManager.get(), path, templates);
     }

     @Override
     public JPAQuery query() {
-        return new JPAQuery(entityManager.get(), templates);
+        return templates == null ? new JPAQuery(entityManager.get()) : new JPAQuery(entityManager.get(), templates);
     }

     @Override
Contributor

TuomasKiviaho commented Apr 18, 2013

Thanks for the clarification,

I didn't think the cloning issue though I guess, sorry about that. Now there's only the main issue and here's a patch for it.

diff --git src/main/java/com/mysema/query/jpa/impl/JPAQueryFactory.java src/main/java/com/mysema/query/jpa/impl/JPAQueryFactory.java
index 06b835d..e33ed05 100644
--- src/main/java/com/mysema/query/jpa/impl/JPAQueryFactory.java
+++ src/main/java/com/mysema/query/jpa/impl/JPAQueryFactory.java
@@ -34,17 +34,17 @@
     private final Provider<EntityManager> entityManager;

     public JPAQueryFactory(Provider<EntityManager> entityManager) {
-        this(JPQLTemplates.DEFAULT, entityManager);
+        this.entityManager = entityManager;
     }

     public JPAQueryFactory(JPQLTemplates templates, Provider<EntityManager> entityManager) {
-        this.entityManager = entityManager;
+        this(entityManager);
         this.templates = templates;
     }

     @Override
     public JPADeleteClause delete(EntityPath<?> path) {
-        return new JPADeleteClause(entityManager.get(), path, templates);
+        return templates == null ? new JPADeleteClause(entityManager.get(), path) : new JPADeleteClause(entityManager.get(), path, templates);
     }

     @Override
@@ -54,12 +54,12 @@

     @Override
     public JPAUpdateClause update(EntityPath<?> path) {
-        return new JPAUpdateClause(entityManager.get(), path, templates);
+        return templates == null ? new JPAUpdateClause(entityManager.get(), path) : new JPAUpdateClause(entityManager.get(), path, templates);
     }

     @Override
     public JPAQuery query() {
-        return new JPAQuery(entityManager.get(), templates);
+        return templates == null ? new JPAQuery(entityManager.get()) : new JPAQuery(entityManager.get(), templates);
     }

     @Override
@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Apr 18, 2013

Member

Looks quite similar to this one 6436828

Member

timowest commented Apr 18, 2013

Looks quite similar to this one 6436828

@TuomasKiviaho

This comment has been minimized.

Show comment
Hide comment
@TuomasKiviaho

TuomasKiviaho Apr 18, 2013

Contributor

Thanks a bunch

Great Minds Think Alike, but Fools Seldom Differ

Contributor

TuomasKiviaho commented Apr 18, 2013

Thanks a bunch

Great Minds Think Alike, but Fools Seldom Differ

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Apr 18, 2013

Member

I'll close it, when it's released.

Member

timowest commented Apr 18, 2013

I'll close it, when it's released.

@timowest timowest reopened this Apr 18, 2013

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest May 27, 2013

Member

Released in 3.2.0

Member

timowest commented May 27, 2013

Released in 3.2.0

@timowest timowest closed this May 27, 2013

@timowest timowest added this to the 3.2.0 milestone Apr 14, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment