Example usage for javax.persistence EnumType ORDINAL

List of usage examples for javax.persistence EnumType ORDINAL

Introduction

In this page you can find the example usage for javax.persistence EnumType ORDINAL.

Prototype

EnumType ORDINAL

To view the source code for javax.persistence EnumType ORDINAL.

Click Source Link

Document

Persist enumerated type property or field as an integer.

Usage

From source file:org.apache.openjpa.persistence.jdbc.AnnotationPersistenceMappingSerializer.java

@Override
protected void serializeFieldMappingContent(FieldMetaData fmd, PersistenceStrategy strategy,
        AnnotationBuilder ab) {/*from w w w . j  a  va 2 s .co  m*/
    if (fmd.getMappedBy() != null)
        return;

    // while I'd like to do auto detection based on join directions, etc.
    // the distinguished column / table / etc names forces our hand
    // esp for OpenJPA custom mappings.
    FieldMapping field = (FieldMapping) fmd;
    switch (strategy) {
    case ONE_ONE:
    case MANY_ONE:
        serializeColumns(field.getValueInfo(), ColType.JOIN, field.getMappingInfo().getTableName(), null, fmd);
        return;
    case ONE_MANY:
        if (field.getMappingInfo().getJoinDirection() == MappingInfo.JOIN_NONE) {
            serializeColumns(field.getElementMapping().getValueInfo(), ColType.JOIN, null, null, fmd);
            return;
        }
        // else no break
    case MANY_MANY:
        if (field.getMappingInfo().hasSchemaComponents()
                || field.getElementMapping().getValueInfo().hasSchemaComponents()) {
            AnnotationBuilder abJoinTbl = addAnnotation(JoinTable.class, fmd);
            String table = field.getMappingInfo().getTableName();
            if (table != null) {
                int index = table.indexOf('.');
                if (index < 0)
                    abJoinTbl.add("name", table);
                else {
                    abJoinTbl.add("schema", table.substring(0, index));
                    abJoinTbl.add("name", table.substring(index + 1));
                }
            }
            serializeColumns(field.getMappingInfo(), ColType.JOIN, null, abJoinTbl, null);
            serializeColumns(field.getElementMapping().getValueInfo(), ColType.INVERSE, null, abJoinTbl, null);
        }
        return;
    }

    serializeColumns(field.getValueInfo(), ColType.COL, field.getMappingInfo().getTableName(), null, fmd);
    if (strategy == PersistenceStrategy.BASIC && isLob(field)) {
        addAnnotation(Lob.class, fmd);
    }
    TemporalType temporal = getTemporal(field);
    if (temporal != null) {
        addAnnotation(Temporal.class, fmd).add(null, temporal);
    }

    EnumType enumType = getEnumType(field);
    if (enumType != null && enumType != EnumType.ORDINAL) {
        addAnnotation(Enumerated.class, fmd).add(null, enumType);
    }
}

From source file:org.apache.openjpa.persistence.jdbc.AnnotationPersistenceMappingSerializer.java

/**
 * Return enum type for the field./*w w w  . j  a v a 2  s  .  c o m*/
 */
protected EnumType getEnumType(FieldMapping field) {
    if (field.getDeclaredTypeCode() != JavaTypes.OBJECT)
        return null;
    if (!(field.getHandler() instanceof EnumValueHandler))
        return null;
    return ((EnumValueHandler) field.getHandler()).getStoreOrdinal() ? EnumType.ORDINAL : EnumType.STRING;
}

From source file:org.apache.openjpa.persistence.jdbc.XMLPersistenceMappingParser.java

/**
 * Parse enumerated.//from ww  w  .  j  a va  2s . c  o  m
 */
