yml配置:
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
jpa:
show-sql: false
hibernate:
ddl-auto: update
实体类代码:
@Getter
@Setter
@Entity
@Table(name = "t_db_bo_report")
public class UnreturnedFaultyBoReport extends BaseEntity {
private static final long serialVersionUID = 1L;
private String rmaLine;
private String odaName;
private String faultyChasing;
private String yearMonthValue;
private String remarks;
}
运行后发现数据库生成的字段顺序与实体类顺序不一致
原因:在org.hibernate.cfg包下的PropertyContainer类在取fields的时候是使用TreeMap来保存的,导致程序给字段自动排序了
解决方法:在项目中新建一个org.hibernate.cfg包名,重写PropertyContainer类(主要将treeMap改成linkedHashMap)
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.hibernate.cfg;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.LinkedHashMap;
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;
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,