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

The following examples show how to use javax.persistence.Column#precision() . 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: AccessibleProperty.java    From warpdb with Apache License 2.0 5 votes vote down vote up
private static String getDefaultColumnType(Class<?> type, Column col) {
	String ddl = DEFAULT_COLUMN_TYPES.get(type);
	if (ddl.equals("VARCHAR($1)")) {
		ddl = ddl.replace("$1", String.valueOf(col == null ? 255 : col.length()));
	}
	if (ddl.equals("DECIMAL($1,$2)")) {
		int preci = col == null ? 0 : col.precision();
		int scale = col == null ? 0 : col.scale();
		if (preci == 0) {
			preci = 10; // default DECIMAL precision of MySQL
		}
		ddl = ddl.replace("$1", String.valueOf(preci)).replace("$2", String.valueOf(scale));
	}
	return ddl;
}
 
Example 2
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);
   }
}
 
Example 3
Source File: JPAEdmFacets.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private static void setPrecision(final Column column, final SimpleProperty edmProperty) {
  if (column.precision() > 0) {
    ((Facets) edmProperty.getFacets()).setPrecision(column.precision());
  }
}
 
Example 4
Source File: JPAEdmFacets.java    From cloud-odata-java with Apache License 2.0 4 votes vote down vote up
private static void setPrecision(final Column column, final SimpleProperty edmProperty) {
  if (column.precision() > 0) {
    ((Facets) edmProperty.getFacets()).setPrecision(column.precision());
  }
}