这两天在用aop写个发信机制,由于发信模板用的是spring的velocity正好顺便学习了其中的el表达式
#if #end #foreach #end
…………跑题了
总之就是我想直接给interceptor传递一个对象剩下取值就全扔给view了
其实我一直很喜欢对象view层是对象到了controller也是对象vopo
所以我比较欣赏用json尤其是getForm和setForm时对于table的list更不用说了,…………又跑题了
比如用户注册我的实体比较乱
User下有one-to-many的Addressone-to-one的Company等
更有些Const是在table保存的codeTable这样几乎也是一对一
比如birthday就是一个CodeList对应
CodeLis.id=111;
CodeList.value="1985"
不过前台只能这样写
name='user.birthday.id' name='user.name'这样我 在action 接受到 的就是一个 user 用 hibernate save 后 db里 也出了数据 然后 我直接
public User todo(User user){
user=this.save(user);
user=this.get(user.getId());
System.out.println(user.getBirthDay.getId()) ;
System.out.println(user.getBirthDay.getValue()) ;
}结果111null
这就很奇怪通过跟踪其实当执行完save后db里已经有了数据可是什么查不到,我看了hb生成的sql语句发现只差了User没有差Address和Birthday莫非是lazyfalse的原因但一对一不可能啊,
最后想了想应该是hb缓存的原因缓存存放对应keyvaluekey为idvalue为po因为我从前台传进了id所以hb从缓存中得到了key就不会去查db自然value也不会更改
于是乎 我调用public void clearCache() {
getHibernateTemplate().clear();
}
public User todo(User user){
user=this.save(user);
this.clearCache();
user=this.get(user.getId());
System.out.println(user.getBirthDay.getId()) ;
System.out.println(user.getBirthDay.getValue()) ;
}这样保证我在get之前清空session拿到的肯定是db里的数据
其实仔细想了下只要我在save后把id改掉就ok不过又担心save后其实vo已经成了po改id会不会导致update干脆就省事的clear吧