具有 @Transient 字段的 JPA 实体可以存储来自 SQL 查询结果集的数据吗?
示例:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您使用了 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 theotherName
won't map.If you use the other form of createNativeQuery, that uses a named
@SqlResultSetMapping
, you might be able to get the value ofotherName
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 :-(不,这是不可能的。但修复方法很明显:将
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.