一、在application.yml配置中添加数据库根据实体类自动创建数据库表的配置(这里数据库采用MySQL数据库)

jpa:
database: MYSQL
show-sql: true
#Hibernate ddl auto (validate|create|create-drop|update)
hibernate:
ddl-auto: update
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
enable_lazy_load_no_trans: true

  启动项目时,系统就会自动创建实体类中的定义的表,创建完之后你会发现数据库里的字段和实体类里的字段顺序是不一样的 是乱序的 这是为什么呢?

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

  翻了翻源码才发现,很多地方都是使用LinkedHashMap或者是List来传输Entity里面的fields,于是感觉Hibernate应该是考虑到使用Entity里面定义的fields的顺序来实现建表语句里的表字段顺序的。 
  于是就一步步跟踪下去,终于在一个地方发现了一个问题:org.hibernate.cfg包下的PropertyContainer类在取fields的时候是使用TreeMap来保存的,于是试着改了下,将这个里面的所有TreeMap改成了LinkedHashMap,编译通过,打包,测试。 
终于,我们期待已久的结果出来了:建表语句里面的字段顺序和Entity里面的fields的顺序一致了。

 具体做法如下:

/**

    在本项目中创建一个和源码类一样的包结构和一样名字的类,把所有源码中的所有代码复制到你创建的那个类中,

              然后,就可以对你创建的类 进行修改了,修改好之后启动项目,你就会发现程序走的是你创建的那个类,

    数据库的所有字段都是和实体类排序一样的了。

       **/

这里附上修改后的代码,仅供参考(这里的Hibernate版本为hibernate5)

package org.hibernate.cfg;

import java.util.*;
import javax.persistence.Access;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Transient;
import org.hibernate.AnnotationException;
import org.hibernate.annotations.Any;
import org.hibernate.annotations.ManyToAny;
import org.hibernate.annotations.Target;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.boot.MappingException;
import org.hibernate.boot.jaxb.Origin;
import org.hibernate.boot.jaxb.SourceType;
import org.hibernate.cfg.annotations.HCANNHelper;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.jboss.logging.Logger; /**
* 解决SpringDataJpa实体类中属性顺序与数据库中生成字段顺序不一致的问题
*/
class PropertyContainer {
private static final CoreMessageLogger LOG = (CoreMessageLogger)Logger.getMessageLogger(CoreMessageLogger.class, PropertyContainer.class.getName());
private final XClass xClass;
private final XClass entityAtStake;
private final AccessType classLevelAccessType;
private final LinkedHashMap<String, XProperty> persistentAttributeMap; 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<XProperty> fields = this.xClass.getDeclaredProperties(AccessType.FIELD.getType());
List<XProperty> getters = this.xClass.getDeclaredProperties(AccessType.PROPERTY.getType());
this.preFilter(fields, getters);
Map<String, XProperty> persistentAttributesFromGetters = new HashMap();
this.collectPersistentAttributesUsingLocalAccessType(this.persistentAttributeMap, persistentAttributesFromGetters, fields, getters);
this.collectPersistentAttributesUsingClassLevelAccessType(this.persistentAttributeMap, persistentAttributesFromGetters, fields, getters);
} private void preFilter(List<XProperty> fields, List<XProperty> getters) {
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<String, XProperty> persistentAttributeMap, Map<String, XProperty> persistentAttributesFromGetters, List<XProperty> fields, List<XProperty> getters) {
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<String, XProperty> persistentAttributeMap, Map<String, XProperty> persistentAttributesFromGetters, List<XProperty> fields, List<XProperty> getters) {
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);
}
}
} } public XClass getEntityAtStake() {
return this.entityAtStake;
} public XClass getDeclaringClass() {
return this.xClass;
} public AccessType getClassLevelAccessType() {
return this.classLevelAccessType;
} public Collection<XProperty> getProperties() {
this.assertTypesAreResolvable();
return Collections.unmodifiableCollection(this.persistentAttributeMap.values());
} private void assertTypesAreResolvable() {
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 new AnnotationException(msg);
} private AccessType 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;
} return classDefinedAccessType;
}
} private static boolean discoverTypeWithoutReflection(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 boolean mustBeSkipped(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());
}
}

至此,又可以愉快的玩耍啦。。。。。。。。。。。。

