Liny_@NotePad

沉迷ACG中

JPA属性访问方式

在model中封装了一个对象,在一些属性的setter中加入了对该对象的访问,结果发现全无效果。

百度了下,原来是因为JPA有两种属性访问方式:

  • 域访问field access):无需设置getter/setter,直接在字段上设定@Column等值,在运行时注入值。
  • 属性访问property access):必须设定getter/setter,在getter上设定@Column等值,通过getter/setter注入值。

原本之前是用直接配置在字段名字上的field access方式,所以没有调用到getter/setter,

而项目使用的是1.0版本的JPA,不支持在一个entity中混用两种方式,因此我把这个entity的属性全部改为了property access。

关于域访问方式与属性访问方式的比较,可以阅读此文:[译文]JPA的实施模式:域访问和属性访问之间的比较

no transaction in progress

用Spring做事务管理,在配JPA的时候出问题,原来用Hibernate的时候就没问题的,换JPA就报这个错:no transaction in progress

百度不到,google之,得解:

Use the spring transaction annotations (which must be enabled on the context) 
For getters use the @Transactional(propagation = Propagation.SUPPORTS). This will ensure a session will actually be openend. 
For create/update/delete use the @Transactional(propagation = Propagation.REQUIRED) to ensure that a either new transaction will be started, or the call on the DAO participates on the current transaction. 

 

原本只在IBaseDao上注明事务,现在要对每个方法都注解一下才可用,囧rz,不知道会是哪里出了问题。