Re-enabled additional tests

This commit is contained in:
Andrea Boriero 2021-07-08 14:42:17 +02:00
parent 6b4f4755f3
commit 3f7044eebe
15 changed files with 445 additions and 389 deletions

View File

@ -17,7 +17,7 @@
-->
<hibernate-mapping package="org.hibernate.test.optlock">
<hibernate-mapping package="org.hibernate.orm.test.optlock">
<class name="Document" entity-name="LockDirty" table="Document" optimistic-lock="dirty" dynamic-update="true">
<id name="id">

View File

@ -6,7 +6,7 @@
*/
//$Id: Document.java 7552 2005-07-19 18:51:24Z oneovthafew $
package org.hibernate.test.optlock;
package org.hibernate.orm.test.optlock;
/**

View File

@ -0,0 +1,205 @@
/*
* 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.orm.test.optlock;
import javax.persistence.PersistenceException;
import org.hibernate.JDBCException;
import org.hibernate.Session;
import org.hibernate.StaleObjectStateException;
import org.hibernate.StaleStateException;
import org.hibernate.dialect.CockroachDialect;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Tests relating to the optimistic-lock mapping option.
*
* @author Gavin King
* @author Steve Ebersole
*/
@RequiresDialectFeature(
feature = DialectFeatureChecks.DoesRepeatableReadNotCauseReadersToBlockWritersCheck.class,
comment = "potential deadlock"
)
@DomainModel(
xmlMappings = "org/hibernate/orm/test/optlock/Document.hbm.xml"
)
@SessionFactory
public class OptimisticLockTest {
@AfterEach
public void tearDown(SessionFactoryScope scope){
scope.inTransaction(
session ->
{
session.createQuery( "delete from LockDirty" ).executeUpdate();
session.createQuery( "delete from LockAll" ).executeUpdate();
}
);
}
@Test
public void testOptimisticLockDirty(SessionFactoryScope scope) {
testUpdateOptimisticLockFailure( "LockDirty", scope );
}
@Test
public void testOptimisticLockAll(SessionFactoryScope scope) {
testUpdateOptimisticLockFailure( "LockAll", scope );
}
@Test
public void testOptimisticLockDirtyDelete(SessionFactoryScope scope) {
testDeleteOptimisticLockFailure( "LockDirty", scope );
}
@Test
public void testOptimisticLockAllDelete(SessionFactoryScope scope) {
testDeleteOptimisticLockFailure( "LockAll", scope );
}
private void testUpdateOptimisticLockFailure(String entityName, SessionFactoryScope scope) {
Document doc = scope.fromTransaction(
session -> {
Document document = new Document();
document.setTitle( "Hibernate in Action" );
document.setAuthor( "Bauer et al" );
document.setSummary( "Very boring book about persistence" );
document.setText( "blah blah yada yada yada" );
document.setPubDate( new PublicationDate( 2004 ) );
session.save( entityName, document );
return document;
}
);
scope.inTransaction(
mainSession -> {
Document document = (Document) mainSession.get( entityName, doc.getId() );
scope.inTransaction(
otherSession -> {
Document otherDoc = (Document) otherSession.get( entityName, document.getId() );
otherDoc.setSummary( "A modern classic" );
}
);
try {
document.setSummary( "A machiavellian achievement of epic proportions" );
mainSession.flush();
fail( "expecting opt lock failure" );
}
catch (PersistenceException e) {
// expected
checkException( mainSession, e, scope );
}
mainSession.clear();
}
);
scope.inTransaction(
session -> {
Document document = (Document) session.load( entityName, doc.getId() );
session.delete( entityName, document );
}
);
}
private void testDeleteOptimisticLockFailure(String entityName, SessionFactoryScope scope) {
Document doc = scope.fromTransaction(
session -> {
Document document = new Document();
document.setTitle( "Hibernate in Action" );
document.setAuthor( "Bauer et al" );
document.setSummary( "Very boring book about persistence" );
document.setText( "blah blah yada yada yada" );
document.setPubDate( new PublicationDate( 2004 ) );
session.save( entityName, document );
session.flush();
document.setSummary( "A modern classic" );
session.flush();
document.getPubDate().setMonth( Integer.valueOf( 3 ) );
session.flush();
return document;
}
);
scope.inTransaction(
mainSession -> {
Document document = (Document) mainSession.get( entityName, doc.getId() );
scope.inTransaction(
otherSession -> {
Document otherDoc = (Document) otherSession.get( entityName, document.getId() );
otherDoc.setSummary( "my other summary" );
otherSession.flush();
}
);
try {
mainSession.delete( document );
mainSession.flush();
fail( "expecting opt lock failure" );
}
catch (StaleObjectStateException e) {
// expected
}
catch (PersistenceException e) {
// expected
checkException( mainSession, e, scope );
}
mainSession.clear();
}
);
scope.inTransaction(
session -> {
Document document = (Document) session.load( entityName, doc.getId() );
session.delete( entityName, document );
}
);
}
private void checkException(Session mainSession, PersistenceException e, SessionFactoryScope scope) {
final Throwable cause = e.getCause();
if ( cause instanceof JDBCException ) {
Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect();
if ( dialect instanceof SQLServerDialect && ( (JDBCException) cause ).getErrorCode() == 3960 ) {
// SQLServer will report this condition via a SQLException
// when using its SNAPSHOT transaction isolation.
// it seems to "lose track" of the transaction as well...
mainSession.getTransaction().rollback();
mainSession.beginTransaction();
}
else if ( dialect instanceof CockroachDialect && ( (JDBCException) cause ).getSQLState().equals(
"40001" ) ) {
// CockroachDB always runs in SERIALIZABLE isolation, and uses SQL state 40001 to indicate
// serialization failure.
}
else {
throw e;
}
}
else if ( !( cause instanceof StaleObjectStateException ) && !( cause instanceof StaleStateException ) ) {
fail( "expected StaleObjectStateException or StaleStateException exception but is " + cause );
}
}
}

