无法容忍的问题
今天一时兴起,搭建了一个Spring Boot 2.0整合Spring Data Jpa的小项目,其实主要目的就是用来给朋友练手的,他接触Java时间不久,写代码的机会少之又少,所以就搭建一个让他来写逻辑玩玩。对于我而言一直都是在用MyBatis,可以说我是MyBatis的死忠粉也不为过,所以今天玩Jpa就遇到了一个问题。使用Jpa创建出来的表字段竟然是乱序的,强迫症怎么能忍!!!
数据表字段顺序乱序
新建了一个User
实体类,大概如下:
@Data
@Entity
@Table(name = "user")
@ApiModel(value = "用户信息")
public class User implements Serializable {
private static final long serialVersionUID = 2936388663101830713L;
@Id
@TableGenerator(name = "AppSeqStore", initialValue = 10000, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "AppSeqStore")
@Column(name = "id", columnDefinition = "bigint(20) comment '主键ID'")
@ApiModelProperty(value = "自增长主键ID", required = false)
private Long id;
/**
* 用户名
*/
@Column(name = "user_name", columnDefinition = "varchar(100) comment '用户名'")
@ApiModelProperty(value = "用户名", required = true)
private String userName;
/**
* 密码
*/
@Column(name = "password", columnDefinition = "varchar(100) comment '用户密码'")
@ApiModelProperty(value = "用户密码", required = true)
private String password;
/**
* 盐值
*/
@Column(name = "salt", columnDefinition = "varchar(100) comment '盐值'")
@ApiModelProperty(value = "盐值", required = true)
private String salt;
/**
* 昵称
*/
@Column(name = "nick_name", columnDefinition = "varchar(100) comment '昵称'")
@ApiModelProperty(value = "昵称", required = true)
private String nickName;
/**
* 头像
*/
@Column(name = "avatar", columnDefinition = "varchar(255) comment '头像url'")
@ApiModelProperty(value = "头像url", required = true)
private String avatar;
/**
* 创建人
*/
@Column(name = "create_id", columnDefinition = "bigint(20) comment '创建人ID'")
@ApiModelProperty(value = "创建人ID", required = true)
private Long createId;
/**
* 创建时间
*/
@Column(name = "create_time", columnDefinition = "datetime comment '创建时间'")
@ApiModelProperty(value = "创建时间", required = true)
private Date createTime;
/**
* 修改人
*/
@Column(name = "modify_id", columnDefinition = "bigint(20) comment '修改人ID'")
@ApiModelProperty(value = "修改人ID", required = true)
private Long modifyId;
/**
* 修改时间
*/
@Column(name = "modify_time", columnDefinition = "datetime comment '修改时间'")
@ApiModelProperty(value = "修改时间", required = true)
private Date modifyTime;
/**
* 是否删除标志
*/
@Column(name = "is_delete", columnDefinition = "int(1) comment '删除标识[0:正常;1:删除;]'")
@ApiModelProperty(value = "删除标识[0:正常;1:删除;]", required = true)
private Integer isDelete;
}
启动项目后创建出来的表效果是这样的:
Jpa创建表乱序原因
经过一顿源码的乱翻,最终刚找到类PropertyContainer
下,发下这里面存放的时候竟然用的TreeMap
,TreeMap
应该是除了HashMap
之外最常用的了,TreeMap
是一个能比较元素大小的Map集合,而且会根据传入key的大小自动进行排序,所以就导致了,Jpa创建数据表的时候,没有跟实体中的字段顺序相对应。
解决方案
既然它使用的是TreeMap,那么就把它换成hashMap就可以变成有序的了。
新建一个包,包路径是org.hibernate.cfg
,然后新建一个类,类名与源码相同PropertyContainer
,将源码复制出来修改一下即可。
代码如下,将其改成LinkedHashMap:
public 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());
}
}
效果
修改后删除原来的表,重新启动项目,效果如下:
修改Jpa建表乱序问题原文地址:传送门
网友评论