mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
Merge pull request #1073 from querydsl/i1071
Respect the user-specified existing templates during JPAQuery.clone()
This commit is contained in:
commit
daad4b4e84
@ -355,13 +355,23 @@ public abstract class AbstractJPAQuery<Q extends AbstractJPAQuery<Q>> extends JP
|
||||
*/
|
||||
public abstract Q clone(EntityManager entityManager);
|
||||
|
||||
/**
|
||||
* Clone the state of this query to a new instance with the given EntityManager
|
||||
* and the specified templates
|
||||
*
|
||||
* @param entityManager
|
||||
* @param templates
|
||||
* @return
|
||||
*/
|
||||
public abstract Q clone(EntityManager entityManager, JPQLTemplates templates);
|
||||
|
||||
/**
|
||||
* Clone the state of this query to a new instance
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Q clone() {
|
||||
return this.clone(this.entityManager);
|
||||
return clone(entityManager, getTemplates());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -74,10 +74,16 @@ public class JPAQuery extends AbstractJPAQuery<JPAQuery> {
|
||||
super(em, templates, metadata);
|
||||
}
|
||||
|
||||
public JPAQuery clone(EntityManager entityManager) {
|
||||
JPAQuery q = new JPAQuery(entityManager, JPAProvider.getTemplates(entityManager), getMetadata().clone());
|
||||
@Override
|
||||
public JPAQuery clone(EntityManager entityManager, JPQLTemplates templates) {
|
||||
JPAQuery q = new JPAQuery(entityManager, templates, getMetadata().clone());
|
||||
q.clone(this);
|
||||
return q;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JPAQuery clone(EntityManager entityManager) {
|
||||
return clone(entityManager, JPAProvider.getTemplates(entityManager));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -13,29 +13,41 @@
|
||||
*/
|
||||
package com.mysema.query;
|
||||
|
||||
import static com.mysema.query.support.Expressions.numberOperation;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.mysema.query.jpa.HQLTemplates;
|
||||
import com.mysema.query.jpa.JPQLTemplates;
|
||||
import com.mysema.query.jpa.domain.QCat;
|
||||
import com.mysema.query.jpa.impl.JPAQuery;
|
||||
import com.mysema.query.types.OperatorImpl;
|
||||
import com.mysema.testutil.JPATestRunner;
|
||||
|
||||
@Ignore
|
||||
@RunWith(JPATestRunner.class)
|
||||
public class JPAQueryMutabilityTest implements JPATest {
|
||||
|
||||
private EntityManager entityManager;
|
||||
|
||||
private final OperatorImpl<Integer> customOperator = new OperatorImpl<Integer>("CUSTOM", "SIGN");
|
||||
|
||||
private final JPQLTemplates customTemplates = new HQLTemplates() {{
|
||||
add(customOperator, "sign({0})");
|
||||
}};
|
||||
|
||||
protected JPAQuery query() {
|
||||
return new JPAQuery(entityManager);
|
||||
}
|
||||
|
||||
protected JPAQuery query(JPQLTemplates templates) {
|
||||
return new JPAQuery(entityManager, templates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEntityManager(EntityManager entityManager) {
|
||||
this.entityManager = entityManager;
|
||||
@ -88,6 +100,33 @@ public class JPAQueryMutabilityTest implements JPATest {
|
||||
query2.list(cat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Clone_Custom_Templates() {
|
||||
QCat cat = QCat.cat;
|
||||
JPAQuery query = query().from(cat);
|
||||
//attach using the custom templates
|
||||
query.clone(entityManager, customTemplates)
|
||||
.uniqueResult(numberOperation(Integer.class, customOperator, cat.floatProperty));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Clone_Keep_Templates() {
|
||||
QCat cat = QCat.cat;
|
||||
JPAQuery query = query(customTemplates).from(cat);
|
||||
//keep the original templates
|
||||
query.clone()
|
||||
.uniqueResult(numberOperation(Integer.class, customOperator, cat.floatProperty));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void Clone_Lose_Templates() {
|
||||
QCat cat = QCat.cat;
|
||||
JPAQuery query = query(customTemplates).from(cat);
|
||||
//clone using the entitymanager's default templates
|
||||
query.clone(entityManager)
|
||||
.uniqueResult(numberOperation(Integer.class, customOperator, cat.floatProperty));
|
||||
}
|
||||
|
||||
private void assertProjectionEmpty(JPAQuery query) {
|
||||
assertTrue(query.getMetadata().getProjection().isEmpty());
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user