packageorg.hibernate.cfg;import java.util.*;importjavax.persistence.Access;importjavax.persistence.ManyToMany;importjavax.persistence.ManyToOne;importjavax.persistence.OneToMany;importjavax.persistence.OneToOne;importjavax.persistence.Transient;importorg.hibernate.AnnotationException;importorg.hibernate.annotations.Any;importorg.hibernate.annotations.ManyToAny;importorg.hibernate.annotations.Target;importorg.hibernate.annotations.Type;importorg.hibernate.annotations.common.reflection.XClass;importorg.hibernate.annotations.common.reflection.XProperty;importorg.hibernate.boot.MappingException;importorg.hibernate.boot.jaxb.Origin;importorg.hibernate.boot.jaxb.SourceType;importorg.hibernate.cfg.annotations.HCANNHelper;importorg.hibernate.internal.CoreMessageLogger;importorg.hibernate.internal.util.StringHelper;importorg.jboss.logging.Logger;/*** 解决SpringDataJpa实体类中属性顺序与数据库中生成字段顺序不一致的问题*/
classPropertyContainer {private static final CoreMessageLogger LOG = (CoreMessageLogger)Logger.getMessageLogger(CoreMessageLogger.class, PropertyContainer.class.getName());private finalXClass xClass;private finalXClass entityAtStake;private finalAccessType classLevelAccessType;private final LinkedHashMappersistentAttributeMap;
PropertyContainer(XClass clazz, XClass entityAtStake, AccessType defaultClassLevelAccessType) {this.xClass =clazz;this.entityAtStake =entityAtStake;if (defaultClassLevelAccessType ==AccessType.DEFAULT) {
defaultClassLevelAccessType=AccessType.PROPERTY;
}
AccessType localClassLevelAccessType= this.determineLocalClassDefinedAccessStrategy();assert localClassLevelAccessType != null;this.classLevelAccessType = localClassLevelAccessType != AccessType.DEFAULT ?localClassLevelAccessType : defaultClassLevelAccessType;assert this.classLevelAccessType == AccessType.FIELD || this.classLevelAccessType ==AccessType.PROPERTY;this.persistentAttributeMap = new LinkedHashMap<>();
List fields = this.xClass.getDeclaredProperties(AccessType.FIELD.getType());
List getters = this.xClass.getDeclaredProperties(AccessType.PROPERTY.getType());this.preFilter(fields, getters);
Map persistentAttributesFromGetters = newHashMap();this.collectPersistentAttributesUsingLocalAccessType(this.persistentAttributeMap, persistentAttributesFromGetters, fields, getters);this.collectPersistentAttributesUsingClassLevelAccessType(this.persistentAttributeMap, persistentAttributesFromGetters, fields, getters);
}private void preFilter(List fields, Listgetters) {
Iterator propertyIterator=fields.iterator();
XProperty property;while(propertyIterator.hasNext()) {
property=(XProperty)propertyIterator.next();if(mustBeSkipped(property)) {
propertyIterator.remove();
}
}
propertyIterator=getters.iterator();while(propertyIterator.hasNext()) {
property=(XProperty)propertyIterator.next();if(mustBeSkipped(property)) {
propertyIterator.remove();
}
}
}private void collectPersistentAttributesUsingLocalAccessType(LinkedHashMap persistentAttributeMap, Map persistentAttributesFromGetters, List fields, Listgetters) {
Iterator propertyIterator=fields.iterator();
XProperty xProperty;
Access localAccessAnnotation;while(propertyIterator.hasNext()) {
xProperty=(XProperty)propertyIterator.next();
localAccessAnnotation= (Access)xProperty.getAnnotation(Access.class);if (localAccessAnnotation != null && localAccessAnnotation.value() ==javax.persistence.AccessType.FIELD) {
propertyIterator.remove();
persistentAttributeMap.put(xProperty.getName(), xProperty);
}
}
propertyIterator=getters.iterator();while(propertyIterator.hasNext()) {
xProperty=(XProperty)propertyIterator.next();
localAccessAnnotation= (Access)xProperty.getAnnotation(Access.class);if (localAccessAnnotation != null && localAccessAnnotation.value() ==javax.persistence.AccessType.PROPERTY) {
propertyIterator.remove();
String name=xProperty.getName();
XProperty previous=(XProperty)persistentAttributesFromGetters.get(name);if (previous != null) {throw new MappingException(LOG.ambiguousPropertyMethods(this.xClass.getName(), HCANNHelper.annotatedElementSignature(previous), HCANNHelper.annotatedElementSignature(xProperty)), new Origin(SourceType.ANNOTATION, this.xClass.getName()));
}
persistentAttributeMap.put(name, xProperty);
persistentAttributesFromGetters.put(name, xProperty);
}
}
}private void collectPersistentAttributesUsingClassLevelAccessType(LinkedHashMap persistentAttributeMap, Map persistentAttributesFromGetters, List fields, Listgetters) {
Iterator var5;
XProperty getter;if (this.classLevelAccessType ==AccessType.FIELD) {
var5=fields.iterator();while(var5.hasNext()) {
getter=(XProperty)var5.next();if (!persistentAttributeMap.containsKey(getter.getName())) {
persistentAttributeMap.put(getter.getName(), getter);
}
}
}else{
var5=getters.iterator();while(var5.hasNext()) {
getter=(XProperty)var5.next();
String name=getter.getName();
XProperty previous=(XProperty)persistentAttributesFromGetters.get(name);if (previous != null) {throw new MappingException(LOG.ambiguousPropertyMethods(this.xClass.getName(), HCANNHelper.annotatedElementSignature(previous), HCANNHelper.annotatedElementSignature(getter)), new Origin(SourceType.ANNOTATION, this.xClass.getName()));
}if (!persistentAttributeMap.containsKey(name)) {
persistentAttributeMap.put(getter.getName(), getter);
persistentAttributesFromGetters.put(name, getter);
}
}
}
}publicXClass getEntityAtStake() {return this.entityAtStake;
}publicXClass getDeclaringClass() {return this.xClass;
}publicAccessType getClassLevelAccessType() {return this.classLevelAccessType;
}public CollectiongetProperties() {this.assertTypesAreResolvable();return Collections.unmodifiableCollection(this.persistentAttributeMap.values());
}private voidassertTypesAreResolvable() {
Iterator var1= this.persistentAttributeMap.values().iterator();
XProperty xProperty;do{if (!var1.hasNext()) {return;
}
xProperty=(XProperty)var1.next();
}while(xProperty.isTypeResolved() ||discoverTypeWithoutReflection(xProperty));
String msg= "Property " + StringHelper.qualify(this.xClass.getName(), xProperty.getName()) + " has an unbound type and no explicit target entity. Resolve this Generic usage issue or set an explicit target attribute (eg @OneToMany(target=) or use an explicit @Type";throw newAnnotationException(msg);
}privateAccessType determineLocalClassDefinedAccessStrategy() {
AccessType hibernateDefinedAccessType=AccessType.DEFAULT;
AccessType jpaDefinedAccessType=AccessType.DEFAULT;
org.hibernate.annotations.AccessType accessType= (org.hibernate.annotations.AccessType)this.xClass.getAnnotation(org.hibernate.annotations.AccessType.class);if (accessType != null) {
hibernateDefinedAccessType=AccessType.getAccessStrategy(accessType.value());
}
Access access= (Access)this.xClass.getAnnotation(Access.class);if (access != null) {
jpaDefinedAccessType=AccessType.getAccessStrategy(access.value());
}if (hibernateDefinedAccessType != AccessType.DEFAULT && jpaDefinedAccessType != AccessType.DEFAULT && hibernateDefinedAccessType !=jpaDefinedAccessType) {throw new org.hibernate.MappingException("@AccessType and @Access specified with contradicting values. Use of @Access only is recommended. ");
}else{
AccessType classDefinedAccessType;if (hibernateDefinedAccessType !=AccessType.DEFAULT) {
classDefinedAccessType=hibernateDefinedAccessType;
}else{
classDefinedAccessType=jpaDefinedAccessType;
}returnclassDefinedAccessType;
}
}private static booleandiscoverTypeWithoutReflection(XProperty p) {if (p.isAnnotationPresent(OneToOne.class) && !((OneToOne)p.getAnnotation(OneToOne.class)).targetEntity().equals(Void.TYPE)) {return true;
}else if (p.isAnnotationPresent(OneToMany.class) && !((OneToMany)p.getAnnotation(OneToMany.class)).targetEntity().equals(Void.TYPE)) {return true;
}else if (p.isAnnotationPresent(ManyToOne.class) && !((ManyToOne)p.getAnnotation(ManyToOne.class)).targetEntity().equals(Void.TYPE)) {return true;
}else if (p.isAnnotationPresent(ManyToMany.class) && !((ManyToMany)p.getAnnotation(ManyToMany.class)).targetEntity().equals(Void.TYPE)) {return true;
}else if (p.isAnnotationPresent(Any.class)) {return true;
}else if (p.isAnnotationPresent(ManyToAny.class)) {if (!p.isCollection() && !p.isArray()) {throw new AnnotationException("@ManyToAny used on a non collection non array property: " +p.getName());
}else{return true;
}
}else if (p.isAnnotationPresent(Type.class)) {return true;
}else{return p.isAnnotationPresent(Target.class);
}
}private static booleanmustBeSkipped(XProperty property) {return property.isAnnotationPresent(Transient.class) || "net.sf.cglib.transform.impl.InterceptFieldCallback".equals(property.getType().getName()) || "org.hibernate.bytecode.internal.javassist.FieldHandler".equals(property.getType().getName());
}
}