Skip to content

Commit 57c3803

Browse files
committed
fix @DialectOverrides.SQLRestrictions
and add a test
1 parent d60aa27 commit 57c3803

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

hibernate-core/src/main/java/org/hibernate/annotations/DialectOverride.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public interface DialectOverride {
252252

253253
org.hibernate.annotations.SQLRestriction override();
254254
}
255-
@Target({METHOD, FIELD})
255+
@Target({METHOD, FIELD, TYPE})
256256
@Retention(RUNTIME)
257257
@interface SQLRestrictions {
258258
SQLRestriction[] value();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.H2Dialect;
15+
import org.hibernate.dialect.MySQLDialect;
16+
import org.hibernate.dialect.PostgreSQLDialect;
17+
import org.hibernate.testing.orm.junit.DomainModel;
18+
import org.hibernate.testing.orm.junit.RequiresDialect;
19+
import org.hibernate.testing.orm.junit.SessionFactory;
20+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.security.MessageDigest;
24+
import java.security.NoSuchAlgorithmException;
25+
26+
import static org.junit.jupiter.api.Assertions.assertNotNull;
27+
28+
@SessionFactory
29+
@DomainModel(annotatedClasses = CustomSqlRestrictionOverridesTest.Secure.class)
30+
@RequiresDialect(H2Dialect.class)
31+
@RequiresDialect(MySQLDialect.class)
32+
@RequiresDialect(value = PostgreSQLDialect.class, majorVersion = 13)
33+
public class CustomSqlRestrictionOverridesTest {
34+
@Test
35+
public void testCustomSql(SessionFactoryScope scope) throws NoSuchAlgorithmException {
36+
Secure sec = new Secure();
37+
sec.hash = MessageDigest.getInstance( "SHA-256" ).digest("hello".getBytes());
38+
scope.inTransaction(s -> s.persist(sec) );
39+
Secure secure = scope.fromTransaction( s -> s.find( Secure.class, sec.id ) );
40+
assertNotNull(secure);
41+
}
42+
@Entity
43+
@Table(name = "SecureTable")
44+
@DialectOverride.SQLRestriction(dialect = H2Dialect.class,
45+
override = @SQLRestriction("hash = hash('SHA-256', 'hello')"))
46+
@DialectOverride.SQLRestriction(dialect = MySQLDialect.class,
47+
override = @SQLRestriction("hash = unhex(sha2('hello', 256))"))
48+
@DialectOverride.SQLRestriction(dialect = PostgreSQLDialect.class,
49+
override = @SQLRestriction("hash = sha256('hello')"))
50+
static class Secure {
51+
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
52+
Long id;
53+
byte[] hash;
54+
}
55+
}

0 commit comments

Comments
 (0)