mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-30 21:08:30 +08:00
#657721 : added support for settings hints and lockMode
This commit is contained in:
parent
13cf6d9afb
commit
e2cb63429d
@ -6,10 +6,13 @@
|
||||
package com.mysema.query.jpa.impl;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.LockModeType;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
@ -42,7 +45,12 @@ public abstract class AbstractJPAQuery<Q extends AbstractJPAQuery<Q>> extends JP
|
||||
private static final Logger logger = LoggerFactory.getLogger(JPAQuery.class);
|
||||
|
||||
private final JPASessionHolder sessionHolder;
|
||||
|
||||
private final Map<String,Object> hints = new HashMap<String,Object>();
|
||||
|
||||
@Nullable
|
||||
private LockModeType lockMode;
|
||||
|
||||
public AbstractJPAQuery(EntityManager em) {
|
||||
this(new DefaultSessionHolder(em), HQLTemplates.DEFAULT, new DefaultQueryMetadata());
|
||||
}
|
||||
@ -111,6 +119,14 @@ public abstract class AbstractJPAQuery<Q extends AbstractJPAQuery<Q>> extends JP
|
||||
query.setFirstResult(modifiers.getOffset().intValue());
|
||||
}
|
||||
}
|
||||
|
||||
if (lockMode != null){
|
||||
query.setLockMode(lockMode);
|
||||
}
|
||||
|
||||
for (Map.Entry<String, Object> entry : hints.entrySet()){
|
||||
query.setHint(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
// set transformer, if necessary and possible
|
||||
List<? extends Expression<?>> projection = getMetadata().getProjection();
|
||||
@ -204,4 +220,18 @@ public abstract class AbstractJPAQuery<Q extends AbstractJPAQuery<Q>> extends JP
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Q setLockMode(LockModeType lockMode) {
|
||||
this.lockMode = lockMode;
|
||||
return (Q)this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Q setHint(String name, Object value){
|
||||
hints.put(name, value);
|
||||
return (Q)this;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -5,12 +5,14 @@
|
||||
*/
|
||||
package com.mysema.query;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.LockModeType;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@ -53,5 +55,29 @@ public abstract class AbstractJPATest extends AbstractStandardTest{
|
||||
assertNotNull(results);
|
||||
assertFalse(results.isEmpty());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void Hint(){
|
||||
javax.persistence.Query query = query().from(QCat.cat).setHint("org.hibernate.cacheable", true).createQuery(QCat.cat);
|
||||
assertTrue(query.getHints().containsKey("org.hibernate.cacheable"));
|
||||
assertFalse(query.getResultList().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Hint2(){
|
||||
assertFalse(query().from(QCat.cat).setHint("org.hibernate.cacheable", true).list(QCat.cat).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void LockMode(){
|
||||
javax.persistence.Query query = query().from(QCat.cat).setLockMode(LockModeType.READ).createQuery(QCat.cat);
|
||||
assertTrue(query.getLockMode().equals(LockModeType.READ));
|
||||
assertFalse(query.getResultList().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void LockMode2(){
|
||||
assertFalse(query().from(QCat.cat).setLockMode(LockModeType.READ).list(QCat.cat).isEmpty());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -25,7 +25,6 @@ import com.mysema.query.annotations.Config;
|
||||
import com.mysema.query.codegen.SerializerConfig;
|
||||
import com.mysema.query.codegen.SimpleSerializerConfig;
|
||||
import com.mysema.query.jpa.domain.Domain;
|
||||
import com.mysema.query.jpa.domain2.Domain2;
|
||||
|
||||
public class HibernateDomainExporterTest {
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user