mysql数据库中属性次序_解决SpringDataJpa实体类中属性顺序与数据库中生成字段顺序不一致的问题...

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());

}

}

【2021年,将Spring全家桶系列课程进行Review,修复顺序等错误。进入2022年,将Spring的课程进行整理,整理为案例精讲的系列课程,并新增高级的Spring Security等内容,通过手把手一步步教你从零开始学会应用Spring,课件将逐步进行上传,敬请期待】 本课程是Spring案例精讲课程的第五部分Spring DataSpring案例精讲课程以真实场景、项目实战为导向,循序渐进,深入浅出的讲解Java网络编程,助力您在技术工作更进一步。 本课程聚焦Spring Data的核心知识点:Spring Data Repository、Spring Data JPA(增删改查案例、实体自动生成数据库表、增加新的Repository方法、分页、排序、@NamedQuery、@Query及其分页和排序及参数设置、@NamedEntityGragh实现多对多映射、及QueryHints等)、Spring Data JDBC(增删改查案例、@Query等)的案例介绍, 快速掌握Spring Data的核心知识,快速上手,为学习及工作做好充足的准备。 由于本课程聚焦于案例,即直接上手操作,对于Spring的原理等不会做过多介绍,希望了解原理等内容的需要通过其他视频或者书籍去了解,建议按照该案例课程一步步做下来,之后再去进一步回顾原理,这样能够促进大家对原理有更好的理解。 【通过Spring全家桶,我们保证你能收获到以下几点】 1、掌握Spring全家桶主要部分的开发、实现2、可以使用Spring MVC、Spring Boot、Spring Cloud及Spring Data进行大部分的Spring开发3、初步了解使用微服务、了解使用Spring进行微服务的设计实现4、奠定扎实的Spring技术,具备了一定的独立开发的能力  【实力讲师】 毕业于清华大学软件学院软件工程专业,曾在Accenture、IBM等知名外企任管理及架构职位,近15年的JavaEE经验,近8年的Spring经验,一直致力于架构、设计、开发及管理工作,在电商、零售、制造业等有丰富的项目实施经验  【本课程适用人群】如果你是一定不要错过!  适合于有JavaEE基础的,如:JSP、JSTL、Java基础等的学习者没有基础的学习者跟着课程可以学习,但是需要补充相关基础知识后,才能很好的参与到相关的工作。 【Spring全家桶课程共包含如下几门】5. 进阶篇:SpringData 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值