use of javax.persistence.EnumType in project hibernate-orm by hibernate.
the class JPAOverriddenAnnotationReader method getMapKeyEnumerated.
/**
* Adds a @MapKeyEnumerated annotation to the specified annotationList if the specified element
* contains a map-key-enumerated sub-element. This should only be the case for
* element-collection, many-to-many, or one-to-many associations.
*/
private void getMapKeyEnumerated(List<Annotation> annotationList, Element element) {
Element subelement = element != null ? element.element("map-key-enumerated") : null;
if (subelement != null) {
AnnotationDescriptor ad = new AnnotationDescriptor(MapKeyEnumerated.class);
EnumType value = EnumType.valueOf(subelement.getTextTrim());
ad.setValue("value", value);
annotationList.add(AnnotationFactory.create(ad));
}
}
use of javax.persistence.EnumType in project hibernate-orm by hibernate.
the class EnumeratedSmokeTest method validateEnumMapping.
private void validateEnumMapping(Property property, EnumType expectedJpaEnumType) {
assertThat(property.getType(), instanceOf(CustomType.class));
final CustomType customType = (CustomType) property.getType();
assertThat(customType.getUserType(), instanceOf(org.hibernate.type.EnumType.class));
final org.hibernate.type.EnumType hibernateMappingEnumType = (org.hibernate.type.EnumType) customType.getUserType();
assertThat(hibernateMappingEnumType.isOrdinal(), is(expectedJpaEnumType == EnumType.ORDINAL));
assertThat(hibernateMappingEnumType.sqlTypes().length, is(1));
assertThat(hibernateMappingEnumType.sqlTypes()[0], is(expectedJpaEnumType == EnumType.ORDINAL ? Types.INTEGER : Types.VARCHAR));
}
use of javax.persistence.EnumType in project CloudStack-archive by CloudStack-extras.
the class GenericDaoBase method setField.
@DB(txn = false)
protected void setField(Object entity, Field field, ResultSet rs, int index) throws SQLException {
try {
final Class<?> type = field.getType();
if (type == String.class) {
byte[] bytes = rs.getBytes(index);
if (bytes != null) {
try {
if (field.getAnnotation(Column.class).encryptable()) {
field.set(entity, DBEncryptionUtil.decrypt(new String(bytes, "UTF-8")));
} else {
field.set(entity, new String(bytes, "UTF-8"));
}
} catch (IllegalArgumentException e) {
assert (false);
throw new CloudRuntimeException("IllegalArgumentException when converting UTF-8 data");
} catch (UnsupportedEncodingException e) {
assert (false);
throw new CloudRuntimeException("UnsupportedEncodingException when converting UTF-8 data");
}
} else {
field.set(entity, null);
}
} else if (type == long.class) {
field.setLong(entity, rs.getLong(index));
} else if (type == Long.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getLong(index));
}
} else if (type.isEnum()) {
final Enumerated enumerated = field.getAnnotation(Enumerated.class);
final EnumType enumType = (enumerated == null) ? EnumType.STRING : enumerated.value();
final Enum<?>[] enums = (Enum<?>[]) field.getType().getEnumConstants();
for (final Enum<?> e : enums) {
if ((enumType == EnumType.STRING && e.name().equalsIgnoreCase(rs.getString(index))) || (enumType == EnumType.ORDINAL && e.ordinal() == rs.getInt(index))) {
field.set(entity, e);
return;
}
}
} else if (type == int.class) {
field.set(entity, rs.getInt(index));
} else if (type == Integer.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getInt(index));
}
} else if (type == Date.class) {
final Object data = rs.getDate(index);
if (data == null) {
field.set(entity, null);
return;
}
field.set(entity, DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index)));
} else if (type == Calendar.class) {
final Object data = rs.getDate(index);
if (data == null) {
field.set(entity, null);
return;
}
final Calendar cal = Calendar.getInstance();
cal.setTime(DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index)));
field.set(entity, cal);
} else if (type == boolean.class) {
field.setBoolean(entity, rs.getBoolean(index));
} else if (type == Boolean.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getBoolean(index));
}
} else if (type == URI.class) {
try {
String str = rs.getString(index);
field.set(entity, str == null ? null : new URI(str));
} catch (URISyntaxException e) {
throw new CloudRuntimeException("Invalid URI: " + rs.getString(index), e);
}
} else if (type == URL.class) {
try {
String str = rs.getString(index);
field.set(entity, str != null ? new URL(str) : null);
} catch (MalformedURLException e) {
throw new CloudRuntimeException("Invalid URL: " + rs.getString(index), e);
}
} else if (type == Ip.class) {
final Enumerated enumerated = field.getAnnotation(Enumerated.class);
final EnumType enumType = (enumerated == null) ? EnumType.STRING : enumerated.value();
Ip ip = null;
if (enumType == EnumType.STRING) {
String s = rs.getString(index);
ip = s == null ? null : new Ip(NetUtils.ip2Long(s));
} else {
ip = new Ip(rs.getLong(index));
}
field.set(entity, ip);
} else if (type == short.class) {
field.setShort(entity, rs.getShort(index));
} else if (type == Short.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getShort(index));
}
} else if (type == float.class) {
field.setFloat(entity, rs.getFloat(index));
} else if (type == Float.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getFloat(index));
}
} else if (type == double.class) {
field.setDouble(entity, rs.getDouble(index));
} else if (type == Double.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getDouble(index));
}
} else if (type == byte.class) {
field.setByte(entity, rs.getByte(index));
} else if (type == Byte.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getByte(index));
}
} else if (type == byte[].class) {
field.set(entity, rs.getBytes(index));
} else {
field.set(entity, rs.getObject(index));
}
} catch (final IllegalAccessException e) {
throw new CloudRuntimeException("Yikes! ", e);
}
}
use of javax.persistence.EnumType in project cloudstack by apache.
the class GenericDaoBase method setField.
@DB()
protected void setField(Object entity, Field field, ResultSet rs, int index) throws SQLException {
try {
final Class<?> type = field.getType();
if (type == String.class) {
byte[] bytes = rs.getBytes(index);
if (bytes != null) {
try {
Encrypt encrypt = field.getAnnotation(Encrypt.class);
if (encrypt != null && encrypt.encrypt()) {
field.set(entity, DBEncryptionUtil.decrypt(new String(bytes, "UTF-8")));
} else {
field.set(entity, new String(bytes, "UTF-8"));
}
} catch (IllegalArgumentException e) {
assert (false);
throw new CloudRuntimeException("IllegalArgumentException when converting UTF-8 data");
} catch (UnsupportedEncodingException e) {
assert (false);
throw new CloudRuntimeException("UnsupportedEncodingException when converting UTF-8 data");
}
} else {
field.set(entity, null);
}
} else if (type == long.class) {
field.setLong(entity, rs.getLong(index));
} else if (type == Long.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getLong(index));
}
} else if (type.isEnum()) {
final Enumerated enumerated = field.getAnnotation(Enumerated.class);
final EnumType enumType = (enumerated == null) ? EnumType.STRING : enumerated.value();
final Enum<?>[] enums = (Enum<?>[]) field.getType().getEnumConstants();
for (final Enum<?> e : enums) {
if ((enumType == EnumType.STRING && e.name().equalsIgnoreCase(rs.getString(index))) || (enumType == EnumType.ORDINAL && e.ordinal() == rs.getInt(index))) {
field.set(entity, e);
return;
}
}
} else if (type == int.class) {
field.set(entity, rs.getInt(index));
} else if (type == Integer.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getInt(index));
}
} else if (type == Date.class) {
final Object data = rs.getDate(index);
if (data == null) {
field.set(entity, null);
return;
}
field.set(entity, DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index)));
} else if (type == Calendar.class) {
final Object data = rs.getDate(index);
if (data == null) {
field.set(entity, null);
return;
}
final Calendar cal = Calendar.getInstance();
cal.setTime(DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index)));
field.set(entity, cal);
} else if (type == boolean.class) {
field.setBoolean(entity, rs.getBoolean(index));
} else if (type == Boolean.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getBoolean(index));
}
} else if (type == URI.class) {
try {
String str = rs.getString(index);
field.set(entity, str == null ? null : new URI(str));
} catch (URISyntaxException e) {
throw new CloudRuntimeException("Invalid URI: " + rs.getString(index), e);
}
} else if (type == URL.class) {
try {
String str = rs.getString(index);
field.set(entity, str != null ? new URL(str) : null);
} catch (MalformedURLException e) {
throw new CloudRuntimeException("Invalid URL: " + rs.getString(index), e);
}
} else if (type == Ip.class) {
final Enumerated enumerated = field.getAnnotation(Enumerated.class);
final EnumType enumType = (enumerated == null) ? EnumType.STRING : enumerated.value();
Ip ip = null;
if (enumType == EnumType.STRING) {
String s = rs.getString(index);
ip = s == null ? null : new Ip(NetUtils.ip2Long(s));
} else {
ip = new Ip(rs.getLong(index));
}
field.set(entity, ip);
} else if (type == short.class) {
field.setShort(entity, rs.getShort(index));
} else if (type == Short.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getShort(index));
}
} else if (type == float.class) {
field.setFloat(entity, rs.getFloat(index));
} else if (type == Float.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getFloat(index));
}
} else if (type == double.class) {
field.setDouble(entity, rs.getDouble(index));
} else if (type == Double.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getDouble(index));
}
} else if (type == byte.class) {
field.setByte(entity, rs.getByte(index));
} else if (type == Byte.class) {
if (rs.getObject(index) == null) {
field.set(entity, null);
} else {
field.set(entity, rs.getByte(index));
}
} else if (type == byte[].class) {
field.set(entity, rs.getBytes(index));
} else {
field.set(entity, rs.getObject(index));
}
} catch (final IllegalAccessException e) {
throw new CloudRuntimeException("Yikes! ", e);
}
}
use of javax.persistence.EnumType in project simplejpa by appoxy.
the class ObjectBuilder method getEnumValue.
static Object getEnumValue(PersistentProperty field, String val) {
EnumType enumType = field.getEnumType();
Class<? extends Enum> retType = (Class<? extends Enum>) field.getPropertyClass();
Object enumVal = null;
if (enumType == EnumType.STRING) {
enumVal = Enum.valueOf(retType, val);
} else {
// ordinal
enumVal = retType.getEnumConstants()[Integer.parseInt(val)];
}
return enumVal;
}
Aggregations