|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.customsql; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.GeneratedValue; |
| 9 | +import jakarta.persistence.GenerationType; |
| 10 | +import jakarta.persistence.Id; |
| 11 | +import jakarta.persistence.Table; |
| 12 | +import org.hibernate.annotations.DialectOverride; |
| 13 | +import org.hibernate.annotations.SQLRestriction; |
| 14 | +import org.hibernate.dialect.DB2Dialect; |
| 15 | +import org.hibernate.dialect.H2Dialect; |
| 16 | +import org.hibernate.dialect.MySQLDialect; |
| 17 | +import org.hibernate.dialect.OracleDialect; |
| 18 | +import org.hibernate.dialect.PostgreSQLDialect; |
| 19 | +import org.hibernate.dialect.SQLServerDialect; |
| 20 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 21 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 22 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 23 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import java.security.MessageDigest; |
| 27 | +import java.security.NoSuchAlgorithmException; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 30 | + |
| 31 | +@SessionFactory |
| 32 | +@DomainModel(annotatedClasses = CustomSqlRestrictionOverridesTest.Secure.class) |
| 33 | +@RequiresDialect(H2Dialect.class) |
| 34 | +@RequiresDialect(MySQLDialect.class) |
| 35 | +@RequiresDialect(SQLServerDialect.class) |
| 36 | +@RequiresDialect(PostgreSQLDialect.class) |
| 37 | +@RequiresDialect(DB2Dialect.class) |
| 38 | +@RequiresDialect(OracleDialect.class) |
| 39 | +public class CustomSqlRestrictionOverridesTest { |
| 40 | + @Test |
| 41 | + public void testCustomSql(SessionFactoryScope scope) throws NoSuchAlgorithmException { |
| 42 | + Secure sec = new Secure(); |
| 43 | + sec.hash = MessageDigest.getInstance( "SHA-256" ).digest("hello".getBytes()); |
| 44 | + scope.inTransaction(s -> s.persist(sec) ); |
| 45 | + Secure secure = scope.fromTransaction( s -> s.find( Secure.class, sec.id ) ); |
| 46 | + assertNotNull(secure); |
| 47 | + } |
| 48 | + @Entity |
| 49 | + @Table(name = "SecureTable") |
| 50 | + @DialectOverride.SQLRestriction(dialect = H2Dialect.class, |
| 51 | + override = @SQLRestriction("hash = hash('SHA-256', 'hello')")) |
| 52 | + @DialectOverride.SQLRestriction(dialect = MySQLDialect.class, |
| 53 | + override = @SQLRestriction("hash = unhex(sha2('hello', 256))")) |
| 54 | + @DialectOverride.SQLRestriction(dialect = PostgreSQLDialect.class, |
| 55 | + override = @SQLRestriction("hash = sha256('hello')")) |
| 56 | + @DialectOverride.SQLRestriction(dialect = SQLServerDialect.class, |
| 57 | + override = @SQLRestriction("hash = hashbytes('SHA2_256', 'hello')")) |
| 58 | + @DialectOverride.SQLRestriction(dialect = DB2Dialect.class, |
| 59 | + override = @SQLRestriction("hash = hash('hello', 2)")) |
| 60 | + @DialectOverride.SQLRestriction(dialect = OracleDialect.class, |
| 61 | + override = @SQLRestriction("hash = standard_hash('hello', 'SHA256')")) |
| 62 | + static class Secure { |
| 63 | + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 64 | + Long id; |
| 65 | + byte[] hash; |
| 66 | + } |
| 67 | +} |
0 commit comments