修改Hibernate源码实现建表时字段和Entity里定义的fields顺序一致
来源:互联网 发布:知乎的邀请函正文 编辑:程序博客网 时间:2024/05/16 04:09
Hibernate(至截稿时最新版本为4.1.3.Final)自动建表的表字段顺序总是随机的,之前我们总是自己写语句建好表,再使用Hibernate进行增删改查。始终是有点不方便。
最近看了下源码,发现很多地方都是使用LinkedHashMap或者是List来传输Entity里面的fields,觉得Hibernate应该是考虑到使用Entity里面定义的fields的顺序来实现建表语句里的表字段顺序的。
于是一步步跟踪下去,终于在一个地方发现了一个问题:org.hibernate.cfg.PropertyContainer在取fields的时候是使用TreeMap来保存的,于是试着改了下,将这个里面的所有TreeMap改成了LinkedHashMap,编译通过,打包,测试。
终于,我们期待已久的结果出来了:建表语句里面的字段顺序和Entity里面的fields的顺序一致了。
以下附上源码和修改后的代码:
源码:
private final TreeMap<String, XProperty> fieldAccessMap;
private final TreeMap<String, XProperty> propertyAccessMap;
private TreeMap<String, XProperty> initProperties(AccessType access) {
if ( !( AccessType.PROPERTY.equals( access ) || AccessType.FIELD.equals( access ) ) ) {
throw new IllegalArgumentException( "Access type has to be AccessType.FIELD or AccessType.Property" );
}
// 其实通过以下注释也可以发现是Hibernate自己的一个失误
//order so that property are used in the same order when binding native query
TreeMap<String, XProperty> propertiesMap = new TreeMap<String, XProperty>();
List<XProperty> properties = xClass.getDeclaredProperties( access.getType() );
for ( XProperty property : properties ) {
if ( mustBeSkipped( property ) ) {
continue;
}
propertiesMap.put( property.getName(), property );
}
return propertiesMap;
}
修改后的代码
private final LinkedHashMap<String, XProperty> fieldAccessMap;
private final LinkedHashMap<String, XProperty> propertyAccessMap;
private LinkedHashMap<String, XProperty> initProperties(AccessType access) {
if ( !( AccessType.PROPERTY.equals( access ) || AccessType.FIELD.equals( access ) ) ) {
throw new IllegalArgumentException( "Access type has to be AccessType.FIELD or AccessType.Property" );
}
//order so that property are used in the same order when binding native query
LinkedHashMap<String, XProperty> propertiesMap = new LinkedHashMap<String, XProperty>();
List<XProperty> properties = xClass.getDeclaredProperties( access.getType() );
for ( XProperty property : properties ) {
if ( mustBeSkipped( property ) ) {
continue;
}
propertiesMap.put( property.getName(), property );
}
return propertiesMap;
}
PS:通过以下代码可以测试建表时的语句:
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
最近看了下源码,发现很多地方都是使用LinkedHashMap或者是List来传输Entity里面的fields,觉得Hibernate应该是考虑到使用Entity里面定义的fields的顺序来实现建表语句里的表字段顺序的。
于是一步步跟踪下去,终于在一个地方发现了一个问题:org.hibernate.cfg.PropertyContainer在取fields的时候是使用TreeMap来保存的,于是试着改了下,将这个里面的所有TreeMap改成了LinkedHashMap,编译通过,打包,测试。
终于,我们期待已久的结果出来了:建表语句里面的字段顺序和Entity里面的fields的顺序一致了。
以下附上源码和修改后的代码:
源码:
private final TreeMap<String, XProperty> fieldAccessMap;
private final TreeMap<String, XProperty> propertyAccessMap;
private TreeMap<String, XProperty> initProperties(AccessType access) {
if ( !( AccessType.PROPERTY.equals( access ) || AccessType.FIELD.equals( access ) ) ) {
throw new IllegalArgumentException( "Access type has to be AccessType.FIELD or AccessType.Property" );
}
// 其实通过以下注释也可以发现是Hibernate自己的一个失误
//order so that property are used in the same order when binding native query
TreeMap<String, XProperty> propertiesMap = new TreeMap<String, XProperty>();
List<XProperty> properties = xClass.getDeclaredProperties( access.getType() );
for ( XProperty property : properties ) {
if ( mustBeSkipped( property ) ) {
continue;
}
propertiesMap.put( property.getName(), property );
}
return propertiesMap;
}
修改后的代码
private final LinkedHashMap<String, XProperty> fieldAccessMap;
private final LinkedHashMap<String, XProperty> propertyAccessMap;
private LinkedHashMap<String, XProperty> initProperties(AccessType access) {
if ( !( AccessType.PROPERTY.equals( access ) || AccessType.FIELD.equals( access ) ) ) {
throw new IllegalArgumentException( "Access type has to be AccessType.FIELD or AccessType.Property" );
}
//order so that property are used in the same order when binding native query
LinkedHashMap<String, XProperty> propertiesMap = new LinkedHashMap<String, XProperty>();
List<XProperty> properties = xClass.getDeclaredProperties( access.getType() );
for ( XProperty property : properties ) {
if ( mustBeSkipped( property ) ) {
continue;
}
propertiesMap.put( property.getName(), property );
}
return propertiesMap;
}
PS:通过以下代码可以测试建表时的语句:
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
0 0
- 修改Hibernate源码实现建表时字段和Entity里定义的fields顺序一致
- 修改Hibernate源码实现建表时字段和Entity里定义的fields顺序一致
- elasticsearch创建multi-fields字段及修改非multi-fields字段为multi-fields字段及multi-field字段的不同的analyzer进行分析和搜索
- webservice接口soap请求报文的xml字段顺序必须与wsdl中定义的顺序一致才能解析正确
- C++编码器军规(004):保持类成员变量定义和初始化的顺序一致
- Hibernate entity column定义
- 修改ORACLE的字段顺序
- 数组的定义和顺序实现
- Hibernate 统一的修改和删除源码
- hibernate字段注释和生成表的字段顺序和外键名称
- FireBird里定义字段的大小写问题
- Hibernate中的Entity类的JPA字段/属性访问策略
- Hibernate entity属性修改导致对象的记录被修改
- 如何让外键表中的字段和主键表的ID一致
- javax.persistence.Entity和org.hibernate.annotations.Entity的区别
- 位域的定义和使用 C Bit Fields
- Hibernate 重复定义字段的错误
- 修改mysql字段顺序的方法
- HTTP请求的方法(与服务器交互方法)
- 欢迎使用CSDN-markdown编辑器
- 2015-12-22
- 软件设计原则(三)里氏替换原则 -Liskov Substitution Principle
- How To Install MySQL on CentOS 7
- 修改Hibernate源码实现建表时字段和Entity里定义的fields顺序一致
- bzoj1618
- 算法复杂度
- [Language]Python中函数与函数式编程
- Linux下执行bcp指令
- Php 数组去重
- Android fragment嵌套fragment 子fragment控制父对象改变布局
- mysql随机获取一条或者多条数据
- HDU 1005 Number Sequence