[英]Mapping a Transient Property to an Alias - Spring JPA
Is it possible in Spring JPA to map a Transient property of an Object to an alias like so? 在Spring JPA中,是否可以像这样将Object的Transient属性映射到别名?
Native Query 本机查询
SELECT *, 1 AS liked FROM User WHERE user_id = 123 // + logic to determine if liked
Class 类
@Entity
public class User {
@Id
private Long userId;
@Column(name = "displayName")
private String displayName;
@Transient
private int liked; // not tied to any column
}
I've tried to implement this but liked
always returns 0 where it should be 1 (and null if I defined the field as an Object type) 我已经尝试实现此功能,但是
liked
总是返回0,它应该为1(如果我将字段定义为Object类型,则返回null)
Any help is appreciated! 任何帮助表示赞赏!
You should use @Formula annotation for the field (see the example ) 您应该为该字段使用@Formula批注(请参见示例 )
The @Formula annotation to provide an SQL snippet which Hibernate will execute when it fetches the entity from the database.
@Formula批注提供SQL片段,当Hibernate从数据库中获取实体时,它将执行该片段。 The return value of the SQL snippet gets mapped to a read-only entity attribute.
SQL代码段的返回值被映射到只读实体属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.