来自数据库的Hibernate字段中的瞬态

时间:2011-02-16 15:40:32

标签: java hibernate spring

我已将@Entity称为Video,其中包含字段:

@Column(name="TC_IN")
private BigDecimal tcIn;

@Column(name="TC_OUT")
private BigDecimal tcOut;

在应用程序中,我需要将值转换为另一种格式才能使用它。

所以我添加了字段:

@transient
private String formatTCOut;

public String getFormatTCOut(){
    if (formatTCOut==null){
       sysParamService = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()).getBean("SysParamService");
       formatTCOut =  tcOut * sysParamService.findParamByName("accuracy");
    }
   return formatTCOut 
}

是否有权在Hibernate实体中调用其他服务?\

我的@Transactional( readOnly = true, propagation = Propagation.SUPPORTS )服务高于findParamByName

1 个答案:

答案 0 :(得分:2)

在我看来,这不是一个好的设计,因为这意味着你的Hibernate对象现在永远不能在web / JSF上下文之外使用。

这降低了代码的可重用性,并且难以进行单元测试。

组合来自多个bean或来源的数据/值应该在更高的层次上完成。