本文整理了Java中org.hibernate.query.Query.setLockOptions
方法的一些代码示例,展示了Query.setLockOptions
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.setLockOptions
方法的具体详情如下:
包路径:org.hibernate.query.Query
类名称:Query
方法名:setLockOptions
[英]Set the lock options for the query. Specifically only the following are taken into consideration:
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testQueryUsingLockOptions() {
// todo : need an association here to make sure the alias-specific lock modes are applied correctly
doInHibernate( this::sessionFactory, session -> {
session.createQuery( "from A a" )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
.uniqueResult();
session.createQuery( "from A a" )
.setLockOptions( new LockOptions().setAliasSpecificLockMode( "a", LockMode.PESSIMISTIC_WRITE ) )
.uniqueResult();
} );
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithUnionThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Vehicle> vehicles = session.createQuery( "select v from Vehicle v" )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
.getResultList();
assertEquals( 3, vehicles.size() );
assertEquals( 4, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Override
protected void applySkipLocked(Query query) {
query.setLockOptions(
new LockOptions( lockMode() ).setFollowOnLocking( false )
);
}
代码示例来源:origin: hibernate/hibernate-orm
protected void applySkipLocked(Query query) {
query.setLockOptions(
new LockOptions( lockMode() )
.setTimeOut( LockOptions.SKIP_LOCKED )
);
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithCountDistinctThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products = session.createQuery(
"select p from Product p where ( select count(distinct p1.id) from Product p1 ) > 0 ", Product.class )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ).setFollowOnLocking( false ) )
.getResultList();
assertEquals( 50, products.size() );
assertEquals( 1, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithGroupByThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Object[]> products =
session.createQuery(
"select count(p), p " +
"from Product p " +
"group by p.id, p.name " )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
.getResultList();
assertEquals( 50, products.size() );
assertEquals( 51, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithDistinctThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products =
session.createQuery(
"select distinct p from Product p",
Product.class
)
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
.getResultList();
assertEquals( 50, products.size() );
assertEquals( 51, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithMaxResultsThenNoFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products =
session.createQuery(
"select p from Product p", Product.class )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
.setMaxResults( 10 )
.getResultList();
assertEquals( 10, products.size() );
assertEquals( 1, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithMaxResultsAndOrderByThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products =
session.createQuery(
"select p from Product p order by p.id", Product.class )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
.setMaxResults( 10 )
.getResultList();
assertEquals( 10, products.size() );
assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithGroupByWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Object[]> products =
session.createQuery(
"select count(p), p " +
"from Product p " +
"group by p.id, p.name " )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setFollowOnLocking( true ) )
.getResultList();
assertEquals( 50, products.size() );
assertEquals( 51, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithFirstResultsThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products =
session.createQuery(
"select p from Product p", Product.class )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ) )
.setFirstResult( 40 )
.setMaxResults( 10 )
.getResultList();
assertEquals( 10, products.size() );
assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithMaxResultsAndOrderByWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products =
session.createQuery(
"select p from Product p order by p.id", Product.class )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setFollowOnLocking( true ) )
.setMaxResults( 10 )
.getResultList();
assertEquals( 10, products.size() );
assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithDistinctWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products =
session.createQuery(
"select distinct p from Product p where p.id > 40" )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setFollowOnLocking( true ) )
.setMaxResults( 10 )
.getResultList();
assertEquals( 10, products.size() );
assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@RequiresDialect({ Oracle8iDialect.class, PostgreSQL81Dialect.class,
SQLServer2005Dialect.class } )
public void testNoWait()
throws NoSuchFieldException, IllegalAccessException {
Session session = sessionFactory().openSession();
session.beginTransaction();
try {
session.createQuery(
"select a from A a", A.class )
.unwrap( org.hibernate.query.Query.class )
.setLockOptions(
new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setTimeOut( LockOptions.NO_WAIT ) )
.list();
String lockingQuery = sqlStatementInterceptor.getSqlQueries().getLast();
assertTrue( lockingQuery.toLowerCase().contains( "nowait") );
}
finally {
session.getTransaction().commit();
session.close();
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithFirstResultsWhileExplicitlyEnablingFollowOnLockingThenFollowOnLocking() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
List<Product> products =
session.createQuery(
"select p from Product p", Product.class )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setFollowOnLocking( true ) )
.setFirstResult( 40 )
.setMaxResults( 10 )
.getResultList();
assertEquals( 10, products.size() );
assertEquals( 11, sqlStatementInterceptor.getSqlQueries().size() );
session.getTransaction().commit();
session.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithUnionWhileExplicitlyDisablingFollowOnLockingThenFails() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
try {
List<Vehicle> vehicles = session.createQuery( "select v from Vehicle v" )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE ).setFollowOnLocking( false ) )
.getResultList();
fail( "Should throw exception since Oracle does not support UNION if follow on locking is disabled" );
}
catch ( PersistenceException expected ) {
assertEquals(
SQLGrammarException.class,
expected.getCause().getClass()
);
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithDistinctWhileExplicitlyDisablingFollowOnLockingThenFails() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
try {
List<Product> products =
session.createQuery(
"select distinct p from Product p where p.id > 40",
Product.class
)
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setFollowOnLocking( false ) )
.getResultList();
fail( "Should throw exception since Oracle does not support DISTINCT if follow on locking is disabled" );
}
catch ( PersistenceException expected ) {
assertEquals(
SQLGrammarException.class,
expected.getCause().getClass()
);
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithGroupByWhileExplicitlyDisablingFollowOnLockingThenFails() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
try {
List<Object[]> products =
session.createQuery(
"select count(p), p " +
"from Product p " +
"group by p.id, p.name " )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setFollowOnLocking( false ) )
.getResultList();
fail( "Should throw exception since Oracle does not support GROUP BY if follow on locking is disabled" );
}
catch ( PersistenceException expected ) {
assertEquals(
SQLGrammarException.class,
expected.getCause().getClass()
);
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithMaxResultsAndOrderByWhileExplicitlyDisablingFollowOnLockingThenFails() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
try {
List<Product> products =
session.createQuery(
"select p from Product p order by p.id",
Product.class
)
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setFollowOnLocking( false ) )
.setMaxResults( 10 )
.getResultList();
fail( "Should throw exception since Oracle does not support ORDER BY if follow on locking is disabled" );
}
catch ( PersistenceException expected ) {
assertEquals(
SQLGrammarException.class,
expected.getCause().getClass()
);
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testPessimisticLockWithFirstResultsWhileExplicitlyDisablingFollowOnLockingThenFails() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
try {
List<Product> products =
session.createQuery(
"select p from Product p", Product.class )
.setLockOptions( new LockOptions( LockMode.PESSIMISTIC_WRITE )
.setFollowOnLocking( false ) )
.setFirstResult( 40 )
.setMaxResults( 10 )
.getResultList();
fail( "Should throw exception since Oracle does not support ORDER BY if follow on locking is disabled" );
}
catch ( PersistenceException expected ) {
assertEquals(
SQLGrammarException.class,
expected.getCause().getClass()
);
}
}
内容来源于网络,如有侵权,请联系作者删除!