解决SpringDataJpa实体类中属性顺序与数据库中生成字段顺序不一致的问题的更多相关文章

  1. swoft orm中的坑(针对实体类的属性名称和数据库字段不相等)

    最近在用swoft的orm,发现了一些问题: 首先看下实体类的定义 它的属性名称和所映射的数据库字段名不一致,这个就会导致蛋疼的问题,首先,在我们使用orm的时候,应该使用哪个字段? 我直接说结论,在 ...

  2. Java实体类的属性类型与数据库表字段类型对应表

    原文地址:https://blog.csdn.net/lyhjava/article/details/50562786 Java中的数据类型和SQL中的数据类型有很多不一样,需要仔细区分,不然易在开发 ...

  3. ibernate学习笔记5---实体类或属性名与数据库关键字冲突、hql命名参数、hql实现通用分页

    一.实体类或属性名与数据库关键字冲突问题1.实体类名与数据库中的关键字冲突比如:实体表User与oracle中的系统表冲突解决方式1:在xml中添加table属性,指定表名,使其不与name默认相等 ...

  4. 关于解决SpringDataJpa框架实体类表字段创建顺序与数据库表字段展示顺序不一致的问题

    今天在公司的项目开发中,遇到一个问题: 后端对象实体类中写入字段顺序与数据库中的存储顺序不一致. 仔细观察到数据库中的表字段的排序方式是按照拼音字母的顺序abcdef......来存储的 而我的实体类 ...

  5. java 中利用反射机制获取和设置实体类的属性值

    摘要: 在java编程中,我们经常不知道传入自己方法中的实体类中到底有哪些方法,或者,我们需要根据用户传入的不同的属性来给对象设置不同的属性值,那么,java自带的反射机制可以很方便的达到这种目的,同 ...

  6. 实体类相同属性间赋值与如何判断实体类中是否所有的字段都为null

    1,实体类相同属性间赋值 /// <summary> /// 将实体2的值动态赋值给实体1(名称一样的属性进行赋值) /// </summary> /// <param ...

  7. java中循环遍历实体类的属性和数据类型以及属性值

    package com.walkerjava.test; import java.lang.reflect.Field; import java.lang.reflect.InvocationTarg ...

  8. java中如何遍历实体类的属性和数据类型以及属性值

      package com.walkerjava.test; import java.lang.reflect.Field; import java.lang.reflect.InvocationTa ...

  9. 解决mybatis实体类和数据库列名不匹配的两种办法

    我们在实际开发中,会遇到实体类与数据库类不匹配的情况,在开发中就会产生各种各样的错误,那么我们应该怎么去解决这一类的错误呢?很简单,下面我们介绍两种解决方法: 首先我们看一下数据库和实体类不匹配的情况 ...

随机推荐

  1. War 包部署

    Springboot 进行war包部署,以及踩坑历险!!! https://www.jianshu.com/p/4c2f27809571 Springboot2项目配置(热部署+war+外部tomca ...

  2. C# in 参数修饰符

    in 修饰符记录: 新版C# 新增加的 in 修饰符:保证发送到方法当中的数据不被更改(值类型),当in 修饰符用于引用类型时,可以改变变量的内容,单不能更改变量本身. 个人理解:in 修饰符传递的数 ...

  3. C# - VS2019WinFrm程序通过SMTP方式实现邮件发送

    前言 本篇主要记录:VS2019 WinFrm桌面应用程序通过SMTP方式实现邮件发送.作为Delphi转C#的关键一步,接下来将逐步实现Delphi中常用到的功能. 准备工作 搭建WinFrm前台界 ...

  4. java 的11个特性

    以下11个特性来自于著名的"java白皮书"中,博主会针对这些特性一一进行粗略的解释.相信看完博主的这篇文章,以后在和小伙伴们吹牛逼,可就有了切实可靠的理论依据了. 11个特性分别 ...

  5. Redis入门学习(一):简介

    Redis是一个开源的.高性能的.基于键值对的缓存与存储系统,通过提供多种键值数据类型来适应不同场景下的缓存与存储需求.同时Redis的诸多高层级功能使其可以胜任消息队列.任务队列等不同的角色. 20 ...

  6. 用python登录12306 并保存cookie

    一篇拿来记录的文章,是看其他博主写的,想在这记一下 import sys import time import requests from PIL import Image import json i ...

  7. 数据库系统(五)---MySQL基础

    一.SQL基本概念: SQL 已经成为关系数据库的标准语言,是一种数据库查询和程序设计语言,用 于存取数据以及查询.更新和管理关系数据库系统. 功能不仅仅是查询,还包括数据定义.数据操纵和数据控制等于 ...

  8. bit和byte的区别是什么?

    bit(位/比特):计算机运算的基础单位: byte(字节):计算机中文件大小的基本计量单位. 转换关系:8 bit = 1 Byte1024 Byte = 1 KB1024 KB = 1 MB102 ...

  9. python_机器学习_最临近规则分类(K-Nearest Neighbor)KNN算法

    1. 概念: https://scikit-learn.org/stable/modules/neighbors.html 1. Cover和Hart在1968年提出了最初的临近算法 2. 分类算法( ...

  10. linux 线程基础

    线程基础函数 查看进程中有多少个线程,查看线程的LWP ps -Lf 进程ID(pid) 执行结果:LWP列 y:~$ ps -Lf 1887 UID PID PPID LWP C NLWP STIM ...