Add support for JPA enums

This commit is contained in:
Stuart Douglas
2018-09-24 19:40:29 +10:00
parent c55b18439f
commit 9fc0f9c408
5 changed files with 46 additions and 1 deletions

View File

@@ -72,6 +72,9 @@ public class JPATestBootstrapEndpoint extends HttpServlet {
for (Person p : allpersons) {
p.describeFully(sb);
sb.append("\n\t");
if(p.getStatus() != Status.LIVING) {
throw new RuntimeException("Incorrect status " + p);
}
}
sb.append("\nList complete.\n");
System.out.print(sb);
@@ -80,6 +83,7 @@ public class JPATestBootstrapEndpoint extends HttpServlet {
private static void persistNewPerson(EntityManager entityManager) {
Person person = new Person();
person.setName(randomName());
person.setStatus(Status.LIVING);
person.setAddress(new SequencedAddress("Street " + randomName()));
entityManager.persist(person);
}

View File

@@ -14,6 +14,7 @@ public class Person {
private long id;
private String name;
private SequencedAddress address;
private Status status;
public Person() {
}
@@ -50,8 +51,16 @@ public class Person {
this.address = address;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public void describeFully(StringBuilder sb) {
sb.append( "Person with id=" ).append( id ).append( ", name='" ).append( name ).append( "', address { " );
sb.append( "Person with id=" ).append( id ).append( ", name='" ).append( name ).append("', status='").append(status).append( "', address { " );
getAddress().describeFully( sb );
sb.append( " }" );
}

View File

@@ -0,0 +1,6 @@
package org.jboss.shamrock.example.jpa;
public enum Status {
LIVING,
DECEASED
}

View File

@@ -3,6 +3,7 @@ package org.jboss.shamrock.jpa;
import java.util.Objects;
import org.hibernate.tuple.component.PojoComponentTuplizer;
import org.hibernate.type.EnumType;
import org.jboss.shamrock.deployment.ProcessorContext;
/**
@@ -36,6 +37,7 @@ final class HibernateReflectiveNeeds {
simpleConstructor(org.hibernate.id.enhanced.SequenceStyleGenerator.class);
simpleConstructor(org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl.class);
simpleConstructor(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl.class);
simpleConstructor(EnumType.class);
processorContext.addReflectiveClass(true, false, com.arjuna.ats.jta.UserTransaction.class.getName());
processorContext.addReflectiveClass(true, false, com.arjuna.ats.jta.TransactionManager.class.getName());

View File

@@ -2,6 +2,7 @@ package org.jboss.shamrock.jpa;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
@@ -19,6 +20,7 @@ import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.ClassType;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.IndexView;
import org.jboss.shamrock.deployment.ArchiveContext;
import org.jboss.shamrock.deployment.ProcessorContext;
@@ -43,6 +45,8 @@ final class JpaJandexScavenger {
private static final DotName EMBEDDED = DotName.createSimple(Embedded.class.getName());
private static final DotName MAPPED_SUPERCLASS = DotName.createSimple(MappedSuperclass.class.getName());
private static final DotName ENUM = DotName.createSimple(Enum.class.getName());
private final ArchiveContext archiveContext;
private final ProcessorContext processorContext;
@@ -153,6 +157,15 @@ final class JpaJandexScavenger {
throw new IllegalStateException("The Jandex index is not complete, missing: " + className.toString());
}
}
//we need to check for enums
for(FieldInfo fieldInfo : classInfo.fields()) {
DotName type = fieldInfo.type().name();
ClassInfo typeCi = index.getClassByName(type);
if(typeCi != null && typeCi.superName().equals(ENUM)) {
collector.addEnumType(type.toString());
}
}
//Capture this one (for various needs: Reflective access enablement, Hibernate enhancement, JPA Template)
collector.addEntity(className.toString());
// add superclass recursively
@@ -166,6 +179,7 @@ final class JpaJandexScavenger {
private static class DomainObjectSet implements KnownDomainObjects {
private final Set<String> classNames = new HashSet<String>();
private final Set<String> enumTypes = new HashSet<String>();
public void addEntity(final String className) {
classNames.add(className);
@@ -181,12 +195,22 @@ final class JpaJandexScavenger {
for (String className : classNames) {
processorContext.addReflectiveClass(true, true, className);
}
if(!enumTypes.isEmpty()) {
processorContext.addReflectiveClass(true, false, Enum.class.getName());
for (String className : enumTypes) {
processorContext.addReflectiveClass(true, false, className);
}
}
}
@Override
public boolean contains(final String className) {
return classNames.contains(className);
}
public void addEnumType(String s) {
enumTypes.add(s);
}
}
}