|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.test.locking; |
| 8 | + |
| 9 | +import javax.persistence.Entity; |
| 10 | +import javax.persistence.GeneratedValue; |
| 11 | +import javax.persistence.Id; |
| 12 | +import javax.persistence.LockModeType; |
| 13 | +import javax.persistence.Version; |
| 14 | + |
| 15 | +import org.hibernate.dialect.HANAColumnStoreDialect; |
| 16 | +import org.hibernate.dialect.HANARowStoreDialect; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +import org.hibernate.testing.RequiresDialect; |
| 20 | +import org.hibernate.testing.RequiresDialects; |
| 21 | +import org.hibernate.testing.TestForIssue; |
| 22 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 23 | + |
| 24 | +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; |
| 25 | +import static org.junit.Assert.assertNotNull; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Chris Cranford |
| 29 | + */ |
| 30 | +@TestForIssue(jiraKey = "HHH-11656") |
| 31 | +@RequiresDialects( { @RequiresDialect(HANAColumnStoreDialect.class), @RequiresDialect(HANARowStoreDialect.class) }) |
| 32 | +public class HANAOptimisticLockingTest extends BaseCoreFunctionalTestCase { |
| 33 | +@Override |
| 34 | +protected Class<?>[] getAnnotatedClasses() { |
| 35 | +return new Class<?>[] { SomeEntity.class }; |
| 36 | +} |
| 37 | + |
| 38 | +@Test |
| 39 | +public void testOptimisticLock() throws Exception { |
| 40 | +testWithSpecifiedLockMode( LockModeType.OPTIMISTIC ); |
| 41 | +} |
| 42 | + |
| 43 | +@Test |
| 44 | +public void testOptimisticLockForceIncrement() throws Exception { |
| 45 | +testWithSpecifiedLockMode( LockModeType.OPTIMISTIC_FORCE_INCREMENT ); |
| 46 | +} |
| 47 | + |
| 48 | +private void testWithSpecifiedLockMode(LockModeType lockModeType) { |
| 49 | +// makes sure we have an entity to actually query |
| 50 | +final Object id = doInHibernate( this::sessionFactory, session -> { |
| 51 | +return session.save( new SomeEntity() ); |
| 52 | +} ); |
| 53 | + |
| 54 | +// tests that both the query execution doesn't throw a SQL syntax (which is the main bug) and that |
| 55 | +// the query returns an expected entity object. |
| 56 | +doInHibernate( this::sessionFactory, session -> { |
| 57 | +/** |
| 58 | + * This generates the wrong SQL query for HANA. |
| 59 | + * Using optimistic lock and string query cause a bug. |
| 60 | + * |
| 61 | + * Generated SQL query for HANA is as follows: |
| 62 | + * |
| 63 | + * SELECT |
| 64 | + * someentity0_.id as id1_0_, |
| 65 | + * someentity0_.version as version2_0_ |
| 66 | + * FROM SomeEntity someentity0_ |
| 67 | + * WHERE someentity0_ = 1 of someentity0_.id |
| 68 | + * |
| 69 | + * The exception thrown by HANA is: |
| 70 | + * com.sap.db.jdbc.exceptions.JDBCDriverException: SAP DBTech JDBC: [257]: |
| 71 | + * sql syntax error: incorrect syntax near "of": line 1 |
| 72 | + * |
| 73 | + */ |
| 74 | +SomeEntity entity = session |
| 75 | +.createQuery( "SELECT e FROM SomeEntity e WHERE e.id = :id", SomeEntity.class ) |
| 76 | +.setParameter( "id", id ) |
| 77 | +.setLockMode( lockModeType ) |
| 78 | +.uniqueResult(); |
| 79 | + |
| 80 | +assertNotNull( entity ); |
| 81 | +} ); |
| 82 | +} |
| 83 | + |
| 84 | +@Entity(name = "SomeEntity") |
| 85 | +public static class SomeEntity { |
| 86 | +@Id |
| 87 | +@GeneratedValue |
| 88 | +private Integer id; |
| 89 | +@Version |
| 90 | +private Integer version; |
| 91 | + |
| 92 | +public Integer getId() { |
| 93 | +return id; |
| 94 | +} |
| 95 | + |
| 96 | +public void setId(Integer id) { |
| 97 | +this.id = id; |
| 98 | +} |
| 99 | + |
| 100 | +public Integer getVersion() { |
| 101 | +return version; |
| 102 | +} |
| 103 | + |
| 104 | +public void setVersion(Integer version) { |
| 105 | +this.version = version; |
| 106 | +} |
| 107 | +} |
| 108 | +} |
0 commit comments