HHH-17719 Supports boolean as return type of mutation query method

This commit is contained in:
Yanming Zhou 2024-02-07 17:19:27 +08:00 committed by Gavin King
parent da0e327d09
commit c804ff3ac5
5 changed files with 21 additions and 5 deletions

View File

@ -662,7 +662,7 @@ List<Book> books =
// })
// ----
An `insert`, `update`, or `delete` query must return `int` or `void`.
An `insert`, `update`, or `delete` query must return `int`, `boolean`, or `void`.
[source,java]
----
@ -670,6 +670,12 @@ An `insert`, `update`, or `delete` query must return `int` or `void`.
int deleteAllBooks();
----
[source,java]
----
@HQL("update Book set discontinued = true where discontinued = false and isbn = :isbn")
boolean discontinueBook(String isbn);
----
[source,java]
----
@HQL("update Book set discontinued = true where isbn = :isbn")

View File

@ -88,8 +88,8 @@ import static java.lang.annotation.RetentionPolicy.CLASS;
* </ul>
* <p>
* For an {@code insert}, {@code update}, or {@code delete} query,
* the return type of the annotated method must be {@code int} or
* {@code void}.
* the return type of the annotated method must be {@code int},
* {@code boolean}, or {@code void}.
* <p>
* The method parameters must match the parameters of the HQL query,
* either by name or by position:
@ -125,6 +125,7 @@ import static java.lang.annotation.RetentionPolicy.CLASS;
* @see Find
*
* @author Gavin King
* @author Yanming Zhou
* @since 6.3
*/
@Target(METHOD)

View File

@ -83,6 +83,7 @@ import static org.hibernate.jpamodelgen.util.TypeUtils.getAnnotationValueRef;
* @author Hardy Ferentschik
* @author Emmanuel Bernard
* @author Gavin King
* @author Yanming Zhou
*/
public class AnnotationMetaEntity extends AnnotationMeta {
@ -1053,9 +1054,10 @@ public class AnnotationMetaEntity extends AnnotationMeta {
AnnotationValue value) {
if ( returnType == null
|| returnType.getKind() != TypeKind.VOID
&& returnType.getKind() != TypeKind.BOOLEAN
&& returnType.getKind() != TypeKind.INT ) {
context.message( method, mirror, value,
"return type of mutation query method must be 'int' or 'void'",
"return type of mutation query method must be 'int', 'boolean', or 'void'",
Diagnostic.Kind.ERROR );
}
}

View File

@ -16,6 +16,7 @@ import static org.hibernate.jpamodelgen.util.StringUtil.getUpperUnderscoreCaseFr
/**
* @author Gavin King
* @author Yanming Zhou
*/
public class QueryMethod extends AbstractQueryMethod {
private final String queryString;
@ -116,8 +117,11 @@ public class QueryMethod extends AbstractQueryMethod {
if ( isUpdate ) {
declaration
.append("\n\t\t\t.executeUpdate()");
if ( "boolean".equals(returnTypeName) ) {
declaration.append(" > 0");
}
}
else if ( containerTypeName == null) {
else if ( containerTypeName == null ) {
declaration
.append("\n\t\t\t.getSingleResult()");
}

View File

@ -74,6 +74,9 @@ public interface Dao {
@HQL("delete from Book")
int deleteBooks();
@HQL("delete from Book book where book.isbn=:isbn")
boolean deleteBook(String isbn);
@HQL("select count(*), count(*)>1 from Book")
Object[] funnyQueryReturningArray();