Hibernate Entityinstance+的列表标准;外场

Hibernate Entityinstance+的列表标准;外场,hibernate,criteria,hibernate-criteria,Hibernate,Criteria,Hibernate Criteria,我敢肯定,这个问题在任何地方都已经被问过了——没有找到答案 我得到了这样一个简单的标准: s.createCriteria(Human.class).list() 给我一个entityinstance的结果列表(性别和姓名) 我是否可以只在生成的entityinstances中添加一个计算,如“敬礼”(不更改Human.java)并避免创建二维数组 我知道,这应该是这样一个装饰类的工作,有什么解决办法吗?放大的entityinstance应该扩展Human类 不,这不完全可能。条件查询只能返回

我敢肯定,这个问题在任何地方都已经被问过了——没有找到答案

我得到了这样一个简单的标准:

s.createCriteria(Human.class).list()
给我一个entityinstance的结果列表(性别和姓名)

我是否可以只在生成的entityinstances中添加一个计算,如“敬礼”(不更改Human.java)并避免创建二维数组


我知道,这应该是这样一个装饰类的工作,有什么解决办法吗?放大的entityinstance应该扩展Human类

不,这不完全可能。条件查询只能返回实体、标量数组或由标量生成的值对象(使用
ResultTransformer

您可以返回一个
HumanWithSalutation
对象列表,该列表将包含与Human+附加称呼相同的字段,但这些将是值对象,而不是持久对象:对这些对象所做的任何修改都不会像对Human实例所做的那样持久化到数据库中

为此,请创建类:

public class HumanWithSalutation extends Human {
    private String salutation;

    // getter and setter
}
为其分配一个
别名BeanResultTransformer
(这将使用setter填充所有
HumanWithValuation
对象),并确保条件具有一个投影列表,返回Human+称呼的所有字段(别名为“称呼”):


如果您不想使用SQL投影,可以在
getsaltation()
方法中用Java实现转换(并删除setter)。

如果您对
@Formula
快速抓取感到满意,可以向
Human
类中添加一个带有
@Formula
映射的属性

或者,如果需要延迟获取此属性,则在与
Human
class相同的表上创建一个额外的实体

@Entity
@Table("HUMAN")
@Immutable
public class HumanWithSalutation extends Human {
    @Id
    private Long id;
    @Formula("(select ...)")
    private String salutation;

    // getter and setter
}
然后通过
@OneToOne(fetch=FetchType.LAZY)
@PrimaryKeyJoinColumn

Human
映射它,使用Hibernate是可能的-查找
addEntity()
addScalar()
@Entity
@Table("HUMAN")
@Immutable
public class HumanWithSalutation extends Human {
    @Id
    private Long id;
    @Formula("(select ...)")
    private String salutation;

    // getter and setter
}