View File

@ -1,4 +1,4 @@
package org.hibernate.test.optlock;
package org.hibernate.orm.test.optlock;
import javax.persistence.Entity;
import javax.persistence.Id;
@ -6,28 +6,32 @@ import javax.persistence.LockModeType;
import javax.persistence.Version;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class OptimisticLockWithGloballyQuotedIdentifierTest extends BaseCoreFunctionalTestCase {
@Override
protected void configure(Configuration configuration) {
configuration.setProperty( AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, "true" );
}
@DomainModel(
annotatedClasses = {
OptimisticLockWithGloballyQuotedIdentifierTest.Person.class
}
)
@SessionFactory
@ServiceRegistry(
settings = @Setting( name = AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, value = "true")
)
public class OptimisticLockWithGloballyQuotedIdentifierTest {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Person.class };
}
@Before
public void setUp() {
inTransaction(
@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Person person = new Person( "1", "Fabiana" );
session.persist( person );
@ -35,9 +39,9 @@ public class OptimisticLockWithGloballyQuotedIdentifierTest extends BaseCoreFunc
);
}
@After
public void tearDown() {
inTransaction(
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Person" ).executeUpdate();
}
@ -45,8 +49,8 @@ public class OptimisticLockWithGloballyQuotedIdentifierTest extends BaseCoreFunc
}
@Test
public void testHqlQueryWithOptimisticLock() {
inTransaction(
public void testHqlQueryWithOptimisticLock(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "from Person e", Person.class )
.setLockMode( LockModeType.OPTIMISTIC )

View File

@ -1,4 +1,4 @@
package org.hibernate.test.optlock;
package org.hibernate.orm.test.optlock;
import javax.persistence.Column;
import javax.persistence.Entity;
@ -6,24 +6,23 @@ import javax.persistence.Id;
import javax.persistence.LockModeType;
import javax.persistence.Version;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class OptimisticLockWithQuotedVersionTest extends BaseCoreFunctionalTestCase {
@DomainModel(
annotatedClasses = OptimisticLockWithQuotedVersionTest.Person.class
)
@SessionFactory
public class OptimisticLockWithQuotedVersionTest {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Person.class };
}
@Before
public void setUp() {
inTransaction(
@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Person person = new Person( "1", "Fabiana" );
session.persist( person );
@ -31,9 +30,9 @@ public class OptimisticLockWithQuotedVersionTest extends BaseCoreFunctionalTestC
);
}
@After
public void tearDown() {
inTransaction(
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Person" ).executeUpdate();
}
@ -41,8 +40,8 @@ public class OptimisticLockWithQuotedVersionTest extends BaseCoreFunctionalTestC
}
@Test
public void testHqlQueryWithOptimisticLock() {
inTransaction(
public void testHqlQueryWithOptimisticLock(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "from Person e", Person.class )
.setLockMode( LockModeType.OPTIMISTIC )

View File

@ -6,7 +6,7 @@
*/
//$Id: PublicationDate.java 7556 2005-07-19 23:22:27Z oneovthafew $
package org.hibernate.test.optlock;
package org.hibernate.orm.test.optlock;
public class PublicationDate {

View File

@ -1,4 +1,4 @@
package org.hibernate.test.orderby;
package org.hibernate.orm.test.orderby;
import java.util.List;
import javax.persistence.DiscriminatorValue;
@ -6,30 +6,32 @@ import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Christian Beikov
*/
public class OrderByTest extends BaseCoreFunctionalTestCase {
@DomainModel(
annotatedClasses = {
OrderByTest.Person.class,
OrderByTest.P1.class,
OrderByTest.P2.class
}
)
@SessionFactory
public class OrderByTest {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
Person.class,
P1.class,
P2.class
};
}
@Override
protected void prepareTest() throws Exception {
doInHibernate(
this::sessionFactory, session -> {
@BeforeEach
protected void prepareTest(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.persist( new P1( 1L, "abc" ) );
session.persist( new P1( 2L, "abc" ) );
session.persist( new P2( 3L, "def" ) );
@ -37,20 +39,19 @@ public class OrderByTest extends BaseCoreFunctionalTestCase {
);
}
@Override
protected void cleanupTest() throws Exception {
doInHibernate(
this::sessionFactory, session -> {
session.createQuery( "delete from Person" ).executeUpdate();
}
@AfterEach
protected void cleanupTest(SessionFactoryScope scope) {
scope.inTransaction(
session ->
session.createQuery( "delete from Person" ).executeUpdate()
);
}
@Test
@TestForIssue( jiraKey = "HHH-14351")
public void testOrderBySqlNode() {
doInHibernate(
this::sessionFactory, session -> {
@TestForIssue(jiraKey = "HHH-14351")
public void testOrderBySqlNode(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
List<Person> list = session.createQuery( "from Person p order by type(p) desc, p.id", Person.class )
.getResultList();
assertEquals( 3L, list.get( 0 ).getId().longValue() );
@ -92,7 +93,7 @@ public class OrderByTest extends BaseCoreFunctionalTestCase {
}
@Entity(name = "P1")
@DiscriminatorValue( "P1" )
@DiscriminatorValue("P1")
public static class P1 extends Person {
public P1() {
}
@ -103,7 +104,7 @@ public class OrderByTest extends BaseCoreFunctionalTestCase {
}
@Entity(name = "P2")
@DiscriminatorValue( "P2" )
@DiscriminatorValue("P2")
public static class P2 extends Person {
public P2() {
}

View File

@ -4,40 +4,45 @@
* 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.test.ordered;
package org.hibernate.orm.test.ordered;
import java.util.Arrays;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.dialect.MySQL5Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Vlad Mihalcea
*/
public class HqlOrderByIdsTest extends BaseEntityManagerFunctionalTestCase {
@Jpa(
annotatedClasses = HqlOrderByIdsTest.Person.class
)
public class HqlOrderByIdsTest {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Person.class
};
@AfterEach
public void tearDown(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager ->
entityManager.createQuery( "delete from Person" ).executeUpdate()
);
}
@Test
@TestForIssue( jiraKey = "HHH-10502" )
@RequiresDialect( value = MySQL5Dialect.class )
public void testLifecycle() {
doInJPA( this::entityManagerFactory, entityManager -> {
@TestForIssue(jiraKey = "HHH-10502")
@RequiresDialect(value = MySQLDialect.class, version = 500)
public void testLifecycle(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
Person person1 = new Person();
person1.setId( 1L );
person1.setName( "John" );
@ -59,20 +64,21 @@ public class HqlOrderByIdsTest extends BaseEntityManagerFunctionalTestCase {
entityManager.persist( person3 );
entityManager.persist( person4 );
} );
doInJPA( this::entityManagerFactory, entityManager -> {
List<Person> persons = entityManager.createQuery(
"SELECT p " +
"FROM Person p " +
"WHERE p.id IN (:ids) " +
"ORDER BY FIELD(id, :ids) ", Person.class)
.setParameter( "ids" , Arrays.asList(3L, 1L, 2L))
.getResultList();
assertEquals(3, persons.size());
scope.inTransaction( entityManager -> {
List<Person> persons = entityManager.createQuery(
"SELECT p " +
"FROM Person p " +
"WHERE p.id IN (:ids) " +
"ORDER BY FIELD(id, :ids) ", Person.class )
.setParameter( "ids", Arrays.asList( 3L, 1L, 2L ) )
.getResultList();
assertEquals( 3, persons.size() );
int index = 0;
assertEquals( Long.valueOf( 3L ), persons.get( index++ ).getId() );
assertEquals( Long.valueOf( 1L ), persons.get( index++ ).getId() );
assertEquals( Long.valueOf( 2L ), persons.get( index++ ).getId() );
assertEquals( 3L, persons.get( index++ ).getId() );
assertEquals( 1L, persons.get( index++ ).getId() );
assertEquals( 2L, persons.get( index++ ).getId() );
} );
}

View File

@ -4,7 +4,7 @@
* 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.test.ordered;
package org.hibernate.orm.test.ordered;
import java.util.Iterator;
import javax.persistence.criteria.CriteriaBuilder;
@ -13,26 +13,40 @@ import javax.persistence.criteria.JoinType;
import org.hibernate.Hibernate;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Gavin King
*/
public class OrderByTest extends BaseCoreFunctionalTestCase {
public String[] getMappings() {
return new String[] { "ordered/Search.hbm.xml" };
@DomainModel(
xmlMappings = "org/hibernate/orm/test/ordered/Search.hbm.xml"
)
@SessionFactory
public class OrderByTest {
@AfterEach
public void tearDonw(SessionFactoryScope scope){
scope.inTransaction(
session ->
session.createQuery( "delete from Search" ).executeUpdate()
);
}
@Test
@SuppressWarnings({ "unchecked" })
public void testOrderBy() {
public void testOrderBy(SessionFactoryScope scope) {
inTransaction(
scope.inTransaction(
sess -> {
Search s = new Search( "Hibernate" );
s.getSearchResults().add( "jboss.com" );
@ -49,9 +63,9 @@ public class OrderByTest extends BaseCoreFunctionalTestCase {
// s = (Search) sess.createCriteria( Search.class ).uniqueResult();
assertFalse( Hibernate.isInitialized( s.getSearchResults() ) );
Iterator iter = s.getSearchResults().iterator();
assertEquals( iter.next(), "HiA" );
assertEquals( iter.next(), "hibernate.org" );
assertEquals( iter.next(), "jboss.com" );
assertThat( iter.next(), is( "HiA" ) );
assertThat( iter.next(), is( "hibernate.org" ) );
assertThat( iter.next(), is( "jboss.com" ) );
assertFalse( iter.hasNext() );
sess.clear();
@ -63,9 +77,9 @@ public class OrderByTest extends BaseCoreFunctionalTestCase {
// .uniqueResult();
assertTrue( Hibernate.isInitialized( s.getSearchResults() ) );
iter = s.getSearchResults().iterator();
assertEquals( iter.next(), "HiA" );
assertEquals( iter.next(), "hibernate.org" );
assertEquals( iter.next(), "jboss.com" );
assertThat( iter.next(), is( "HiA" ) );
assertThat( iter.next(), is( "hibernate.org" ) );
assertThat( iter.next(), is( "jboss.com" ) );
assertFalse( iter.hasNext() );
sess.clear();
@ -73,9 +87,9 @@ public class OrderByTest extends BaseCoreFunctionalTestCase {
.uniqueResult();
assertTrue( Hibernate.isInitialized( s.getSearchResults() ) );
iter = s.getSearchResults().iterator();
assertEquals( iter.next(), "HiA" );
assertEquals( iter.next(), "hibernate.org" );
assertEquals( iter.next(), "jboss.com" );
assertThat( iter.next(), is( "HiA" ) );
assertThat( iter.next(), is( "hibernate.org" ) );
assertThat( iter.next(), is( "jboss.com" ) );
assertFalse( iter.hasNext() );
/*sess.clear();

View File

@ -13,7 +13,7 @@
-->
<hibernate-mapping package="org.hibernate.test.ordered">
<hibernate-mapping package="org.hibernate.orm.test.ordered">
<class name="Search">
<id name="searchString"/>

View File

@ -6,7 +6,7 @@
*/
//$Id: Search.java 7772 2005-08-05 23:03:46Z oneovthafew $
package org.hibernate.test.ordered;
package org.hibernate.orm.test.ordered;
import java.util.HashSet;
import java.util.Set;

View File

@ -0,0 +1,82 @@
/*
* 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.orm.test.ordered;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
/**
* @author Andrea Boriero
*/
@DomainModel(
annotatedClasses = {
UseReservedKeywordInOrderByTest.Person.class,
UseReservedKeywordInOrderByTest.Location.class
}
)
@SessionFactory
public class UseReservedKeywordInOrderByTest {
@Test
public void testOrderBy(SessionFactoryScope scope) {
scope.inSession(
session ->
session.createQuery( "from Person p order by p.update" )
);
}
@Test
public void testMultipleOrderBy(SessionFactoryScope scope) {
scope.inSession(
session ->
session.createQuery( "from Person p order by p.name,p.update" )
);
}
@Test
public void testOrderByOfAssociationEntityField(SessionFactoryScope scope) {
scope.inSession(
session ->
session.createQuery( "from Person p order by p.location.update" )
);
}
@Entity(name = "Person")
public static class Person {
@Id
private Integer id;
private String name;
@Column(name = "update_date")
private Date update;
@OneToOne
Location location;
}
@Entity(name = "Location")
public static class Location {
@Id
private Integer id;
private String city;
@Column(name = "update_date")
private Date update;
}
}

View File

@ -1,183 +0,0 @@
/*
* 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.test.optlock;
import javax.persistence.PersistenceException;
import org.hibernate.JDBCException;
import org.hibernate.Session;
import org.hibernate.StaleObjectStateException;
import org.hibernate.StaleStateException;
import org.hibernate.dialect.CockroachDialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.fail;
/**
* Tests relating to the optimistic-lock mapping option.
*
* @author Gavin King
* @author Steve Ebersole
*/
@RequiresDialectFeature(
value = DialectChecks.DoesRepeatableReadNotCauseReadersToBlockWritersCheck.class,
comment = "potential deadlock"
)
public class OptimisticLockTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "optlock/Document.hbm.xml" };
}
@Test
public void testOptimisticLockDirty() {
testUpdateOptimisticLockFailure( "LockDirty" );
}
@Test
public void testOptimisticLockAll() {
testUpdateOptimisticLockFailure( "LockAll" );
}
@Test
public void testOptimisticLockDirtyDelete() {
testDeleteOptimisticLockFailure( "LockDirty" );
}
@Test
public void testOptimisticLockAllDelete() {
testDeleteOptimisticLockFailure( "LockAll" );
}
private void testUpdateOptimisticLockFailure(String entityName) {
Session mainSession = openSession();
mainSession.beginTransaction();
Document doc = new Document();
doc.setTitle( "Hibernate in Action" );
doc.setAuthor( "Bauer et al" );
doc.setSummary( "Very boring book about persistence" );
doc.setText( "blah blah yada yada yada" );
doc.setPubDate( new PublicationDate( 2004 ) );
mainSession.save( entityName, doc );
mainSession.getTransaction().commit();
mainSession.close();
mainSession = openSession();
mainSession.beginTransaction();
doc = ( Document ) mainSession.get( entityName, doc.getId() );
Session otherSession = sessionFactory().openSession();
otherSession.beginTransaction();
Document otherDoc = ( Document ) otherSession.get( entityName, doc.getId() );
otherDoc.setSummary( "A modern classic" );
otherSession.getTransaction().commit();
otherSession.close();
try {
doc.setSummary( "A machiavellian achievement of epic proportions" );
mainSession.flush();
fail( "expecting opt lock failure" );
}
catch (PersistenceException e){
// expected
checkException( mainSession, e );
}
mainSession.clear();
mainSession.getTransaction().rollback();
mainSession.close();
mainSession = openSession();
mainSession.beginTransaction();
doc = ( Document ) mainSession.load( entityName, doc.getId() );
mainSession.delete( entityName, doc );
mainSession.getTransaction().commit();
mainSession.close();
}
private void testDeleteOptimisticLockFailure(String entityName) {
Session mainSession = openSession();
mainSession.beginTransaction();
Document doc = new Document();
doc.setTitle( "Hibernate in Action" );
doc.setAuthor( "Bauer et al" );
doc.setSummary( "Very boring book about persistence" );
doc.setText( "blah blah yada yada yada" );
doc.setPubDate( new PublicationDate( 2004 ) );
mainSession.save( entityName, doc );
mainSession.flush();
doc.setSummary( "A modern classic" );
mainSession.flush();
doc.getPubDate().setMonth( Integer.valueOf( 3 ) );
mainSession.flush();
mainSession.getTransaction().commit();
mainSession.close();
mainSession = openSession();
mainSession.beginTransaction();
doc = ( Document ) mainSession.get( entityName, doc.getId() );
Session otherSession = openSession();
otherSession.beginTransaction();
Document otherDoc = ( Document ) otherSession.get( entityName, doc.getId() );
otherDoc.setSummary( "my other summary" );
otherSession.flush();
otherSession.getTransaction().commit();
otherSession.close();
try {
mainSession.delete( doc );
mainSession.flush();
fail( "expecting opt lock failure" );
}
catch ( StaleObjectStateException e ) {
// expected
}
catch (PersistenceException e){
// expected
checkException( mainSession, e );
}
mainSession.clear();
mainSession.getTransaction().rollback();
mainSession.close();
mainSession = openSession();
mainSession.beginTransaction();
doc = ( Document ) mainSession.load( entityName, doc.getId() );
mainSession.delete( entityName, doc );
mainSession.getTransaction().commit();
mainSession.close();
}
private void checkException(Session mainSession, PersistenceException e) {
final Throwable cause = e.getCause();
if ( cause instanceof JDBCException ) {
if ( getDialect() instanceof SQLServerDialect && ((JDBCException) cause).getErrorCode() == 3960 ) {
// SQLServer will report this condition via a SQLException
// when using its SNAPSHOT transaction isolation.
// it seems to "lose track" of the transaction as well...
mainSession.getTransaction().rollback();
mainSession.beginTransaction();
}
else if ( getDialect() instanceof CockroachDialect && ((JDBCException) cause).getSQLState().equals( "40001")) {
// CockroachDB always runs in SERIALIZABLE isolation, and uses SQL state 40001 to indicate
// serialization failure.
}
else {
throw e;
}
}
else if ( !(cause instanceof StaleObjectStateException) && !(cause instanceof StaleStateException) ) {
fail( "expected StaleObjectStateException or StaleStateException exception but is " + cause );
}
}
}

View File

@ -1,77 +0,0 @@
/*
* 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.test.ordered;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Version;
import org.hibernate.Session;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Andrea Boriero
*/
public class UseReservedKeywordInOrderByTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{Person.class, Location.class};
}
@Test
public void testOrderBy(){
try(Session s = openSession();){
s.createQuery( "from Person p order by p.update" );
}
}
@Test
public void testMultipleOrderBy(){
try(Session s = openSession();){
s.createQuery( "from Person p order by p.name,p.update" );
}
}
@Test
public void testOrderByOfAssociationEntityField(){
try(Session s = openSession();){
s.createQuery( "from Person p order by p.location.update" );
}
}
@Entity(name = "Person")
public static class Person {
@Id
private Integer id;
private String name;
@Column(name = "update_date")
private Date update;
@OneToOne
Location location;
}
@Entity(name = "Location")
public static class Location{
@Id
private Integer id;
private String city;
@Column(name = "update_date")
private Date update;
}
}

View File

@ -367,4 +367,9 @@ abstract public class DialectFeatureChecks {
}
}
public static class DoesRepeatableReadNotCauseReadersToBlockWritersCheck implements DialectFeatureCheck {
public boolean apply(Dialect dialect) {
return ! dialect.doesRepeatableReadCauseReadersToBlockWriters();
}
}
}