gpt4 book ai didi

java - 将 transient 属性映射到别名 - Spring JPA

转载 作者:行者123 更新时间:2023-12-02 12:22:07 24 4
gpt4 key购买 nike

在 Spring JPA 中是否可以将对象的 Transient 属性映射到这样的别名?

native 查询

SELECT *, 1 AS liked FROM User WHERE user_id = 123 // + logic to determine if liked

@Entity
public class User {

@Id
private Long userId;

@Column(name = "displayName")
private String displayName;

@Transient
private int liked; // not tied to any column

}

我尝试实现这个,但 liked 总是返回 0,而它应该是 1(如果我将该字段定义为对象类型,则返回 null)

感谢任何帮助!

最佳答案

您应该对该字段使用 @Formula 注释(请参阅 the example )

The @Formula annotation to provide an SQL snippet which Hibernate will execute when it fetches the entity from the database. The return value of the SQL snippet gets mapped to a read-only entity attribute.

关于java - 将 transient 属性映射到别名 - Spring JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45692816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com