File tree Expand file tree Collapse file tree 3 files changed +33
-5
lines changed
main/java/tech/ydb/hibernate/dialect
test/java/tech/ydb/hibernate/student Expand file tree Collapse file tree 3 files changed +33
-5
lines changed Original file line number Diff line number Diff line change 12
12
import org .hibernate .mapping .Constraint ;
13
13
import org .hibernate .mapping .ForeignKey ;
14
14
import org .hibernate .mapping .Index ;
15
+ import org .hibernate .query .spi .QueryOptions ;
15
16
import org .hibernate .service .ServiceRegistry ;
16
17
import org .hibernate .sql .ast .SqlAstTranslatorFactory ;
17
18
import org .hibernate .sql .ast .spi .SqlAppender ;
@@ -121,8 +122,20 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
121
122
}
122
123
123
124
@ Override
124
- public String getQueryHintString (String query , List <String > hintList ) {
125
- return IndexQueryHintHandler .INSTANCE .addQueryHints (query , hintList );
125
+ public String addSqlHintOrComment (String sql , QueryOptions queryOptions , boolean commentsEnabled ) {
126
+ if (queryOptions .getDatabaseHints () != null ) {
127
+ sql = IndexQueryHintHandler .addQueryHints (sql , queryOptions .getDatabaseHints ());
128
+ }
129
+
130
+ if (queryOptions .getComment () != null && IndexQueryHintHandler .commentIsHint (queryOptions .getComment ())) {
131
+ return IndexQueryHintHandler .addQueryHints (sql , List .of (queryOptions .getComment ()));
132
+ }
133
+
134
+ if (commentsEnabled && queryOptions .getComment () != null ) {
135
+ sql = prependComment (sql , queryOptions .getComment ());
136
+ }
137
+
138
+ return sql ;
126
139
}
127
140
128
141
@ Override
Original file line number Diff line number Diff line change @@ -13,9 +13,12 @@ public class IndexQueryHintHandler {
13
13
.compile ("^\\ s*(select.+?from\\ s+\\ w+)(.+where.+)$" , Pattern .CASE_INSENSITIVE );
14
14
15
15
public static final String HINT_USE_INDEX = "use_index:" ;
16
- public static final IndexQueryHintHandler INSTANCE = new IndexQueryHintHandler ();
17
16
18
- public String addQueryHints (String query , List <String > hints ) {
17
+ public static boolean commentIsHint (String comment ) {
18
+ return comment .startsWith (HINT_USE_INDEX );
19
+ }
20
+
21
+ public static String addQueryHints (String query , List <String > hints ) {
19
22
if (hints .isEmpty ()) {
20
23
return query ;
21
24
}
Original file line number Diff line number Diff line change 3
3
import java .util .List ;
4
4
import org .hibernate .cfg .AvailableSettings ;
5
5
import org .hibernate .cfg .Configuration ;
6
+ import org .hibernate .jpa .HibernateHints ;
6
7
import org .hibernate .query .Query ;
7
8
import static org .junit .jupiter .api .Assertions .assertEquals ;
8
9
import org .junit .jupiter .api .BeforeAll ;
@@ -165,7 +166,18 @@ void groupByGroupName_ViewIndex() {
165
166
session -> {
166
167
Group group = session
167
168
.createQuery ("FROM Group g WHERE g.name = 'M3439'" , Group .class )
168
- .addQueryHint ("use_index:group_name_index" )
169
+ .addQueryHint ("use_index:group_name_index" ) // Hibernate
170
+ .getSingleResult ();
171
+
172
+ assertEquals ("M3439" , group .getName ());
173
+ }
174
+ );
175
+
176
+ inTransaction (
177
+ session -> {
178
+ Group group = session
179
+ .createQuery ("FROM Group g WHERE g.name = 'M3439'" , Group .class )
180
+ .setHint (HibernateHints .HINT_COMMENT , "use_index:group_name_index" ) // JPA
169
181
.getSingleResult ();
170
182
171
183
assertEquals ("M3439" , group .getName ());
You can’t perform that action at this time.
0 commit comments