Java Code Examples for javax.persistence.Column#nullable()

The following examples show how to use javax.persistence.Column#nullable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractCellPostParser.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
@Override
public void parse(Context context) {
	if (context.getValue() != null) {
		MappingRule mappingRule =context.getCurrentMappingRule();
		BeanMap beanMap = BeanMap.create(context.getCurrentEntity());
		if (context.getValue() != Constants.IGNORE_ERROR_FORMAT_DATA) {
			Field field = ReflectionUtils.findField(context.getEntityClass(), mappingRule.getPropertyName());
			Column column = field.getAnnotation(Column.class);
			if (column.nullable()) {
				if (context.getValue() == null) {
					throw new DataNullableException(context.getCurrentCell().getRow(), context.getCurrentCell().getCol());
				}
			}
			
			if (field.getType() == String.class && !field.isAnnotationPresent(Lob.class)) {
				String value = (String) context.getValue();
				if (value.getBytes().length > column.length()) {
					throw new DataLengthException(context.getCurrentCell().getRow(), context.getCurrentCell().getCol(), value, column.length());
				}
			}
			beanMap.put(mappingRule.getPropertyName(), context.getValue());
		}
	}
}
 
Example 2
Source File: AccessibleProperty.java    From warpdb with Apache License 2.0 5 votes vote down vote up
private boolean isNullable() {
	if (isId()) {
		return false;
	}
	Column col = this.accessible.getAnnotation(Column.class);
	return col == null || col.nullable();
}
 
Example 3
Source File: AbstractEntityMetaFactory.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
@Override
protected void initAttribute(MetaAttribute attr) {
	ManyToMany manyManyAnnotation = attr.getAnnotation(ManyToMany.class);
	ManyToOne manyOneAnnotation = attr.getAnnotation(ManyToOne.class);
	OneToMany oneManyAnnotation = attr.getAnnotation(OneToMany.class);
	OneToOne oneOneAnnotation = attr.getAnnotation(OneToOne.class);
	Version versionAnnotation = attr.getAnnotation(Version.class);
	Lob lobAnnotation = attr.getAnnotation(Lob.class);
	Column columnAnnotation = attr.getAnnotation(Column.class);

	boolean idAttr = attr.getAnnotation(Id.class) != null || attr.getAnnotation(EmbeddedId.class) != null;
	boolean attrGenerated = attr.getAnnotation(GeneratedValue.class) != null;

	attr.setVersion(versionAnnotation != null);
	attr.setAssociation(
			manyManyAnnotation != null || manyOneAnnotation != null || oneManyAnnotation != null || oneOneAnnotation !=
					null);

	attr.setLazy(isJpaLazy(attr.getAnnotations(), attr.isAssociation()));
	attr.setLob(lobAnnotation != null);
	attr.setFilterable(lobAnnotation == null);
	attr.setSortable(lobAnnotation == null);

	attr.setCascaded(getCascade(manyManyAnnotation, manyOneAnnotation, oneManyAnnotation, oneOneAnnotation));

	if (attr.getReadMethod() == null) {
		throw new IllegalStateException("no getter found for " + attr.getParent().getName() + "." + attr.getName());
	}
	Class<?> attributeType = attr.getReadMethod().getReturnType();
	boolean isPrimitiveType = ClassUtils.isPrimitiveType(attributeType);
	boolean columnNullable = (columnAnnotation == null || columnAnnotation.nullable()) &&
			(manyOneAnnotation == null || manyOneAnnotation.optional()) &&
			(oneOneAnnotation == null || oneOneAnnotation.optional());
	attr.setNullable(!isPrimitiveType && columnNullable);

	boolean hasSetter = attr.getWriteMethod() != null;
	attr.setInsertable(hasSetter && (columnAnnotation == null || columnAnnotation.insertable()) && !attrGenerated
			&& versionAnnotation == null);
	attr.setUpdatable(
			hasSetter && (columnAnnotation == null || columnAnnotation.updatable()) && !idAttr && versionAnnotation == null);

}
 
Example 4
Source File: JPAAnnotationsConfigurer.java    From oval with Eclipse Public License 2.0 4 votes vote down vote up
protected void initializeChecks(final Column annotation, final Collection<Check> checks, final AccessibleObject fieldOrMethod) {
   /* If the value is generated (annotated with @GeneratedValue) it is allowed to be null
    * before the entity has been persisted, same is true in case of optimistic locking
    * when a field is annotated with @Version.
    * Therefore and because of the fact that there is no generic way to determine if an entity
    * has been persisted already, a not-null check will not be performed for such fields.
    */
   if (!annotation.nullable() //
      && !fieldOrMethod.isAnnotationPresent(GeneratedValue.class) //
      && !fieldOrMethod.isAnnotationPresent(Version.class) //
      && !fieldOrMethod.isAnnotationPresent(NotNull.class) //
      && !containsCheckOfType(checks, NotNullCheck.class) //
   ) {
      checks.add(new NotNullCheck());
   }

   // add Length check based on Column.length parameter, but only:
   if (!fieldOrMethod.isAnnotationPresent(Lob.class) && // if @Lob is not present
      !fieldOrMethod.isAnnotationPresent(Enumerated.class) && // if @Enumerated is not present
      !fieldOrMethod.isAnnotationPresent(Length.class) // if an explicit @Length constraint is not present
   ) {
      final LengthCheck lengthCheck = new LengthCheck();
      lengthCheck.setMax(annotation.length());
      checks.add(lengthCheck);
   }

   // add Range check based on Column.precision/scale parameters, but only:
   if (!fieldOrMethod.isAnnotationPresent(Range.class) // if an explicit @Range is not present
      && annotation.precision() > 0 // if precision is > 0
      && Number.class.isAssignableFrom(fieldOrMethod instanceof Field //
         ? ((Field) fieldOrMethod).getType() //
         : ((Method) fieldOrMethod).getReturnType()) // if numeric field type
   ) {
      /* precision = 6, scale = 2  => -9999.99<=x<=9999.99
       * precision = 4, scale = 1  =>   -999.9<=x<=999.9
       */
      final RangeCheck rangeCheck = new RangeCheck();
      rangeCheck.setMax(Math.pow(10, annotation.precision() - annotation.scale()) - Math.pow(0.1, annotation.scale()));
      rangeCheck.setMin(-1 * rangeCheck.getMax());
      checks.add(rangeCheck);
   }
}