private void endEnumerated() {
    String text = currentText();
    if (StringUtils.isEmpty(text))
        return;
    EnumType type = Enum.valueOf(EnumType.class, text);

    FieldMapping fm = (FieldMapping) currentElement();
    String strat = EnumValueHandler.class.getName() + "(StoreOrdinal="
            + String.valueOf(type == EnumType.ORDINAL) + ")";
    if (fm.isElementCollection())
        fm.getElementMapping().getValueInfo().setStrategy(strat);
    else
        fm.getValueInfo().setStrategy(strat);
}

From source file:org.apache.openjpa.persistence.jdbc.XMLPersistenceMappingParser.java

/**
 * Parse map-key-enumerated.//  ww  w  .j a  v a 2s.  co  m
 */
private void endMapKeyEnumerated() {
    String text = currentText();
    if (StringUtils.isEmpty(text))
        return;
    EnumType type = Enum.valueOf(EnumType.class, text);

    FieldMapping fm = (FieldMapping) currentElement();
    String strat = EnumValueHandler.class.getName() + "(StoreOrdinal="
            + String.valueOf(type == EnumType.ORDINAL) + ")";
    fm.getKeyMapping().getValueInfo().setStrategy(strat);
}

From source file:org.apache.openjpa.persistence.jdbc.XMLPersistenceMappingSerializer.java

@Override
protected void serializeFieldMappingContent(FieldMetaData fmd, PersistenceStrategy strategy)
        throws SAXException {
    if (fmd.getMappedBy() != null)
        return;/*from   ww w.  j  a v  a 2s  .  c  o m*/

    // while I'd like to do auto detection based on join directions, etc.
    // the distinguished column / table / etc names forces our hand
    // esp for OpenJPA custom mappings.
    FieldMapping field = (FieldMapping) fmd;
    switch (strategy) {
    case ONE_ONE:
    case MANY_ONE:
        serializeColumns(field.getValueInfo(), ColType.JOIN, field.getMappingInfo().getTableName());
        return;
    case ONE_MANY:
        if (field.getMappingInfo().getJoinDirection() == MappingInfo.JOIN_NONE) {
            serializeColumns(field.getElementMapping().getValueInfo(), ColType.JOIN, null);
            return;
        }
        // else no break
    case MANY_MANY:
        if (field.getMappingInfo().hasSchemaComponents()
                || field.getElementMapping().getValueInfo().hasSchemaComponents()) {
            String table = field.getMappingInfo().getTableName();
            if (table != null) {
                int index = table.indexOf('.');
                if (index < 0)
                    addAttribute("name", table);
                else {
                    addAttribute("schema", table.substring(0, index));
                    addAttribute("name", table.substring(index + 1));
                }
            }
            startElement("join-table");
            serializeColumns(field.getMappingInfo(), ColType.JOIN, null);
            serializeColumns(field.getElementMapping().getValueInfo(), ColType.INVERSE, null);
            endElement("join-table");
        }
        return;
    case ELEM_COLL:
        if (field.getMappingInfo().hasSchemaComponents()
                || field.getElementMapping().getValueInfo().hasSchemaComponents()) {
            String table = field.getMappingInfo().getTableName();
            if (table != null) {
                int index = table.indexOf('.');
                if (index < 0)
                    addAttribute("name", table);
                else {
                    addAttribute("schema", table.substring(0, index));
                    addAttribute("name", table.substring(index + 1));
                }
            }
            startElement("collection-table");
            ValueMappingImpl elem = (ValueMappingImpl) field.getElement();
            serializeColumns(elem.getValueInfo(), ColType.COL, null);
            endElement("collection-table");
        }
        return;
    }

    serializeColumns(field.getValueInfo(), ColType.COL, field.getMappingInfo().getTableName());
    if (strategy == PersistenceStrategy.BASIC && isLob(field)) {
        startElement("lob");
        endElement("lob");
    }
    TemporalType temporal = getTemporal(field);
    if (temporal != null) {
        startElement("temporal");
        addText(temporal.toString());
        endElement("temporal");
    }

    EnumType enumType = getEnumType(field);
    if (enumType != null && enumType != EnumType.ORDINAL) {
        startElement("enumerated");
        addText(enumType.toString());
        endElement("enumerated");
    }
}

