List of usage examples for javax.persistence AttributeConverter convertToDatabaseColumn
public Y convertToDatabaseColumn(X attribute);
From source file:io.stallion.dataAccess.db.DB.java
/** * Trys to convert java type into what is needed by JDBC to store to the database. * * @param o/*w w w . j ava2 s . co m*/ * @param col * @param arg * @return */ public Object convertColumnArg(Model o, Col col, Object arg) { if (arg == null && col.getDefaultValue() != null) { arg = col.getDefaultValue(); } if (col.getAttributeConverter() != null) { arg = col.getAttributeConverter().convertToDatabaseColumn(arg); } else if (arg != null && !StringUtils.isBlank(col.getConverterClassName())) { AttributeConverter converter = this.getConverter(col.getConverterClassName()); arg = converter.convertToDatabaseColumn(arg); } else if (arg instanceof LocalDateTime) { arg = new Timestamp(((LocalDateTime) arg).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); } else if (arg instanceof ZonedDateTime) { arg = new Timestamp(((ZonedDateTime) arg).toInstant().toEpochMilli()); } else if (arg instanceof Enum) { return arg.toString(); } return arg; }
From source file:org.kuali.rice.krad.data.jpa.Filter.java
/** * Uses the {@link Convert} converter to first translate the {@code attributeValue} to the desired type and then * convert it to the database format for searching. * * @param convert the conversion annotation. * @param attributeName the attribute name of the field to coerce. * @param attributeValue the value to coerce. * @return the coerced value./* www . jav a 2 s.co m*/ */ private static Object coerceConverterValue(Convert convert, String attributeName, String attributeValue) { try { AttributeConverter<Object, String> converter = (AttributeConverter<Object, String>) convert.converter() .newInstance(); Object entityAttribute = converter.convertToEntityAttribute(attributeValue); return converter.convertToDatabaseColumn(entityAttribute); } catch (Exception e) { LOG.error("Could not create the converter for " + attributeName, e); } return attributeValue; }