在JPA查询中使用@Transient访问字段 [英] Access field with @Transient in JPA query

查看:480
本文介绍了在JPA查询中使用@Transient访问字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有过渡属性的实体:

I have an entity with a transient attribute:

@Entity
@Table(name = "asset")
public class Asset {
    @Transient
    private String locationIdentifier = "N/A";

    @SuppressWarnings("unused")
    @PostLoad
    private void onPostLoad() {
        if (location != null) {
            locationIdentifier = location.getIdentifier();
        }
    }

   [other stuffs]

   }

我试图在JPA中以这种方式访问​​locationIdentifier:

I tried to access locationIdentifier this way in JPA:

String sqlString = "SELECT asset FROM Asset WHERE asset.locationIdentifier = :inputstr";
Query query = entityManager.createQuery(sqlString);

但是我收到一个错误:Cannot resolve the property locationIdentifier

But I got an error: Cannot resolve the property locationIdentifier

我想问一下我如何使用JPQL访问locationIdentifier?

I want to ask how I access locationIdentifier using JPQL?

对不起,我的英语.预先感谢!

Sorry for my English. Thanks in advance!

推荐答案

JPQL查询被转换为SQL查询,并且SQL查询对数据库中的数据进行操作.将属性标记为瞬态意味着该属性不会持久保存到数据库中,因此无法从数据库中查询它.

JPQL queries are transformed to SQL queries and SQL queried operate to the data in database. Marking property transient means that property is not persisted to the database and consequently it cannot be queried from the database.

这篇关于在JPA查询中使用@Transient访问字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