From source file:org.batoo.jpa.core.impl.jdbc.BasicColumn.java

/**
 * @param jdbcAdaptor/*w w  w. ja  v a  2 s.  co  m*/
 *            the jdbc adaptor
 * @param mapping
 *            the mapping
 * @param sqlType
 *            the SQL type
 * @param metadata
 *            the metadata
 * 
 * @since $version
 * @author hceylan
 */
@SuppressWarnings("unchecked")
public BasicColumn(JdbcAdaptor jdbcAdaptor, BasicMapping<?, ?> mapping, int sqlType, ColumnMetadata metadata) {
    super();

    this.jdbcAdaptor = jdbcAdaptor;
    this.mapping = mapping;
    this.locator = metadata != null ? metadata.getLocator() : null;
    this.sqlType = sqlType;

    this.mappingName = (metadata != null) && StringUtils.isNotBlank(metadata.getName()) ? metadata.getName()
            : this.mapping.getAttribute().getName();
    this.name = this.jdbcAdaptor.escape(this.mappingName);

    this.tableName = metadata != null ? metadata.getTable() : "";
    this.columnDefinition = metadata != null ? metadata.getColumnDefinition() : "";
    this.length = metadata != null ? metadata.getLength() : 255;
    this.precision = metadata != null ? metadata.getPrecision() : 0;
    this.scale = metadata != null ? metadata.getScale() : 0;
    this.insertable = metadata != null ? metadata.isInsertable() : true;
    this.nullable = metadata != null ? metadata.isNullable() : true;
    this.unique = metadata != null ? metadata.isUnique() : false;
    this.updatable = metadata != null ? metadata.isUpdatable() : true;

    this.numberType = Number.class.isAssignableFrom(mapping.getAttribute().getJavaType())
            ? mapping.getAttribute().getJavaType()
            : null;
    this.temporalType = mapping.getAttribute().getTemporalType();
    this.enumType = mapping.getAttribute().getEnumType();
    if (this.enumType != null) {
        Class<Enum<?>> enumJavaType;
        enumJavaType = (Class<Enum<?>>) mapping.getAttribute().getJavaType();
        try {
            if (this.enumType == EnumType.ORDINAL) {
                this.values = (Enum<?>[]) enumJavaType.getMethod("values").invoke(null);
                this.method = null;
            } else {
                this.values = null;
                this.method = enumJavaType.getMethod("valueOf", String.class);
            }
        } catch (final Exception e) {
            throw new MappingException("Unable to map enum type", this.mapping.getAttribute().getLocator());
        }
    } else {
        this.values = null;
        this.method = null;
    }
}

From source file:org.batoo.jpa.core.impl.jdbc.BasicColumn.java

/**
 * Converts the value corresponding to enum or temporal type
 * // w w w.j  a v a  2s .c om
 * @param value
 *            the raw value
 * @return the converted value
 * 
 * @since $version
 * @author hceylan
 */
public Object convertValue(final Object value) {
    if (value == null) {
        return null;
    }

    if (this.temporalType != null) {
        switch (this.temporalType) {
        case DATE:
            if (value instanceof java.sql.Date) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Date(((Date) value).getTime());
            }

            return new java.sql.Date(((Calendar) value).getTimeInMillis());
        case TIME:
            if (value instanceof java.sql.Time) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Time(((Date) value).getTime());
            }

            return new java.sql.Time(((Calendar) value).getTimeInMillis());
        case TIMESTAMP:
            if (value instanceof java.sql.Timestamp) {
                return value;
            }

            if (value instanceof Date) {
                return new java.sql.Timestamp(((Date) value).getTime());
            }

            return new java.sql.Timestamp(((Calendar) value).getTimeInMillis());
        }
    }

    if (this.numberType != null) {
        return ReflectHelper.convertNumber((Number) value, this.numberType);
    }

    if (this.enumType == null) {
        return value;
    }

    final Enum<?> enumValue = (Enum<?>) value;
    if (this.enumType == EnumType.ORDINAL) {
        return enumValue.ordinal();
    }

    return enumValue.name();
}

