@Override public TypeDescriptor getDatabaseSpecificType(Column column) { if ("JSONB".equalsIgnoreCase(column.getColumnType())) { Properties typeParams = new Properties(); if(column.getActualJavaType() == Map.class) { typeParams.put(DynamicParameterizedType.PARAMETER_TYPE, new ColumnParameterType(column, Map.class)); return new TypeDescriptor("com.marvinformatics.hibernate.json.JsonUserType", typeParams); } else if(column.getActualJavaType() == List.class) { typeParams.put(DynamicParameterizedType.PARAMETER_TYPE, new ColumnParameterType(column, Object.class)); return new TypeDescriptor("com.marvinformatics.hibernate.json.JsonListUserType", typeParams); } else { logger.warn("Unsupported column data type: " + column.getActualJavaType()); } } return super.getDatabaseSpecificType(column); }
@SuppressWarnings("unchecked") @Override public void setParameterValues(Properties parameters) { if (parameters == null) { throw new MappingException("No class or return type defined for type: " + JsonBlobType.class.getName()); } DynamicParameterizedType.ParameterType reader = (DynamicParameterizedType.ParameterType) parameters.get(PARAMETER_TYPE); if (reader != null) { setJavaTypeDescriptor(new JsonTypeDescriptor<T>(reader.getReturnedClass())); } else { String className = parameters.getProperty(RETURNED_CLASS); if (className == null) { throw new MappingException("No class name defined for type: " + JsonBlobType.class.getName()); } try { setJavaTypeDescriptor(new JsonTypeDescriptor<T>(ReflectHelper.classForName(className))); } catch (ClassNotFoundException e) { throw new MappingException("Unable to load class from " + RETURNED_CLASS + " parameter", e); } } }
public Type getType() throws MappingException { if ( type != null ) { return type; } if ( typeName == null ) { throw new MappingException( "No type name" ); } if ( typeParameters != null && Boolean.valueOf( typeParameters.getProperty( DynamicParameterizedType.IS_DYNAMIC ) ) && typeParameters.get( DynamicParameterizedType.PARAMETER_TYPE ) == null ) { createParameterImpl(); } Type result = mappings.getTypeResolver().heuristicType( typeName, typeParameters ); if ( result == null ) { String msg = "Could not determine type for: " + typeName; if ( table != null ) { msg += ", at table: " + table.getName(); } if ( columns != null && columns.size() > 0 ) { msg += ", for columns: " + columns; } throw new MappingException( msg ); } return result; }
private void createParameterImpl() { try { String[] columnsNames = new String[columns.size()]; for ( int i = 0; i < columns.size(); i++ ) { Selectable column = columns.get(i); if (column instanceof Column){ columnsNames[i] = ((Column) column).getName(); } } final XProperty xProperty = (XProperty) typeParameters.get( DynamicParameterizedType.XPROPERTY ); // todo : not sure this works for handling @MapKeyEnumerated final Annotation[] annotations = xProperty == null ? null : xProperty.getAnnotations(); typeParameters.put( DynamicParameterizedType.PARAMETER_TYPE, new ParameterTypeImpl( ReflectHelper.classForName( typeParameters.getProperty( DynamicParameterizedType.RETURNED_CLASS ) ), annotations, table.getCatalog(), table.getSchema(), table.getName(), Boolean.valueOf( typeParameters.getProperty( DynamicParameterizedType.IS_PRIMARY_KEY ) ), columnsNames ) ); } catch ( ClassNotFoundException cnfe ) { throw new MappingException( "Could not create DynamicParameterizedType for type: " + typeName, cnfe ); } }
@SuppressWarnings("unchecked") public void setParameterValues(Properties parameters) { try { Class eClass = ReflectHelper.classForName(parameters.getProperty(DynamicParameterizedType.ENTITY)); Field field = ReflectionUtils.findField(eClass, parameters.getProperty(DynamicParameterizedType.PROPERTY)); Type fieldType = field.getGenericType(); if (fieldType instanceof Class || fieldType instanceof ParameterizedType) { type = fieldType; } parseSqlType(field.getAnnotations()); return; } catch (Exception e) { LOG.error(e.getMessage()); } final DynamicParameterizedType.ParameterType reader = (DynamicParameterizedType.ParameterType) parameters.get( DynamicParameterizedType.PARAMETER_TYPE); if (reader != null) { type = reader.getReturnedClass(); parseSqlType(reader.getAnnotationsMethod()); } else { try { type = ReflectHelper.classForName((String) parameters.get(CLASS_NAME)); } catch (ClassNotFoundException exception) { throw new HibernateException("class not found", exception); } } }
public static Class<?> entityClass(Properties properties) { try { return Class.forName(properties.getProperty(DynamicParameterizedType.ENTITY)); } catch (ClassNotFoundException ex) { throw new IllegalStateException(String.format("Entity class not found: %s", properties.getProperty(DynamicParameterizedType.ENTITY))); } }
public static Field mappedField(Class<?> baseClass, Properties properties) { final String fieldName = properties.getProperty(DynamicParameterizedType.PROPERTY); for (Class<?> cls = baseClass; cls != null; cls = cls.getSuperclass()) { for (Field f : cls.getDeclaredFields()) { if (fieldName.equals(f.getName())) { return f; } } } throw new IllegalStateException(String.format("Entity field not found: %s::%s", baseClass, fieldName)); }