Author: epbernard
Date: 2009-05-06 11:20:18 -0400 (Wed, 06 May 2009)
New Revision: 16515
Modified:
core/trunk/core/src/main/java/org/hibernate/cfg/Environment.java
core/trunk/core/src/main/java/org/hibernate/cfg/Settings.java
core/trunk/core/src/main/java/org/hibernate/cfg/SettingsFactory.java
core/trunk/core/src/main/java/org/hibernate/engine/Nullability.java
Log:
HHH-3898 hibernate.check_nullability (default true)
Modified: core/trunk/core/src/main/java/org/hibernate/cfg/Environment.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/cfg/Environment.java 2009-05-06 01:57:32
UTC (rev 16514)
+++ core/trunk/core/src/main/java/org/hibernate/cfg/Environment.java 2009-05-06 15:20:18
UTC (rev 16515)
@@ -503,6 +503,14 @@
*/
public static final String JACC_CONTEXTID = "hibernate.jacc_context_id";
+ /**
+ * Enable nullability checking.
+ * Raises an exception if a property marked as not-null is null.
+ * Default to true.
+ */
+ public static final String CHECK_NULLABILITY = "hibernate.check_nullability";
+
+
public static final String BYTECODE_PROVIDER = "hibernate.bytecode.provider";
public static final String JPAQL_STRICT_COMPLIANCE=
"hibernate.query.jpaql_strict_compliance";
Modified: core/trunk/core/src/main/java/org/hibernate/cfg/Settings.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/cfg/Settings.java 2009-05-06 01:57:32 UTC
(rev 16514)
+++ core/trunk/core/src/main/java/org/hibernate/cfg/Settings.java 2009-05-06 15:20:18 UTC
(rev 16515)
@@ -95,6 +95,7 @@
private boolean strictJPAQLCompliance;
private boolean namedQueryStartupCheckingEnabled;
private EntityTuplizerFactory entityTuplizerFactory;
+ private boolean checkNullability;
// private ComponentTuplizerFactory componentTuplizerFactory; todo : HHH-3517 and
HHH-1907
// private BytecodeProvider bytecodeProvider;
@@ -489,7 +490,15 @@
this.entityTuplizerFactory = entityTuplizerFactory;
}
-// void setComponentTuplizerFactory(ComponentTuplizerFactory componentTuplizerFactory) {
+ public boolean isCheckNullability() {
+ return checkNullability;
+ }
+
+ public void setCheckNullability(boolean checkNullability) {
+ this.checkNullability = checkNullability;
+ }
+
+ // void setComponentTuplizerFactory(ComponentTuplizerFactory componentTuplizerFactory)
{
// this.componentTuplizerFactory = componentTuplizerFactory;
// }
Modified: core/trunk/core/src/main/java/org/hibernate/cfg/SettingsFactory.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/cfg/SettingsFactory.java 2009-05-06
01:57:32 UTC (rev 16514)
+++ core/trunk/core/src/main/java/org/hibernate/cfg/SettingsFactory.java 2009-05-06
15:20:18 UTC (rev 16515)
@@ -328,6 +328,11 @@
log.info( "Named query checking : " + enabledDisabled( namedQueryChecking )
);
settings.setNamedQueryStartupCheckingEnabled( namedQueryChecking );
+ boolean checkNullability = PropertiesHelper.getBoolean(Environment.CHECK_NULLABILITY,
properties, true);
+ log.info( "Check Nullability in Core (should be disabled when Bean Validation is
on): " + enabledDisabled(useStatistics) );
+ settings.setCheckNullability(checkNullability);
+
+
// String provider = properties.getProperty( Environment.BYTECODE_PROVIDER );
// log.info( "Bytecode provider name : " + provider );
// BytecodeProvider bytecodeProvider = buildBytecodeProvider( provider );
Modified: core/trunk/core/src/main/java/org/hibernate/engine/Nullability.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/engine/Nullability.java 2009-05-06
01:57:32 UTC (rev 16514)
+++ core/trunk/core/src/main/java/org/hibernate/engine/Nullability.java 2009-05-06
15:20:18 UTC (rev 16515)
@@ -42,9 +42,11 @@
public final class Nullability {
private final SessionImplementor session;
-
+ private final boolean checkNullability;
+
public Nullability(SessionImplementor session) {
this.session = session;
+ this.checkNullability = session.getFactory().getSettings().isCheckNullability();
}
/**
* Check nullability of the class persister properties
@@ -60,60 +62,65 @@
final EntityPersister persister,
final boolean isUpdate)
throws PropertyValueException, HibernateException {
-
/*
- * Algorithm
- * Check for any level one nullability breaks
- * Look at non null components to
- * recursively check next level of nullability breaks
- * Look at Collections contraining component to
- * recursively check next level of nullability breaks
- *
- *
- * In the previous implementation, not-null stuffs where checked
- * filtering by level one only updateable
- * or insertable columns. So setting a sub component as update="false"
- * has no effect on not-null check if the main component had good checkeability
- * In this implementation, we keep this feature.
- * However, I never see any documentation mentioning that, but it's for
- * sure a limitation.
- */
+ * Typically when Bean Validation is on, we don't want to validate null values
+ * at the Hibernate Core level. Hence the checkNullability setting.
+ */
+ if ( checkNullability ) {
+ /*
+ * Algorithm
+ * Check for any level one nullability breaks
+ * Look at non null components to
+ * recursively check next level of nullability breaks
+ * Look at Collections contraining component to
+ * recursively check next level of nullability breaks
+ *
+ *
+ * In the previous implementation, not-null stuffs where checked
+ * filtering by level one only updateable
+ * or insertable columns. So setting a sub component as update="false"
+ * has no effect on not-null check if the main component had good checkeability
+ * In this implementation, we keep this feature.
+ * However, I never see any documentation mentioning that, but it's for
+ * sure a limitation.
+ */
- final boolean[] nullability = persister.getPropertyNullability();
- final boolean[] checkability = isUpdate ?
- persister.getPropertyUpdateability() :
- persister.getPropertyInsertability();
- final Type[] propertyTypes = persister.getPropertyTypes();
+ final boolean[] nullability = persister.getPropertyNullability();
+ final boolean[] checkability = isUpdate ?
+ persister.getPropertyUpdateability() :
+ persister.getPropertyInsertability();
+ final Type[] propertyTypes = persister.getPropertyTypes();
- for ( int i = 0; i < values.length; i++ ) {
-
- if ( checkability[i] && values[i]!=LazyPropertyInitializer.UNFETCHED_PROPERTY
) {
- final Object value = values[i];
- if ( !nullability[i] && value == null ) {
-
- //check basic level one nullablilty
- throw new PropertyValueException(
- "not-null property references a null or transient value",
- persister.getEntityName(),
- persister.getPropertyNames()[i]
- );
-
- }
- else if ( value != null ) {
-
- //values is not null and is checkable, we'll look deeper
- String breakProperties = checkSubElementsNullability( propertyTypes[i], value );
- if ( breakProperties != null ) {
+ for ( int i = 0; i < values.length; i++ ) {
+
+ if ( checkability[i] && values[i]!=LazyPropertyInitializer.UNFETCHED_PROPERTY
) {
+ final Object value = values[i];
+ if ( !nullability[i] && value == null ) {
+
+ //check basic level one nullablilty
throw new PropertyValueException(
- "not-null property references a null or transient value",
- persister.getEntityName(),
- buildPropertyPath( persister.getPropertyNames()[i], breakProperties )
- );
+ "not-null property references a null or transient value",
+ persister.getEntityName(),
+ persister.getPropertyNames()[i]
+ );
+
}
-
+ else if ( value != null ) {
+
+ //values is not null and is checkable, we'll look deeper
+ String breakProperties = checkSubElementsNullability( propertyTypes[i], value );
+ if ( breakProperties != null ) {
+ throw new PropertyValueException(
+ "not-null property references a null or transient value",
+ persister.getEntityName(),
+ buildPropertyPath( persister.getPropertyNames()[i], breakProperties )
+ );
+ }
+
+ }
}
+
}
-
}
}
Show replies by date