HHH-11376 - Add test for issue

This commit is contained in:
Andrea Boriero 2017-01-10 18:18:23 +00:00
parent ed4e3f50e7
commit 5f0ae6fcb7
2 changed files with 95 additions and 12 deletions

View File

@ -0,0 +1,50 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.test.lock;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import static javax.persistence.FetchType.LAZY;
/**
* @author Andrea Boriero
*/
@Entity
@Table(name = "Person")
public class Person {
private Long id;
private Person parent;
public Person() {
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "parentId", insertable = false, updatable = false)
public Person getParent() {
return parent;
}
public void setParent(Person parent) {
this.parent = parent;
}
}

View File

@ -6,24 +6,22 @@
*/
package org.hibernate.jpa.test.lock;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.List;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;
import org.hibernate.LockMode;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.internal.SessionImpl;
import org.hibernate.jpa.AvailableSettings;
import org.hibernate.jpa.QueryHints;
@ -31,17 +29,26 @@ import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.query.NativeQuery;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.transaction.TransactionUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Steve Ebersole
*/
public class QueryLockingTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Lockable.class, LocalEntity.class };
return new Class[] {Person.class, Lockable.class, LocalEntity.class};
}
@Override
@ -87,7 +94,7 @@ public class QueryLockingTest extends BaseEntityManagerFunctionalTestCase {
em.getTransaction().commit();
em.clear();
// ensure other modes still throw the exception
em.getTransaction().begin();
query = em.createQuery( "delete from Lockable l" ).unwrap( org.hibernate.query.Query.class );
@ -364,8 +371,34 @@ public class QueryLockingTest extends BaseEntityManagerFunctionalTestCase {
em.close();
}
@Entity( name = "LocalEntity" )
@Table( name = "LocalEntity" )
@Test
@TestForIssue(jiraKey = "HHH-11376")
@RequiresDialect( SQLServerDialect.class )
public void testCriteriaWithPessimisticLock() {
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Person> criteria = builder.createQuery( Person.class );
Root<Person> personRoot = criteria.from( Person.class );
ParameterExpression<Long> personIdParameter = builder.parameter( Long.class );
// Eagerly fetch the parent
personRoot.fetch( "parent", JoinType.LEFT );
criteria.select( personRoot )
.where( builder.equal( personRoot.get( "id" ), personIdParameter ) );
final List<Person> resultList = entityManager.createQuery( criteria )
.setParameter( personIdParameter, 1L )
.setLockMode( LockModeType.PESSIMISTIC_WRITE )
.getResultList();
resultList.isEmpty();
} );
}
@Entity(name = "LocalEntity")
@Table(name = "LocalEntity")
public static class LocalEntity {
private Integer id;
private String name;