Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-7470 Introducing interface SchemaConstraint and add implementatio…
…ns for the constraints used in TypeSafeActivator
- Loading branch information
1 parent
d631bf6
commit 389e8d0
Showing
10 changed files
with
534 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
281 changes: 106 additions & 175 deletions
281
hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java
Large diffs are not rendered by default.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
...rnate-core/src/main/java/org/hibernate/cfg/beanvalidation/ddl/DigitsSchemaConstraint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as | ||
* indicated by the @author tags or express copyright attribution | ||
* statements applied by the authors. All third-party contributions are | ||
* distributed under license by Red Hat Inc. | ||
* | ||
* This copyrighted material is made available to anyone wishing to use, modify, | ||
* copy, or redistribute it subject to the terms and conditions of the GNU | ||
* Lesser General Public License, as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this distribution; if not, write to: | ||
* Free Software Foundation, Inc. | ||
* 51 Franklin Street, Fifth Floor | ||
* Boston, MA 02110-1301 USA | ||
*/ | ||
package org.hibernate.cfg.beanvalidation.ddl; | ||
|
||
import javax.validation.constraints.Digits; | ||
import javax.validation.metadata.ConstraintDescriptor; | ||
import javax.validation.metadata.PropertyDescriptor; | ||
|
||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.mapping.Column; | ||
import org.hibernate.mapping.Property; | ||
|
||
/** | ||
* @author Hardy Ferentschik | ||
*/ | ||
public class DigitsSchemaConstraint implements SchemaConstraint { | ||
@Override | ||
public boolean applyConstraint(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor, Dialect dialect) { | ||
if ( !Digits.class.equals( descriptor.getAnnotation().annotationType() ) ) { | ||
return false; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
ConstraintDescriptor<Digits> digitsConstraint = ( ConstraintDescriptor<Digits> ) descriptor; | ||
int integerDigits = digitsConstraint.getAnnotation().integer(); | ||
int fractionalDigits = digitsConstraint.getAnnotation().fraction(); | ||
Column col = ( Column ) property.getColumnIterator().next(); | ||
col.setPrecision( integerDigits + fractionalDigits ); | ||
col.setScale( fractionalDigits ); | ||
return true; | ||
} | ||
} | ||
|
||
|
59 changes: 59 additions & 0 deletions
59
...rnate-core/src/main/java/org/hibernate/cfg/beanvalidation/ddl/LengthSchemaConstraint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as | ||
* indicated by the @author tags or express copyright attribution | ||
* statements applied by the authors. All third-party contributions are | ||
* distributed under license by Red Hat Inc. | ||
* | ||
* This copyrighted material is made available to anyone wishing to use, modify, | ||
* copy, or redistribute it subject to the terms and conditions of the GNU | ||
* Lesser General Public License, as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this distribution; if not, write to: | ||
* Free Software Foundation, Inc. | ||
* 51 Franklin Street, Fifth Floor | ||
* Boston, MA 02110-1301 USA | ||
*/ | ||
package org.hibernate.cfg.beanvalidation.ddl; | ||
|
||
import javax.validation.metadata.ConstraintDescriptor; | ||
import javax.validation.metadata.PropertyDescriptor; | ||
|
||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.mapping.Column; | ||
import org.hibernate.mapping.Property; | ||
|
||
/** | ||
* @author Hardy Ferentschik | ||
*/ | ||
public class LengthSchemaConstraint implements SchemaConstraint { | ||
// apply hibernate validator specific constraints - we cannot import any HV specific classes though! | ||
// no need to check explicitly for @Range. @Range is a composed constraint using @Min and @Max which | ||
// will be taken care later | ||
private static final String LENGTH_CONSTRAINT = "org.hibernate.validator.constraints.Length"; | ||
|
||
@Override | ||
public boolean applyConstraint(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor, Dialect dialect) { | ||
if ( !LENGTH_CONSTRAINT.equals( descriptor.getAnnotation().annotationType().getName() ) | ||
|| !String.class.equals( propertyDescriptor.getElementClass() ) ) { | ||
return false; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
int max = ( Integer ) descriptor.getAttributes().get( "max" ); | ||
Column col = ( Column ) property.getColumnIterator().next(); | ||
if ( max < Integer.MAX_VALUE ) { | ||
col.setLength( max ); | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
|
52 changes: 52 additions & 0 deletions
52
hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/ddl/MaxSchemaConstraint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as | ||
* indicated by the @author tags or express copyright attribution | ||
* statements applied by the authors. All third-party contributions are | ||
* distributed under license by Red Hat Inc. | ||
* | ||
* This copyrighted material is made available to anyone wishing to use, modify, | ||
* copy, or redistribute it subject to the terms and conditions of the GNU | ||
* Lesser General Public License, as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this distribution; if not, write to: | ||
* Free Software Foundation, Inc. | ||
* 51 Franklin Street, Fifth Floor | ||
* Boston, MA 02110-1301 USA | ||
*/ | ||
package org.hibernate.cfg.beanvalidation.ddl; | ||
|
||
import javax.validation.constraints.Max; | ||
import javax.validation.metadata.ConstraintDescriptor; | ||
import javax.validation.metadata.PropertyDescriptor; | ||
|
||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.mapping.Column; | ||
import org.hibernate.mapping.Property; | ||
|
||
/** | ||
* @author Hardy Ferentschik | ||
*/ | ||
public class MaxSchemaConstraint implements SchemaConstraint { | ||
@Override | ||
public boolean applyConstraint(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor, Dialect dialect) { | ||
if ( !Max.class.equals( descriptor.getAnnotation().annotationType() ) ) { | ||
return false; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
ConstraintDescriptor<Max> maxConstraint = ( ConstraintDescriptor<Max> ) descriptor; | ||
long max = maxConstraint.getAnnotation().value(); | ||
Column col = ( Column ) property.getColumnIterator().next(); | ||
String checkConstraint = col.getQuotedName( dialect ) + "<=" + max; | ||
SchemaModificationHelper.applySQLCheck( col, checkConstraint ); | ||
return true; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/ddl/MinSchemaConstraint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as | ||
* indicated by the @author tags or express copyright attribution | ||
* statements applied by the authors. All third-party contributions are | ||
* distributed under license by Red Hat Inc. | ||
* | ||
* This copyrighted material is made available to anyone wishing to use, modify, | ||
* copy, or redistribute it subject to the terms and conditions of the GNU | ||
* Lesser General Public License, as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this distribution; if not, write to: | ||
* Free Software Foundation, Inc. | ||
* 51 Franklin Street, Fifth Floor | ||
* Boston, MA 02110-1301 USA | ||
*/ | ||
package org.hibernate.cfg.beanvalidation.ddl; | ||
|
||
import javax.validation.constraints.Min; | ||
import javax.validation.metadata.ConstraintDescriptor; | ||
import javax.validation.metadata.PropertyDescriptor; | ||
|
||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.mapping.Column; | ||
import org.hibernate.mapping.Property; | ||
|
||
/** | ||
* @author Hardy Ferentschik | ||
*/ | ||
public class MinSchemaConstraint implements SchemaConstraint { | ||
@Override | ||
public boolean applyConstraint(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor, Dialect dialect) { | ||
if ( !Min.class.equals( descriptor.getAnnotation().annotationType() ) ) { | ||
return false; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
ConstraintDescriptor<Min> minConstraint = ( ConstraintDescriptor<Min> ) descriptor; | ||
long min = minConstraint.getAnnotation().value(); | ||
|
||
Column col = ( Column ) property.getColumnIterator().next(); | ||
String checkConstraint = col.getQuotedName( dialect ) + ">=" + min; | ||
SchemaModificationHelper.applySQLCheck( col, checkConstraint ); | ||
return true; | ||
|
||
|
||
} | ||
} | ||
|
||
|
60 changes: 60 additions & 0 deletions
60
...nate-core/src/main/java/org/hibernate/cfg/beanvalidation/ddl/NotNullSchemaConstraint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as | ||
* indicated by the @author tags or express copyright attribution | ||
* statements applied by the authors. All third-party contributions are | ||
* distributed under license by Red Hat Inc. | ||
* | ||
* This copyrighted material is made available to anyone wishing to use, modify, | ||
* copy, or redistribute it subject to the terms and conditions of the GNU | ||
* Lesser General Public License, as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this distribution; if not, write to: | ||
* Free Software Foundation, Inc. | ||
* 51 Franklin Street, Fifth Floor | ||
* Boston, MA 02110-1301 USA | ||
*/ | ||
package org.hibernate.cfg.beanvalidation.ddl; | ||
|
||
import java.util.Iterator; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.metadata.ConstraintDescriptor; | ||
import javax.validation.metadata.PropertyDescriptor; | ||
|
||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.mapping.Column; | ||
import org.hibernate.mapping.Property; | ||
import org.hibernate.mapping.SingleTableSubclass; | ||
|
||
/** | ||
* @author Hardy Ferentschik | ||
*/ | ||
public class NotNullSchemaConstraint implements SchemaConstraint { | ||
@Override | ||
public boolean applyConstraint(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor, Dialect dialect) { | ||
if ( !NotNull.class.equals( descriptor.getAnnotation().annotationType() ) ) { | ||
return false; | ||
} | ||
|
||
if ( !( property.getPersistentClass() instanceof SingleTableSubclass ) ) { | ||
//single table should not be forced to null | ||
if ( !property.isComposite() ) { //composite should not add not-null on all columns | ||
@SuppressWarnings("unchecked") | ||
Iterator<Column> iter = property.getColumnIterator(); | ||
while ( iter.hasNext() ) { | ||
iter.next().setNullable( false ); | ||
|
||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/ddl/SchemaConstraint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as | ||
* indicated by the @author tags or express copyright attribution | ||
* statements applied by the authors. All third-party contributions are | ||
* distributed under license by Red Hat Inc. | ||
* | ||
* This copyrighted material is made available to anyone wishing to use, modify, | ||
* copy, or redistribute it subject to the terms and conditions of the GNU | ||
* Lesser General Public License, as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this distribution; if not, write to: | ||
* Free Software Foundation, Inc. | ||
* 51 Franklin Street, Fifth Floor | ||
* Boston, MA 02110-1301 USA | ||
*/ | ||
package org.hibernate.cfg.beanvalidation.ddl; | ||
|
||
import javax.validation.metadata.ConstraintDescriptor; | ||
import javax.validation.metadata.PropertyDescriptor; | ||
|
||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.mapping.Property; | ||
|
||
/** | ||
* Interface for modification of schema related meta information based on Bean Validation constraints | ||
* | ||
* @author Hardy Ferentschik | ||
*/ | ||
public interface SchemaConstraint { | ||
boolean applyConstraint(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor, Dialect dialect); | ||
} | ||
|
||
|
47 changes: 47 additions & 0 deletions
47
...ate-core/src/main/java/org/hibernate/cfg/beanvalidation/ddl/SchemaModificationHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as | ||
* indicated by the @author tags or express copyright attribution | ||
* statements applied by the authors. All third-party contributions are | ||
* distributed under license by Red Hat Inc. | ||
* | ||
* This copyrighted material is made available to anyone wishing to use, modify, | ||
* copy, or redistribute it subject to the terms and conditions of the GNU | ||
* Lesser General Public License, as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this distribution; if not, write to: | ||
* Free Software Foundation, Inc. | ||
* 51 Franklin Street, Fifth Floor | ||
* Boston, MA 02110-1301 USA | ||
*/ | ||
package org.hibernate.cfg.beanvalidation.ddl; | ||
|
||
import org.hibernate.internal.util.StringHelper; | ||
import org.hibernate.mapping.Column; | ||
|
||
/** | ||
* @author Hardy Ferentschik | ||
*/ | ||
public class SchemaModificationHelper { | ||
private SchemaModificationHelper() { | ||
} | ||
|
||
public static void applySQLCheck(Column col, String checkConstraint) { | ||
String existingCheck = col.getCheckConstraint(); | ||
// need to check whether the new check is already part of the existing check, because applyDDL can be called | ||
// multiple times | ||
if ( StringHelper.isNotEmpty( existingCheck ) && !existingCheck.contains( checkConstraint ) ) { | ||
checkConstraint = col.getCheckConstraint() + " AND " + checkConstraint; | ||
} | ||
col.setCheckConstraint( checkConstraint ); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.