[英]Hibernate/JPA add a calculated field
I'm currently converting hibernate .hbm.xml files to annotations and have a problem very similar this Adding a calculated field on jpa + jackson 我目前正在将hibernate .hbm.xml文件转换为注释,并且遇到了与以下非常相似的问题: 在jpa + jackson上添加计算字段
I've got a DB Table with some fields including first_name and last_name, so this fields should be persistent (this works as expected). 我有一个包含一些字段的数据库表,包括first_name和last_name,因此该字段应该是持久性的(按预期工作)。
But now I've got a method getFullName() 但是现在我有了方法getFullName()
// ?????
public String getFullName() {
return getFirstName() + " " + getLastName();
}
And I don't know which annotation I should add to the method? 而且我不知道应该在方法中添加哪个注释? (tried @Transient, @Formula, @Basic, @Column, .... fullName shouldn't be persistent)
(尝试过@ Transient,@ Formula,@ Basic,@ Column,...。fullName不应是持久的)
The problem is that I use the fullName to create an Order 问题是我使用fullName创建订单
Order.asc("fullName");
and then use it in the method 然后在方法中使用它
public List<Student> findAll(Session session, Order order) {
Criteria crit = session.createCriteria(Student.class);
if (null != order) {
crit.addOrder(order);
}
return crit.list();
}
......................................................... .................................................. .......
My current 'solution' is 我当前的“解决方案”是
@Formula("concat(first_name,' ',last_name)")
private String name;
@Override
public String getFullName() {
return getFirstName() + " " + getLastName();
}
But this is redundant and not really what I want. 但这是多余的,并不是我真正想要的。
In the end I want to use the latest Hibernate version. 最后,我想使用最新的Hibernate版本。
If the fullName
is not persistence then you should use the @Transient
, but the javax.persistence
one. 如果
fullName
不是持久性,则应使用@Transient
,但应使用javax.persistence
。
Also, put the
-> this was actually incorrect as @Transient and @Formula don't work together very well. @Formula("concat(first_name,' ',last_name)")
on the
getFullName
and remove the
getName
此外,把
- >这实际上是不正确的,@Transient和@Formula不在一起工作得很好。 @Formula("concat(first_name,' ',last_name)")
的
getFullName
和删除
getName
Like said in the comment, you can create your own order to add to the criteria api. 如评论中所述,您可以创建自己的订单以添加到条件api。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.