具有 @Transient 字段的 JPA 实体可以存储来自 SQL 查询结果集的数据吗?

发布于 2024-12-10 20:34:13 字数 1015 浏览 0 评论 0原文

示例:

@Entity
class Table_A{
    @Id
    @generatedValue
    private long ID;

    @Column
    private String name;

    @Transient
    private String otherName;

    // getters and setters

    public long getID()
    { return ID;}
    public void setID(long ID)
    { this.ID = ID;}

     public String getName()
    { return Name;}
    public void setName(String name)
    { this.name = name;}

    public String getOtherName()
    { return otherName;}
    public void setOtherName(String otherName)
    { this.otherName = otherName;}
}

查询示例:

String sql = "SELECT Table_A.*, otherName 
FROM Table_A INNER JOIN Other_Table ON Table_A.id = Other_Table.id";

List<Table_A> = em.createNativeQuery(sql, Table_A.class).getResultList();

注意:

“otherName”列是Other_Table 的一个字段。

Other_Table 不是实体

问题:

问题是,我无法将“otherName”列结果存储到 Table_A 实体的 @Transient 字段。有什么办法可以做到这一点吗?它总是返回 null。

Example:

@Entity
class Table_A{
    @Id
    @generatedValue
    private long ID;

    @Column
    private String name;

    @Transient
    private String otherName;

    // getters and setters

    public long getID()
    { return ID;}
    public void setID(long ID)
    { this.ID = ID;}

     public String getName()
    { return Name;}
    public void setName(String name)
    { this.name = name;}

    public String getOtherName()
    { return otherName;}
    public void setOtherName(String otherName)
    { this.otherName = otherName;}
}

Query Example:

String sql = "SELECT Table_A.*, otherName 
FROM Table_A INNER JOIN Other_Table ON Table_A.id = Other_Table.id";

List<Table_A> = em.createNativeQuery(sql, Table_A.class).getResultList();

NOTE:

"otherName" column is a field of Other_Table.

Other_Table is not an Entity

PROBLEM:

the problem is, I can't store the "otherName" column result to the @Transient field of the Table_A entity. Is there any way of doing this? It always returns null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丢了幸福的猪 2024-12-17 20:34:13

问题是您使用了 createNativeQuery 期望能够将结果直接放入您的类 Table_A 中 - 但当然otherName 不会映射。

如果您使用其他形式的 createNativeQuery,使用命名的 @SqlResultSetMapping,您可能能够获取以下值将 otherName 转换为您可以使用的 @ColumnResult 。查看此处的示例

恐怕您仍然可能需要在某个时候手动调用 setOtherName() :-(

The problem is that you've used the form of createNativeQuery that expects to be able to place the result directly into your class Table_A - but of course the otherName won't map.

If you use the other form of createNativeQuery, that uses a named @SqlResultSetMapping, you might be able to get the value of otherName into a @ColumnResult that you can use. Have a look at the example here.

I'm afraid you'll still probably have to manually call setOtherName() at some point though :-(

鸠书 2024-12-17 20:34:13

不,这是不可能的。但修复方法很明显:将 Other_Table 映射到一个实体,并在两个实体之间引入关联。大多数情况下,被迫使用瞬态字段和 SQL 查询是 JPA 设计不正确的标志。不要和JPA打架。按照预期的方式使用它,一切都会变得更加容易。

No, it's not possible. But the fix is obvious: map the Other_Table to an entity, and introduce an association between both entities. Being forced to use transient fields and SQL queries is most of the time a sign of an incorrect JPA design. Don't fight with JPA. Use it the way it's intended to be used, and everything will be much easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文