下面列出了怎么用javax.persistence.Enumerated的API类实例代码及写法,或者点击链接到github查看源代码。
public static void checkEnum(List<Class<?>> classes) throws Exception {
for (Class<?> cls : classes) {
List<Field> fields = FieldUtils.getFieldsListWithAnnotation(cls, FieldDescribe.class);
for (Field field : fields) {
if (field.getType().isEnum()) {
Enumerated enumerated = field.getAnnotation(Enumerated.class);
Column column = field.getAnnotation(Column.class);
if (null == enumerated || (!Objects.equals(EnumType.STRING, enumerated.value())) || (null == column)
|| column.length() > 200) {
System.err.println(String.format("checkEnum error: class: %s, field: %s.", cls.getName(),
field.getName()));
}
}
}
}
}
private void getEnumerated(List<Annotation> annotationList, Element element) {
Element subElement = element != null ? element.element( "enumerated" ) : null;
if ( subElement != null ) {
AnnotationDescriptor ad = new AnnotationDescriptor( Enumerated.class );
String enumerated = subElement.getTextTrim();
if ( "ORDINAL".equalsIgnoreCase( enumerated ) ) {
ad.setValue( "value", EnumType.ORDINAL );
}
else if ( "STRING".equalsIgnoreCase( enumerated ) ) {
ad.setValue( "value", EnumType.STRING );
}
else if ( StringHelper.isNotEmpty( enumerated ) ) {
throw new AnnotationException( "Unknown EnumType: " + enumerated + ". " + SCHEMA_VALIDATION );
}
annotationList.add( AnnotationFactory.create( ad ) );
}
}
@Test
public void testIfThereAreEnumeratedAnnotations() {
Reflections reflections = new Reflections("com.sequenceiq",
new FieldAnnotationsScanner());
Map<String, Set<String>> enumeratedFields = new HashMap<>();
reflections.getFieldsAnnotatedWith(Enumerated.class).forEach(field -> {
try {
String className = field.getDeclaringClass().getName();
enumeratedFields.computeIfAbsent(className, key -> new HashSet<>()).add(field.toString());
} catch (RuntimeException e) {
// ignore if cannot check fields
}
});
Set<String> fields = new HashSet<>();
enumeratedFields.forEach((key, value) -> {
fields.add(key + ": " + String.join(", ", value));
});
Assert.assertTrue(
String.format("Classes with @Enumerated fields: %s%s%s%s", lineSeparator(),
String.join(lineSeparator(), fields), lineSeparator(), "Use @Converter instead of @Enumerated"), enumeratedFields.isEmpty());
}
@ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)
@CollectionTable(name = "USER_ROLES", joinColumns = @JoinColumn(name = "USER_ID"))
@Enumerated(EnumType.STRING)
@Column(name = "role", nullable = false)
public Set<Role> getRoles() {
return roles;
}
/**
* Gets the org code.
*
* @return the org code
*/
@Basic
@Column(name = "ORG_CODE")
@Enumerated(EnumType.STRING)
public OrgCode getOrgCode() {
return orgCode;
}
/**
* Gets the role code.
*
* @return the role code
*/
@Basic
@Column(name = "ROLE_CODE")
@Enumerated(EnumType.STRING)
public RoleStatus getRoleCode() {
return roleCode;
}
/**
* Gets the status.
*
* @return the status
*/
@Basic
@Column(name = "STATUS")
@Enumerated(EnumType.STRING)
public RoleStatus getStatus() {
return status;
}
/**
* Gets the gender.
*
* @return the gender
*/
@Basic
@Column(name = "GENDER")
@Enumerated(EnumType.STRING)
public SexType getGender() {
return gender;
}
/**
* Gets the status.
*
* @return the status
*/
@Basic
@Column(name = "STATUS")
@Enumerated(EnumType.STRING)
public RoleStatus getStatus() {
return status;
}
/**
* Gets the vote.
*
* @return the vote
*/
@Basic
@Column(name = "VOTE")
@Enumerated(EnumType.STRING)
public VoteDecision getVote() {
return vote;
}
/**
* Gets the ballot type.
*
* @return the ballot type
*/
@Basic
@Column(name = "BALLOT_TYPE")
@Enumerated(EnumType.STRING)
public BallotType getBallotType() {
return ballotType;
}
/**
* Gets the vote.
*
* @return the vote
*/
@Basic
@Column(name = "VOTE")
@Enumerated(EnumType.STRING)
public VoteDecision getVote() {
return vote;
}
/**
* Gets the ballot type.
*
* @return the ballot type
*/
@Basic
@Column(name = "BALLOT_TYPE")
@Enumerated(EnumType.STRING)
public BallotType getBallotType() {
return ballotType;
}
@ElementCollection(targetClass = Permission.class, fetch = FetchType.EAGER)
@CollectionTable(name = "USER_PERMISSIONS", joinColumns = @JoinColumn(name = "USER_ID"))
@Enumerated(EnumType.STRING)
@Column(name = "permission", nullable = false)
public Set<Permission> getPermissions() {
return permissions;
}
/**
* Gets the value.
*
* @return the value
*/
@Basic
@Column(name = "VALUE_")
@Enumerated(EnumType.STRING)
public RegionCategory getValue() {
return value;
}
/**
* Gets the value.
*
* @return the value
*/
@Basic
@Column(name = "VALUE_")
@Enumerated(EnumType.STRING)
public AdminRegionCategory getValue() {
return value;
}
/**
* Gets the value.
*
* @return the value
*/
@Basic
@Column(name = "VALUE_")
@Enumerated(EnumType.STRING)
public LendingTypeCategory getValue() {
return value;
}
/**
* Gets the gender.
*
* @return the gender
*/
@Basic
@Column(name = "GENDER")
@Enumerated(EnumType.STRING)
public SexType getGender() {
return gender;
}
/**
* Gets the reference type.
*
* @return the reference type
*/
@Basic
@Column(name = "REFERENCE_TYPE")
@Enumerated(EnumType.STRING)
public ReferenceType getReferenceType() {
return referenceType;
}
/**
* Gets the role description.
*
* @return the role description
*/
@Basic
@Column(name = "ROLE_DESCRIPTION")
@Enumerated(EnumType.STRING)
public DocumentPersonReferenceRoleType getRoleDescription() {
return roleDescription;
}
@Enumerated(EnumType.STRING)
public ProductAvailabilityStatus getAvailabilityStatus() {
if (availabilityStatus == null) {
availabilityStatus = ProductAvailabilityStatus.ON_SALE;
}
return availabilityStatus;
}
/**
* Gets the state.
*
* @return state
*/
@Enumerated(EnumType.STRING)
@Column
public State getState() {
return state;
}
/**
* Gets the status.
*
* @return status
*/
@Column
@Enumerated(EnumType.STRING)
public Status getStatus() {
return status;
}
/**
* Gets the type.
*
* @return type
*/
@Column(name = "matcher")
@Enumerated(EnumType.STRING)
public Matcher getMatcher() {
return matcher;
}
/**
* Get the type of this notification.
* @return notification type
*/
@Enumerated(EnumType.STRING)
@Column(columnDefinition = "type")
public NotificationType getType() {
return type;
}
@Id
@Enumerated(EnumType.STRING)
public com.amazon.photosharing.enums.Role getRole() {return this._role;}
public void prepare(XProperty collectionProperty) {
// fugly
if ( prepared ) {
return;
}
if ( collectionProperty == null ) {
return;
}
prepared = true;
if ( collection.isMap() ) {
if ( collectionProperty.isAnnotationPresent( MapKeyEnumerated.class ) ) {
canKeyBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( MapKeyTemporal.class ) ) {
canKeyBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( MapKeyClass.class ) ) {
canKeyBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( MapKeyType.class ) ) {
canKeyBeConverted = false;
}
}
else {
canKeyBeConverted = false;
}
if ( collectionProperty.isAnnotationPresent( ManyToAny.class ) ) {
canElementBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( OneToMany.class ) ) {
canElementBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( ManyToMany.class ) ) {
canElementBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( Enumerated.class ) ) {
canElementBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( Temporal.class ) ) {
canElementBeConverted = false;
}
else if ( collectionProperty.isAnnotationPresent( CollectionType.class ) ) {
canElementBeConverted = false;
}
// Is it valid to reference a collection attribute in a @Convert attached to the owner (entity) by path?
// if so we should pass in 'clazzToProcess' also
if ( canKeyBeConverted || canElementBeConverted ) {
buildAttributeConversionInfoMaps( collectionProperty, elementAttributeConversionInfoMap, keyAttributeConversionInfoMap );
}
}
private void applyAttributeConverter(XProperty property, ConverterDescriptor attributeConverterDescriptor) {
if ( attributeConverterDescriptor == null ) {
return;
}
LOG.debugf( "Starting applyAttributeConverter [%s:%s]", persistentClassName, property.getName() );
if ( property.isAnnotationPresent( Id.class ) ) {
LOG.debugf( "Skipping AttributeConverter checks for Id attribute [%s]", property.getName() );
return;
}
if ( isVersion ) {
LOG.debugf( "Skipping AttributeConverter checks for version attribute [%s]", property.getName() );
return;
}
if ( !key && property.isAnnotationPresent( Temporal.class ) ) {
LOG.debugf( "Skipping AttributeConverter checks for Temporal attribute [%s]", property.getName() );
return;
}
if ( key && property.isAnnotationPresent( MapKeyTemporal.class ) ) {
LOG.debugf( "Skipping AttributeConverter checks for map-key annotated as MapKeyTemporal [%s]", property.getName() );
return;
}
if ( !key && property.isAnnotationPresent( Enumerated.class ) ) {
LOG.debugf( "Skipping AttributeConverter checks for Enumerated attribute [%s]", property.getName() );
return;
}
if ( key && property.isAnnotationPresent( MapKeyEnumerated.class ) ) {
LOG.debugf( "Skipping AttributeConverter checks for map-key annotated as MapKeyEnumerated [%s]", property.getName() );
return;
}
if ( isAssociation() ) {
LOG.debugf( "Skipping AttributeConverter checks for association attribute [%s]", property.getName() );
return;
}
this.attributeConverterDescriptor = attributeConverterDescriptor;
}
public static String resolveJdbcAlias(Field field) {
if (field.isAnnotationPresent(MappedJdbcType.class)) {
MappedJdbcType jdbcType = field.getAnnotation(MappedJdbcType.class);
return jdbcType.value().name();
}
if (field.isAnnotationPresent(CodeEnum.class)) {
CodeEnum codeEnum = field.getAnnotation(CodeEnum.class);
if (Objects.equals(codeEnum.value(), CodeType.INT)) {
return JdbcType.INTEGER.name();
}
if (Objects.equals(codeEnum.value(), CodeType.STRING)) {
return JdbcType.VARCHAR.name();
}
}
if (field.getType().isEnum()) {
if (field.isAnnotationPresent(Enumerated.class)) {
Enumerated enumerated = field.getAnnotation(Enumerated.class);
if (Objects.equals(enumerated.value(), EnumType.ORDINAL)) {
return "INTEGER";
}
}
return "VARCHAR";
}
Class<?> fieldType = field.getType();
if (Integer.class.equals(fieldType)) {
return "INTEGER";
}
if (Double.class.equals(fieldType)) {
return "DOUBLE";
}
if (Float.class.equals(fieldType)) {
return "FLOAT";
}
if (String.class.equals(fieldType)) {
return "VARCHAR";
}
if (java.util.Date.class.isAssignableFrom(fieldType)) {
return "TIMESTAMP";
}
return null;
}
@Enumerated(EnumType.STRING)
@Column(name = "tipo", nullable = false)
public TipoEnum getTipo() {
return tipo;
}