This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch 8.0.x-hibernate7-dev in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 45f49cb9f533744fa26d7b3cc6c870fe40a8cfd8 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Sun Mar 8 08:54:44 2026 -0500 hibernate7: PMD fixes --- .../groovy/grails/orm/CriteriaMethodInvoker.java | 26 +++++--- .../grails/orm/HibernateCriteriaBuilder.java | 69 +++++++++++++++++++++- .../orm/hibernate/GrailsHibernateTemplate.java | 16 +++++ .../grails/orm/hibernate/HibernateDatastore.java | 27 +++------ .../org/grails/orm/hibernate/HibernateSession.java | 35 ++++++++++- .../grails/orm/hibernate/cfg/IdentityEnumType.java | 20 +++++-- .../grails/orm/hibernate/query/HibernateQuery.java | 8 ++- 7 files changed, 162 insertions(+), 39 deletions(-) diff --git a/grails-data-hibernate7/core/src/main/groovy/grails/orm/CriteriaMethodInvoker.java b/grails-data-hibernate7/core/src/main/groovy/grails/orm/CriteriaMethodInvoker.java index dcfd3c8d3f..6a7ac3aee5 100644 --- a/grails-data-hibernate7/core/src/main/groovy/grails/orm/CriteriaMethodInvoker.java +++ b/grails-data-hibernate7/core/src/main/groovy/grails/orm/CriteriaMethodInvoker.java @@ -44,7 +44,7 @@ public class CriteriaMethodInvoker { this.builder = builder; } - public Object invokeMethod(String name, Object[] args) { + public Object invokeMethod(String name, Object... args) { CriteriaMethods method = CriteriaMethods.fromName(name); Object result = tryCriteriaConstruction(method, args); @@ -65,7 +65,7 @@ public class CriteriaMethodInvoker { return CriteriaMethods.fromName(name, HibernateCriteriaBuilder.class, args); } - private Object tryCriteriaConstruction(CriteriaMethods method, Object[] args) { + private Object tryCriteriaConstruction(CriteriaMethods method, Object... args) { if (method == null || !isCriteriaConstructionMethod(method, args)) { return UNHANDLED; } @@ -76,6 +76,7 @@ public class CriteriaMethodInvoker { case SCROLL_CALL -> builder.setScroll(true); case COUNT_CALL -> builder.setCount(true); case LIST_DISTINCT_CALL -> builder.setDistinct(true); + default -> {} } // Check for pagination params @@ -137,7 +138,7 @@ public class CriteriaMethodInvoker { return result; } - private Object tryMetaMethod(String name, Object[] args) { + private Object tryMetaMethod(String name, Object... args) { MetaMethod metaMethod = builder.getMetaClass().getMetaMethod(name, args); if (metaMethod != null) { return metaMethod.invoke(builder, args); @@ -146,7 +147,7 @@ public class CriteriaMethodInvoker { } @SuppressWarnings("PMD.DataflowAnomalyAnalysis") - private Object tryAssociationOrJunction(String name, CriteriaMethods method, Object[] args) { + private Object tryAssociationOrJunction(String name, CriteriaMethods method, Object... args) { if (!isAssociationQueryMethod(args) && !isAssociationQueryWithJoinSpecificationMethod(args)) { return UNHANDLED; } @@ -172,6 +173,8 @@ public class CriteriaMethodInvoker { return name; } break; + default: + break; } } @@ -204,7 +207,7 @@ public class CriteriaMethodInvoker { } @SuppressWarnings("PMD.DataflowAnomalyAnalysis") - protected Object trySimpleCriteria(String name, CriteriaMethods method, Object[] args) { + protected Object trySimpleCriteria(String name, CriteriaMethods method, Object... args) { if (args.length != 1 || args[0] == null) { return UNHANDLED; } @@ -233,15 +236,18 @@ public class CriteriaMethodInvoker { builder.getHibernateQuery().isEmpty(value); case IS_NOT_EMPTY -> builder.getHibernateQuery().isNotEmpty(value); + default -> {} } return name; + default: + break; } } return UNHANDLED; } @SuppressWarnings("PMD.AvoidLiteralsInIfCondition") - protected Object tryPropertyCriteria(CriteriaMethods method, Object[] args) { + protected Object tryPropertyCriteria(CriteriaMethods method, Object... args) { if (method == null || args.length < 2 || !(args[0] instanceof String propertyName)) { return UNHANDLED; } @@ -297,19 +303,21 @@ public class CriteriaMethodInvoker { return builder.sizeEq(propertyName, ((Number) args[1]).intValue()); } break; + default: + break; } return UNHANDLED; } - private boolean isAssociationQueryMethod(Object[] args) { + private boolean isAssociationQueryMethod(Object... args) { return args.length == 1 && args[0] instanceof Closure; } - private boolean isAssociationQueryWithJoinSpecificationMethod(Object[] args) { + private boolean isAssociationQueryWithJoinSpecificationMethod(Object... args) { return args.length == 2 && (args[0] instanceof Number) && (args[1] instanceof Closure); } - private boolean isCriteriaConstructionMethod(CriteriaMethods method, Object[] args) { + private boolean isCriteriaConstructionMethod(CriteriaMethods method, Object... args) { return (method == CriteriaMethods.LIST_CALL && args.length == 2 && args[0] instanceof Map diff --git a/grails-data-hibernate7/core/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java b/grails-data-hibernate7/core/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java index f381729239..1464f753e5 100644 --- a/grails-data-hibernate7/core/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java +++ b/grails-data-hibernate7/core/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java @@ -152,6 +152,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * * @param propertyName The name of the property */ + @Override public ProjectionList property(String propertyName) { hibernateQuery.projections().property(propertyName); return this; @@ -166,6 +167,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * * @param propertyName The property name */ + @Override public ProjectionList distinct(String propertyName) { hibernateQuery.projections().distinct(propertyName); return this; @@ -176,6 +178,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * * @param propertyName The name of the property */ + @Override public ProjectionList avg(String propertyName) { hibernateQuery.projections().avg(propertyName); return this; @@ -186,11 +189,13 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * * @param associationPath The path of the association */ + @Override public BuildableCriteria join(String associationPath) { join(associationPath, JoinType.INNER); return this; } + @Override public BuildableCriteria join(String property, JoinType joinType) { hibernateQuery.join(property, joinType); return this; @@ -210,6 +215,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * * @param associationPath The path of the association */ + @Override public BuildableCriteria select(String associationPath) { hibernateQuery.select(associationPath); return this; @@ -220,6 +226,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * * @param shouldCache True if the query should be cached */ + @Override public BuildableCriteria cache(boolean shouldCache) { this.shouldCache = shouldCache; return this; @@ -235,12 +242,13 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * * @param readOnly True to disable dirty checking */ + @Override public BuildableCriteria readOnly(boolean readOnly) { this.readOnly = readOnly; return this; } - + @Override public Class<?> getTargetClass() { return targetClass; } @@ -269,11 +277,13 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui return (Objects.nonNull(alias) ? alias + "." : "") + propertyName; } + @Override public ProjectionList id() { hibernateQuery.projections().id(); return this; } + @Override public ProjectionList count() { return hibernateQuery.projections().count(); } @@ -283,6 +293,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * * @param propertyName The name of the property */ + @Override public ProjectionList countDistinct(String propertyName) { return countDistinct(propertyName, null); } @@ -292,10 +303,12 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * * @param propertyName The name of the property */ + @Override public ProjectionList groupProperty(String propertyName) { return groupProperty(propertyName, null); } + @Override public ProjectionList distinct() { hibernateQuery.projections().distinct(); return this; @@ -328,6 +341,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * * @param propertyName The name of the property */ + @Override public ProjectionList max(String propertyName) { return max(propertyName, null); } @@ -348,6 +362,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * * @param propertyName The name of the property */ + @Override public ProjectionList min(String propertyName) { return min(propertyName, null); } @@ -363,6 +378,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui } /** Adds a projection that allows the criteria to return the row count */ + @Override public ProjectionList rowCount() { return count(); } @@ -372,6 +388,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * * @param propertyName The name of the property */ + @Override public ProjectionList sum(String propertyName) { return sum(propertyName, null); } @@ -408,6 +425,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param otherPropertyName The second property name * @return A Criterion instance */ + @Override public Criteria eqProperty(String propertyName, String otherPropertyName) { hibernateQuery.eqProperty(propertyName, otherPropertyName); return this; @@ -420,6 +438,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param otherPropertyName The second property name * @return A Criterion instance */ + @Override public Criteria neProperty(String propertyName, String otherPropertyName) { hibernateQuery.neProperty(propertyName, otherPropertyName); return this; @@ -432,6 +451,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param otherPropertyName The second property name * @return A Criterion instance */ + @Override public Criteria gtProperty(String propertyName, String otherPropertyName) { hibernateQuery.gtProperty(propertyName, otherPropertyName); return this; @@ -445,6 +465,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param otherPropertyName The second property name * @return A Criterion instance */ + @Override public Criteria geProperty(String propertyName, String otherPropertyName) { hibernateQuery.geProperty(propertyName, otherPropertyName); return this; @@ -457,6 +478,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param otherPropertyName The second property name * @return A Criterion instance */ + @Override public Criteria ltProperty(String propertyName, String otherPropertyName) { hibernateQuery.ltProperty(propertyName, otherPropertyName); return this; @@ -470,6 +492,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param otherPropertyName The second property name * @return A Criterion instance */ + @Override public Criteria leProperty(String propertyName, String otherPropertyName) { hibernateQuery.leProperty(propertyName, otherPropertyName); return this; @@ -489,6 +512,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override @SuppressWarnings({"unchecked", "rawtypes"}) public Criteria eqAll(String propertyName, Closure<?> propertyValue) { return eqAll(propertyName, new grails.gorm.DetachedCriteria(targetClass).build(propertyValue)); @@ -502,6 +526,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override @SuppressWarnings({"unchecked", "rawtypes"}) public Criteria gtAll(String propertyName, Closure<?> propertyValue) { return gtAll(propertyName, new grails.gorm.DetachedCriteria(targetClass).build(propertyValue)); @@ -515,6 +540,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override @SuppressWarnings({"unchecked", "rawtypes"}) public Criteria ltAll(String propertyName, Closure<?> propertyValue) { return ltAll(propertyName, new grails.gorm.DetachedCriteria(targetClass).build(propertyValue)); @@ -528,6 +554,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override @SuppressWarnings({"unchecked", "rawtypes"}) public Criteria geAll(String propertyName, Closure<?> propertyValue) { return geAll(propertyName, new grails.gorm.DetachedCriteria(targetClass).build(propertyValue)); @@ -541,6 +568,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override @SuppressWarnings({"unchecked", "rawtypes"}) public Criteria leAll(String propertyName, Closure<?> propertyValue) { return leAll(propertyName, new grails.gorm.DetachedCriteria(targetClass).build(propertyValue)); @@ -554,6 +582,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override public Criteria eqAll( String propertyName, @SuppressWarnings("rawtypes") QueryableCriteria propertyValue) { hibernateQuery.eqAll(propertyName, propertyValue); @@ -568,6 +597,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override public Criteria gtAll( String propertyName, @SuppressWarnings("rawtypes") QueryableCriteria propertyValue) { hibernateQuery.gtAll(propertyName, propertyValue); @@ -657,6 +687,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override public Criteria ltAll( String propertyName, @SuppressWarnings("rawtypes") QueryableCriteria propertyValue) { hibernateQuery.ltAll(propertyName, propertyValue); @@ -671,6 +702,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override public Criteria geAll( String propertyName, @SuppressWarnings("rawtypes") QueryableCriteria propertyValue) { hibernateQuery.geAll(propertyName, propertyValue); @@ -685,6 +717,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override public Criteria leAll( String propertyName, @SuppressWarnings("rawtypes") QueryableCriteria propertyValue) { hibernateQuery.leAll(propertyName, propertyValue); @@ -698,11 +731,13 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override public Criteria gt(String propertyName, Object propertyValue) { hibernateQuery.gt(propertyName, propertyValue); return this; } + @Override public Criteria lte(String s, Object o) { return le(s, o); } @@ -714,6 +749,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override public Criteria ge(String propertyName, Object propertyValue) { hibernateQuery.ge(propertyName, propertyValue); return this; @@ -726,6 +762,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override public Criteria lt(String propertyName, Object propertyValue) { hibernateQuery.lt(propertyName, propertyValue); return this; @@ -738,11 +775,13 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override public Criteria le(String propertyName, Object propertyValue) { hibernateQuery.le(propertyName, propertyValue); return this; } + @Override public Criteria idEquals(Object o) { return idEq(o); } @@ -759,21 +798,25 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui return this; } + @Override public Criteria isEmpty(String property) { hibernateQuery.isEmpty(property); return this; } + @Override public Criteria isNotEmpty(String property) { hibernateQuery.isNotEmpty(property); return this; } + @Override public Criteria isNull(String property) { hibernateQuery.isNull(property); return this; } + @Override public Criteria isNotNull(String property) { hibernateQuery.isNotNull(property); return this; @@ -804,10 +847,12 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return A Criterion instance */ + @Override public Criteria eq(String propertyName, Object propertyValue) { return eq(propertyName, propertyValue, Collections.emptyMap()); } + @Override public Criteria idEq(Object o) { return eq("id", o); } @@ -847,6 +892,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The like value * @return A Criterion instance */ + @Override public Criteria like(String propertyName, Object propertyValue) { hibernateQuery.like(propertyName, propertyValue.toString()); return this; @@ -860,6 +906,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The ilike value * @return A Criterion instance */ + @Override public Criteria ilike(String propertyName, Object propertyValue) { hibernateQuery.ilike(propertyName, propertyValue.toString()); return this; @@ -872,6 +919,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param values A collection of values * @return A Criterion instance */ + @Override @SuppressWarnings("rawtypes") public Criteria in(String propertyName, Collection values) { hibernateQuery.in(propertyName, values.stream().toList()); @@ -879,13 +927,15 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui } /** Delegates to in as in is a Groovy keyword */ + @Override @SuppressWarnings("rawtypes") public Criteria inList(String propertyName, Collection values) { return in(propertyName, values); } /** Delegates to in as in is a Groovy keyword */ - public Criteria inList(String propertyName, Object[] values) { + @Override + public Criteria inList(String propertyName, Object... values) { return in(propertyName, values); } @@ -896,7 +946,8 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param values A collection of values * @return A Criterion instance */ - public Criteria in(String propertyName, Object[] values) { + @Override + public Criteria in(String propertyName, Object... values) { hibernateQuery.in(propertyName, List.of(values)); return this; } @@ -907,6 +958,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyName The property name to order by * @return A Order instance */ + @Override public Criteria order(String propertyName) { order(new Query.Order(propertyName)); return this; @@ -930,6 +982,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param directionString Either "asc" for ascending or "desc" for descending * @return A Order instance */ + @Override public Criteria order(String propertyName, String directionString) { Query.Order.Direction direction = Query.Order.Direction.DESC.name().equalsIgnoreCase(directionString) @@ -946,6 +999,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param size The size to constrain by * @return A Criterion instance */ + @Override public Criteria sizeEq(String propertyName, int size) { hibernateQuery.sizeEq(propertyName, size); return this; @@ -958,6 +1012,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param size The size to constrain by * @return A Criterion instance */ + @Override public Criteria sizeGt(String propertyName, int size) { hibernateQuery.sizeGt(propertyName, size); return this; @@ -971,6 +1026,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param size The size to constrain by * @return A Criterion instance */ + @Override public Criteria sizeGe(String propertyName, int size) { hibernateQuery.sizeGe(propertyName, size); return this; @@ -984,6 +1040,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param size The size to constrain by * @return A Criterion instance */ + @Override public Criteria sizeLe(String propertyName, int size) { hibernateQuery.sizeLe(propertyName, size); return this; @@ -996,6 +1053,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param size The size to constrain by * @return A Criterion instance */ + @Override public Criteria sizeLt(String propertyName, int size) { hibernateQuery.sizeLt(propertyName, size); return this; @@ -1009,6 +1067,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The ilike value * @return A Criterion instance */ + @Override public org.grails.datastore.mapping.query.api.Criteria rlike( String propertyName, Object propertyValue) { hibernateQuery.rlike(propertyName, propertyValue.toString()); @@ -1022,6 +1081,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param size The size to constrain by * @return A Criterion instance */ + @Override public Criteria sizeNe(String propertyName, int size) { hibernateQuery.sizeNe(propertyName, size); return this; @@ -1034,6 +1094,7 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param propertyValue The property value * @return The criterion object */ + @Override public Criteria ne(String propertyName, Object propertyValue) { hibernateQuery.ne(propertyName, propertyValue); return this; @@ -1047,11 +1108,13 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport implements Bui * @param hi The high value * @return A Criterion instance */ + @Override public Criteria between(String propertyName, Object lo, Object hi) { hibernateQuery.between(propertyName, lo, hi); return this; } + @Override public Criteria gte(String s, Object o) { return ge(s, o); } diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/GrailsHibernateTemplate.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/GrailsHibernateTemplate.java index c75a101bb5..f9a78142de 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/GrailsHibernateTemplate.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/GrailsHibernateTemplate.java @@ -255,6 +255,7 @@ public class GrailsHibernateTemplate implements IHibernateTemplate { } } + @Override public SessionFactory getSessionFactory() { return sessionFactory; } @@ -404,16 +405,19 @@ public class GrailsHibernateTemplate implements IHibernateTemplate { new CloseSuppressingInvocationHandler(session, this)); } + @Override @Deprecated(since = "7.0", forRemoval = true) public <T> T get(final Class<T> entityClass, final Serializable id) throws DataAccessException { return doExecute(session -> session.find(entityClass, id), true); } + @Override @Deprecated(since = "7.0", forRemoval = true) public <T> T get(final Class<T> entityClass, final Serializable id, final LockMode mode) { return lock(entityClass, id, mode); } + @Override public void remove(final Object entity) throws DataAccessException { doExecute( session -> { @@ -424,6 +428,7 @@ public class GrailsHibernateTemplate implements IHibernateTemplate { } + @Override public <T> T load(final Class<T> entityClass, final Serializable id) throws DataAccessException { return doExecute(session -> session.getReference(entityClass, id), true); } @@ -446,10 +451,12 @@ public class GrailsHibernateTemplate implements IHibernateTemplate { true); } + @Override public boolean contains(final Object entity) throws DataAccessException { return doExecute(session -> session.contains(entity), true); } + @Override public void evict(final Object entity) throws DataAccessException { doExecute( session -> { @@ -459,6 +466,7 @@ public class GrailsHibernateTemplate implements IHibernateTemplate { true); } + @Override public void lock(final Object entity, final LockMode lockMode) throws DataAccessException { doExecute( session -> { @@ -468,6 +476,7 @@ public class GrailsHibernateTemplate implements IHibernateTemplate { true); } + @Override public void refresh(final Object entity) throws DataAccessException { refresh(entity, null); } @@ -599,11 +608,13 @@ public class GrailsHibernateTemplate implements IHibernateTemplate { * * @see #FLUSH_AUTO */ + @Override public void setFlushMode(int flushMode) { this.flushMode = flushMode; } /** Return if a flush should be forced after executing the callback code. */ + @Override public int getFlushMode() { return flushMode; } @@ -696,6 +707,7 @@ public class GrailsHibernateTemplate implements IHibernateTemplate { return translator.translate("Hibernate operation: " + msg, sql, sqlException); } + @Override public void persist(final Object entity) throws DataAccessException { doExecute( session -> { @@ -705,10 +717,12 @@ public class GrailsHibernateTemplate implements IHibernateTemplate { true); } + @Override public Object merge(final Object entity) throws DataAccessException { return doExecute(session -> session.merge(entity), true); } + @Override public void flush() throws DataAccessException { doExecute( session -> { @@ -718,6 +732,7 @@ public class GrailsHibernateTemplate implements IHibernateTemplate { true); } + @Override public void clear() throws DataAccessException { doExecute( session -> { @@ -727,6 +742,7 @@ public class GrailsHibernateTemplate implements IHibernateTemplate { true); } + @Override public void deleteAll(final Collection<?> objects) { execute( (HibernateCallback<Void>) diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java index a15eeeefc9..ddf27fab84 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java @@ -519,6 +519,7 @@ public class HibernateDatastore extends AbstractDatastore * @param applicationContext The application context (may be null) * @param dataSourceName The data source name */ + @SuppressWarnings("PMD.NullAssignment") protected HibernateDatastore( MappingContext mappingContext, SessionFactory sessionFactory, @@ -587,9 +588,10 @@ public class HibernateDatastore extends AbstractDatastore * @param connectionName The connection name * @return The {@link HibernateDatastore} */ + @Override public HibernateDatastore getDatastoreForConnection(String connectionName) { - if (connectionName.equals(Settings.SETTING_DATASOURCE) - || connectionName.equals(ConnectionSource.DEFAULT)) { + if (Settings.SETTING_DATASOURCE.equals(connectionName) + || ConnectionSource.DEFAULT.equals(connectionName)) { return this; } else { HibernateDatastore hibernateDatastore = this.datastoresByConnectionSource.get(connectionName); @@ -780,7 +782,7 @@ public class HibernateDatastore extends AbstractDatastore try { connectionSources.close(); } catch (IOException e) { - LOG.error("There was an error shutting down GORM for an entity: " + e.getMessage(), e); + LOG.error("There was an error shutting down GORM for an entity: {}", e.getMessage(), e); } } finally { MappingCacheHolder.getInstance().clear(); @@ -857,28 +859,17 @@ public class HibernateDatastore extends AbstractDatastore Action schemaAutoTooling = Action.interpretHbm2ddlSetting(dbCreate); if (schemaAutoTooling != Action.VALIDATE && schemaAutoTooling != Action.NONE) { - Connection connection = null; - try { - connection = defaultConnectionSource.getDataSource().getConnection(); + try (Connection connection = defaultConnectionSource.getDataSource().getConnection()) { try { schemaHandler.useSchema(connection, schemaName); } catch (Exception e) { // schema doesn't exist schemaHandler.createSchema(connection, schemaName); } - + schemaHandler.useDefaultSchema(connection); } catch (SQLException e) { throw new DatastoreConfigurationException( - String.format("Failed to create schema for name [%s]", schemaName)); - } finally { - if (connection != null) { - try { - schemaHandler.useDefaultSchema(connection); - connection.close(); - } catch (SQLException e) { - LOG.trace("Failed to reset to default schema: {}", e.getMessage()); - } - } + String.format("Failed to create schema for name [%s]", schemaName), e); } } @@ -1152,7 +1143,7 @@ public class HibernateDatastore extends AbstractDatastore try { destroy(); } catch (Exception e) { - LOG.error("Error closing hibernate datastore: " + e.getMessage(), e); + LOG.error("Error closing hibernate datastore: {}", e.getMessage(), e); } } diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java index db4dc54ab3..37ef722632 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateSession.java @@ -86,7 +86,7 @@ public class HibernateSession extends AbstractAttributeStoringSession HibernateDatastore hibernateDatastore, SessionFactory sessionFactory) { datastore = hibernateDatastore; hibernateTemplate = - new GrailsHibernateTemplate(sessionFactory, (HibernateDatastore) getDatastore()); + new GrailsHibernateTemplate(sessionFactory, datastore); } @Override @@ -94,6 +94,7 @@ public class HibernateSession extends AbstractAttributeStoringSession return false; } + @Override public Serializable insert(Object o) { return persist(o); } @@ -108,6 +109,7 @@ public class HibernateSession extends AbstractAttributeStoringSession connected = false; // don't actually do any disconnection here. This will be handled by OSVI } + @Override public Transaction beginTransaction() { throw new UnsupportedOperationException("Use HibernatePlatformTransactionManager instead"); } @@ -117,10 +119,12 @@ public class HibernateSession extends AbstractAttributeStoringSession throw new UnsupportedOperationException("Use HibernatePlatformTransactionManager instead"); } + @Override public MappingContext getMappingContext() { return getDatastore().getMappingContext(); } + @Override public Serializable persist(Object o) { hibernateTemplate.persist(o); try { @@ -136,38 +140,47 @@ public class HibernateSession extends AbstractAttributeStoringSession return null; } + @Override public Object merge(Object o) { return hibernateTemplate.merge(o); } + @Override public void refresh(Object o) { hibernateTemplate.refresh(o); } + @Override public void attach(Object o) { hibernateTemplate.lock(o, LockMode.NONE); } + @Override public void flush() { hibernateTemplate.flush(); } + @Override public void clear() { hibernateTemplate.clear(); } + @Override public void clear(Object o) { hibernateTemplate.evict(o); } + @Override public boolean contains(Object o) { return hibernateTemplate.contains(o); } + @Override public void lock(Object o) { hibernateTemplate.lock(o, LockMode.PESSIMISTIC_WRITE); } + @Override public void unlock(Object o) { // do nothing } @@ -178,6 +191,7 @@ public class HibernateSession extends AbstractAttributeStoringSession * @return the result */ @Deprecated + @Override public List<Serializable> persist(Iterable objects) { List<Serializable> ids = new ArrayList<>(); for (Object object : objects) { @@ -187,18 +201,22 @@ public class HibernateSession extends AbstractAttributeStoringSession return ids; } + @Override public <T> T retrieve(Class<T> type, Serializable key) { return getHibernateTemplate().execute(session -> session.find(type, key)); } + @Override public <T> T proxy(Class<T> type, Serializable key) { return hibernateTemplate.load(type, key); } + @Override public <T> T lock(Class<T> type, Serializable key) { return getHibernateTemplate().execute(session -> session.find(type, key, LockModeType.PESSIMISTIC_WRITE)); } + @Override public void delete(Iterable objects) { Collection list = getIterableAsCollection(objects); hibernateTemplate.deleteAll(list); @@ -215,18 +233,22 @@ public class HibernateSession extends AbstractAttributeStoringSession return list; } + @Override public void delete(Object obj) { hibernateTemplate.remove(obj); } + @Override public List retrieveAll(Class type, Serializable... keys) { return retrieveAll(type, Arrays.asList(keys)); } + @Override public Persister getPersister(Object o) { return null; } + @Override public Transaction getTransaction() { throw new UnsupportedOperationException("Use HibernatePlatformTransactionManager instead"); } @@ -238,15 +260,18 @@ public class HibernateSession extends AbstractAttributeStoringSession return resource != null; } + @Override public Datastore getDatastore() { return datastore; } + @Override public boolean isDirty(Object o) { // not used, Hibernate manages dirty checking itself return true; } + @Override public Object getNativeInterface() { return hibernateTemplate; } @@ -281,6 +306,7 @@ public class HibernateSession extends AbstractAttributeStoringSession * @param criteria The criteria * @return The total number of records deleted */ + @Override @SuppressWarnings("PMD.DataflowAnomalyAnalysis") public long deleteAll(final QueryableCriteria criteria) { return getHibernateTemplate() @@ -312,7 +338,7 @@ public class HibernateSession extends AbstractAttributeStoringSession List<?> parameters = jpaQueryInfo.getParameters(); if (parameters != null) { - for (int i = 0, count = parameters.size(); i < count; i++) { + for (int i = 0; i < parameters.size(); i++) { query.setParameter( JpaQueryBuilder.PARAMETER_NAME_PREFIX + (i + 1), parameters.get(i)); } @@ -327,6 +353,7 @@ public class HibernateSession extends AbstractAttributeStoringSession * @param properties The properties * @return The total number of records updated */ + @Override @SuppressWarnings("PMD.DataflowAnomalyAnalysis") public long updateAll(final QueryableCriteria criteria, final Map<String, Object> properties) { return getHibernateTemplate() @@ -366,6 +393,7 @@ public class HibernateSession extends AbstractAttributeStoringSession }); } + @Override @SuppressWarnings({"PMD.DataflowAnomalyAnalysis", "unchecked"}) public List retrieveAll(final Class type, final Iterable keys) { final PersistentEntity persistentEntity = @@ -387,6 +415,7 @@ public class HibernateSession extends AbstractAttributeStoringSession }); } + @Override public Query createQuery(Class type) { return createQuery(type, null); } @@ -404,6 +433,7 @@ public class HibernateSession extends AbstractAttributeStoringSession return (GrailsHibernateTemplate) getNativeInterface(); } + @Override public void setFlushMode(FlushModeType flushMode) { if (flushMode == FlushModeType.AUTO) { hibernateTemplate.setFlushMode(GrailsHibernateTemplate.FLUSH_AUTO); @@ -412,6 +442,7 @@ public class HibernateSession extends AbstractAttributeStoringSession } } + @Override public FlushModeType getFlushMode() { if (hibernateTemplate.getFlushMode() == GrailsHibernateTemplate.FLUSH_COMMIT) { return FlushModeType.COMMIT; diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/IdentityEnumType.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/IdentityEnumType.java index 6610b8b73a..47210825f2 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/IdentityEnumType.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/IdentityEnumType.java @@ -77,6 +77,7 @@ public class IdentityEnumType implements UserType, ParameterizedType, Serializab return m; } + @Override @SuppressWarnings("unchecked") public void setParameterValues(Properties properties) { try { @@ -86,14 +87,14 @@ public class IdentityEnumType implements UserType, ParameterizedType, Serializab .getContextClassLoader() .loadClass((String) properties.get(PARAM_ENUM_CLASS)); if (LOG.isDebugEnabled()) { - LOG.debug(String.format("Building ID-mapping for Enum Class %s", enumClass.getName())); + LOG.debug("Building ID-mapping for Enum Class {}", enumClass.getName()); } bidiMap = getBidiEnumMap(enumClass); type = (AbstractStandardBasicType<?>) typeConfiguration.getBasicTypeRegistry().getRegisteredType(bidiMap.keyType.getName()); if (LOG.isDebugEnabled()) { - LOG.debug(String.format("Mapped Basic Type is %s", type)); + LOG.debug("Mapped Basic Type is {}", type); } sqlTypes = type.getSqlTypeCodes(null); } catch (Exception e) { @@ -101,8 +102,9 @@ public class IdentityEnumType implements UserType, ParameterizedType, Serializab } } + @Override public int[] getSqlTypes() { - return sqlTypes; + return sqlTypes != null ? sqlTypes.clone() : null; } @Override @@ -110,6 +112,7 @@ public class IdentityEnumType implements UserType, ParameterizedType, Serializab return 0; } + @Override public Class<?> returnedClass() { return enumClass; } @@ -124,34 +127,42 @@ public class IdentityEnumType implements UserType, ParameterizedType, Serializab return o.hashCode(); } + @Override public Object deepCopy(Object o) throws HibernateException { return o; } + @Override public boolean isMutable() { return false; } + @Override public Serializable disassemble(Object o) throws HibernateException { return (Serializable) o; } + @Override public Object assemble(Serializable cached, Object owner) throws HibernateException { return cached; } + @Override public Object replace(Object orig, Object target, Object owner) throws HibernateException { return orig; } + @Override public long getDefaultSqlLength() { return UserType.super.getDefaultSqlLength(); } + @Override public int getDefaultSqlPrecision() { return UserType.super.getDefaultSqlPrecision(); } + @Override public int getDefaultSqlScale() { return UserType.super.getDefaultSqlScale(); } @@ -190,8 +201,7 @@ public class IdentityEnumType implements UserType, ParameterizedType, Serializab enumToKey.put((Enum) value, id); if (keytoEnum.containsKey(id)) { LOG.warn( - String.format( - "Duplicate Enum ID '%s' detected for Enum %s!", id, enumClass.getName())); + "Duplicate Enum ID '{}' detected for Enum {}!", id, enumClass.getName()); } keytoEnum.put(id, value); } diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java index 7600503b27..c751eeb644 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateQuery.java @@ -22,6 +22,7 @@ import grails.gorm.DetachedCriteria; import groovy.lang.Closure; import jakarta.persistence.criteria.CriteriaQuery; import jakarta.persistence.criteria.JoinType; +import java.util.Deque; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -65,8 +66,8 @@ public class HibernateQuery extends Query { } private final Map<String, CriteriaAndAlias> createdAssociationPaths = new HashMap<>(); - protected LinkedList<PersistentEntity> entityStack = new LinkedList<>(); - protected LinkedList<Association> associationStack = new LinkedList<>(); + protected Deque<PersistentEntity> entityStack = new LinkedList<>(); + protected Deque<Association> associationStack = new LinkedList<>(); protected DetachedCriteria<?> detachedCriteria; protected ProxyHandler proxyHandler = new HibernateProxyHandler(); @@ -145,6 +146,7 @@ public class HibernateQuery extends Query { return detachedCriteria.getCriteria(); } + @Override public void add(Criterion criterion) { detachedCriteria.add(criterion); } @@ -153,6 +155,7 @@ public class HibernateQuery extends Query { detachedCriteria.add(new Conjunction(detachedCriteria.getCriteria())); } + @Override public void add(Junction currentJunction, Criterion criterion) { Disjunction disjunction = (Disjunction) @@ -617,6 +620,7 @@ public class HibernateQuery extends Query { return this; } + @Override public Query maxResults(int maxResults) { this.max = maxResults; return this;
