修改Hibernate源码实现建表时字段和Entity里定义的fields顺序一致 -

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); 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hibernate可以使用悲观锁和乐观锁来控制多个线程同访问同一条记录的并发性问题。 实现悲观锁的方法是,在Hibernate的查询语句中使用“for update”语句,例如: ``` Session session = sessionFactory.getCurrentSession(); Transaction tx = session.beginTransaction(); try { Item item = (Item) session.get(Item.class, itemId, LockMode.UPGRADE); item.setPrice(newPrice); session.update(item); tx.commit(); } catch (Exception e) { tx.rollback(); throw e; } ``` 在这个例子中,我们使用了LockMode.UPGRADE参数来获取悲观锁,这会在数据库中将该行记录锁定,直到事务提交或回滚为止。 要实现乐观锁,可以使用Hibernate的@Version注解来定义一个版本号属性,例如: ``` @Entity public class Item { @Id private Long id; private String name; private double price; @Version private int version; // getters and setters } ``` 在使用乐观锁的代码中,我们首先获取实体对象,修改实体对象的属性值,然后执行更新操作,例如: ``` Session session = sessionFactory.getCurrentSession(); Transaction tx = session.beginTransaction(); try { Item item = (Item) session.get(Item.class, itemId); item.setPrice(newPrice); session.update(item); tx.commit(); } catch (StaleObjectStateException e) { tx.rollback(); throw new OptimisticLockException("The item has been updated by another transaction", e); } catch (Exception e) { tx.rollback(); throw e; } ``` 在这个例子中,如果在我们修改实体对象的属性值后,有另一个事务已经修改了该实体对象,那么我们就会捕获到StaleObjectStateException异常,这我们就可以回滚事务并抛出一个OptimisticLockException异常,提示用户该实体对象已经被其他事务修改过了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值