transient、persistent、detached状态关系图如下:
1、transient状态的特征:
* 在数据库中没有与之匹配的数据
* 没有纳入session的管理
2、persistent状态的特征:
* persistent状态的对象在数据库中有与之匹配的数据
* 纳入了session的管理
* 在清理缓存(脏数据检查)的时候,会和数据库同步
3、detached状态的特征:
* 在数据库中有与之匹配的数据
* 没有纳入session的管理
PS:了解这几种状态对深入使用hibernate有比较大的意义,开发过程中减少很多不必要的错误。
项目中可能有许多实体的需要辅助的属性和方法辅助,hibernate中实体bean中所有的非static非@Transient那么必须使用@Transient注解的属性都可以被持久化,除非你将其注解为@Transient。 所有没有定义注解的属性相等于@Basic。 @Transient public BigDecimal getTotalServicePrice() { BigDecimal total = new BigDecimal(0); // 总修正费用 total = total.add(getTotalFixPrice()); // 总送票费用 BigDecimal df = getDeliveryInfo().getDeliveryFare(); if (df != null) total = total.add(getDeliveryInfo().getDeliveryFare()); // 总的卖价格调整 total = total.add(priceFix); return total; } http://topmanopensource.javaeye.com/blog/574830