HHH-18176 upsert() with @Version-ed entity on Oracle

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-05-28 20:01:56 +02:00
parent e21a590e7d
commit 8c78c44bda
3 changed files with 21 additions and 10 deletions

View File

@ -191,6 +191,7 @@ public class SqlAstTranslatorWithUpsert<T extends JdbcOperation> extends Abstrac
protected void renderMergeUpdate(OptionalTableUpdate optionalTableUpdate) {
final List<ColumnValueBinding> valueBindings = optionalTableUpdate.getValueBindings();
final List<ColumnValueBinding> optimisticLockBindings = optionalTableUpdate.getOptimisticLockBindings();
appendSql( " when matched then update set " );
for ( int i = 0; i < valueBindings.size(); i++ ) {
@ -202,5 +203,21 @@ public class SqlAstTranslatorWithUpsert<T extends JdbcOperation> extends Abstrac
appendSql( "=" );
binding.getColumnReference().appendColumnForWrite( this, "s" );
}
renderMatchedWhere( optimisticLockBindings );
}
private void renderMatchedWhere(List<ColumnValueBinding> optimisticLockBindings) {
if ( !optimisticLockBindings.isEmpty() ) {
appendSql( " where " );
for (int i = 0; i < optimisticLockBindings.size(); i++) {
final ColumnValueBinding binding = optimisticLockBindings.get( i );
if ( i>0 ) {
appendSql(" and ");
}
binding.getColumnReference().appendColumnForWrite( this, "t" );
appendSql("<=");
binding.getValueExpression().accept( this );
}
}
}
}

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.dialect.identity;
import org.hibernate.dialect.Dialect;
import org.hibernate.generator.EventType;
import org.hibernate.id.insert.GetGeneratedKeysDelegate;
import org.hibernate.persister.entity.EntityPersister;
@ -25,7 +24,7 @@ public class Oracle12cIdentityColumnSupport extends IdentityColumnSupportImpl {
@Override
public String getIdentityColumnString(int type) {
return "generated as identity";
return "generated by default as identity";
}
@Override

View File

@ -6,7 +6,6 @@ import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Version;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
@ -30,9 +29,8 @@ public class StatelessSessionVersioningTest {
assertEquals(0, v.version);
s.update(v);
assertEquals(1, v.version);
if ( !(dialect instanceof SQLServerDialect) && !(dialect instanceof OracleDialect) ) {
if ( !(dialect instanceof SQLServerDialect) ) {
//TODO: upsert() with IDENTITY not working on SQL Server
//TODO: upsert() with version not working on Oracle
s.upsert(v);
assertEquals(2, v.version);
}
@ -47,11 +45,8 @@ public class StatelessSessionVersioningTest {
assertEquals(0, v.version);
s.update(v);
assertEquals(1, v.version);
if ( !(dialect instanceof OracleDialect) ) {
//TODO: upsert() with version not working on Oracle
s.upsert(v);
assertEquals(2, v.version);
}
s.upsert(v);
assertEquals(2, v.version);
s.delete(v);
});
}