fix @DialectOverrides.SQLRestrictions

and add a test
This commit is contained in:
Gavin King 2024-11-28 21:48:24 +01:00
parent d60aa27e7c
commit 78cd996037
2 changed files with 68 additions and 1 deletions

View File

@ -252,7 +252,7 @@ public interface DialectOverride {
org.hibernate.annotations.SQLRestriction override();
}
@Target({METHOD, FIELD})
@Target({METHOD, FIELD, TYPE})
@Retention(RUNTIME)
@interface SQLRestrictions {
SQLRestriction[] value();

View File

@ -0,0 +1,67 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.customsql;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.annotations.DialectOverride;
import org.hibernate.annotations.SQLRestriction;
import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@SessionFactory
@DomainModel(annotatedClasses = CustomSqlRestrictionOverridesTest.Secure.class)
@RequiresDialect(H2Dialect.class)
@RequiresDialect(MySQLDialect.class)
@RequiresDialect(SQLServerDialect.class)
@RequiresDialect(PostgreSQLDialect.class)
@RequiresDialect(DB2Dialect.class)
@RequiresDialect(OracleDialect.class)
public class CustomSqlRestrictionOverridesTest {
@Test
public void testCustomSql(SessionFactoryScope scope) throws NoSuchAlgorithmException {
Secure sec = new Secure();
sec.hash = MessageDigest.getInstance( "SHA-256" ).digest("hello".getBytes());
scope.inTransaction(s -> s.persist(sec) );
Secure secure = scope.fromTransaction( s -> s.find( Secure.class, sec.id ) );
assertNotNull(secure);
}
@Entity
@Table(name = "SecureTable")
@DialectOverride.SQLRestriction(dialect = H2Dialect.class,
override = @SQLRestriction("hash = hash('SHA-256', 'hello')"))
@DialectOverride.SQLRestriction(dialect = MySQLDialect.class,
override = @SQLRestriction("hash = unhex(sha2('hello', 256))"))
@DialectOverride.SQLRestriction(dialect = PostgreSQLDialect.class,
override = @SQLRestriction("hash = sha256('hello')"))
@DialectOverride.SQLRestriction(dialect = SQLServerDialect.class,
override = @SQLRestriction("hash = hashbytes('SHA2_256', 'hello')"))
@DialectOverride.SQLRestriction(dialect = DB2Dialect.class,
override = @SQLRestriction("hash = hash('hello', 2)"))
@DialectOverride.SQLRestriction(dialect = OracleDialect.class,
override = @SQLRestriction("hash = standard_hash('hello', 'SHA256')"))
static class Secure {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
byte[] hash;
}
}