From source file:org.batoo.jpa.core.impl.jdbc.BasicColumn.java

/**
 * Sets the value for the instance/*from   w w w . ja  v a  2  s.c  o m*/
 * 
 * @param instance
 *            the instance of which to set value
 * @param value
 *            the value to set
 * 
 * @since $version
 * @author hceylan
 */
@Override
public void setValue(Object instance, Object value) {
    if ((value != null) && (this.enumType != null)) {
        if (this.enumType == EnumType.ORDINAL) {
            value = this.values[((Number) value).shortValue()];
        } else {
            try {
                value = this.method.invoke(null, value);
            } catch (final Exception e) {
            }
        }
    }

    this.mapping.set(instance, value);
}

From source file:org.batoo.jpa.core.impl.model.attribute.BasicAttribute.java

/**
 * Constructor for basic attributes./*w  w w .  j a  v  a  2  s.  c  om*/
 * 
 * @param declaringType
 *            the declaring type
 * @param metadata
 *            the metadata
 * 
 * @since 2.0.0
 */
public BasicAttribute(ManagedTypeImpl<X> declaringType, BasicAttributeMetadata metadata) {
    super(declaringType, metadata);

    this.version = false;
    this.idType = null;
    this.generator = null;

    this.type = this.getDeclaringType().getMetamodel().createBasicType(this.getJavaType());
    this.optional = metadata.isOptional();
    this.index = metadata.getIndex();
    this.columnTransformer = metadata.getColumnTransformer();

    if (Date.class.isAssignableFrom(this.getJavaType())
            || Calendar.class.isAssignableFrom(this.getJavaType())) {
        if (metadata.getTemporalType() == null) {
            this.temporalType = TemporalType.TIMESTAMP;
        } else {
            this.temporalType = metadata.getTemporalType();
        }
    } else {
        this.temporalType = null;
    }

    if (this.getJavaType().getSuperclass() == Enum.class) {
        if (metadata.getEnumType() != null) {
            this.enumType = metadata.getEnumType();
        } else {
            this.enumType = EnumType.ORDINAL;
        }
    } else {
        this.enumType = null;
    }

    this.lob = this.inferLobType(metadata.isLob());
}

From source file:org.batoo.jpa.jdbc.AbstractColumn.java

/**
 * @param javaType/*from   w ww.  j a  va 2  s  .  c  o m*/
 *            the java type
 * @param idType
 *            the id type
 * @param temporalType
 *            the temporal type
 * @param enumType
 *            the enum type
 * @param lob
 *            if the column is lob
 * @param locator
 *            the locator
 * 
 * @since 2.0.0
 */
@SuppressWarnings("unchecked")
public AbstractColumn(Class<?> javaType, IdType idType, TemporalType temporalType, EnumType enumType,
        boolean lob, AbstractLocator locator) {
    super();

    this.javaType = javaType;

    this.idType = idType;
    this.temporalType = temporalType;
    this.enumType = enumType;
    this.lob = lob;
    this.locator = locator;

    this.numberType = Number.class.isAssignableFrom(javaType) ? javaType : null;

    if (this.enumType != null) {
        Class<Enum<?>> enumJavaType;
        enumJavaType = (Class<Enum<?>>) javaType;
        try {
            if (this.enumType == EnumType.ORDINAL) {
                this.values = (Enum<?>[]) enumJavaType.getMethod("values").invoke(null);
                this.method = null;
            } else {
                this.values = null;
                this.method = enumJavaType.getMethod("valueOf", String.class);
            }
        } catch (final Exception e) {
            throw new MappingException("Unable to map enum type", this.locator);
        }
    } else {
        this.values = null;
        this.method = null;
    }
}