在使用Hibernate时经常会遇到实体类某个字段存的是code值而非我们最终想要的中文具体显示的值,

如果使用Hibernate的一对一关联这种,一个属性还好说,但是如果一个实体类里有多个字段都是需

要转换数据字段的,就麻烦了,用HQL写关联也比较费事,Hibernate @Formula这个注解可以解

决此问题

@Formula("(select t.user_name from user_info t where t.user_id = user_id)")
public String getUserName() {
return userName;
}

 

@Formula("(select t.user_name from user_info t where t.user_id = user_id)")

表示这个字段的值,是表user_info 中user_name 的值,过滤条件是user_id =当前实体类userId

对应的值,加入查询test_formula这个表后,这个实体类的userId的值是admin,那么

where t.user_id = ‘admin’,最后 本实体类中的userName就等于 ‘系统管理员’



@Formula("(select t.code_value from code_list t where t.code = sex)")
public String getSexValue() {
return sexValue;
}

相关文章:

  • 2021-05-17
  • 2022-12-23
  • 2021-11-28
  • 2022-01-17
  • 2021-09-25
  • 2021-11-07
  • 2022-12-23
猜你喜欢
  • 2021-06-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
相关资源
相似解决方案