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]
}
I tried to access locationIdentifier this way in JPA:
String sqlString = "SELECT asset FROM Asset WHERE asset.locationIdentifier = :inputstr";
Query query = entityManager.createQuery(sqlString);
But I got an error: Cannot resolve the property locationIdentifier
I want to ask how I access locationIdentifier using JPQL?
Sorry for my English. Thanks in advance!
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.
@Transient means that the attribute is totally ignored by JPA. It cannot be referred to in a query.
Simply remove @Transient and it should work.
Also, you need to provide the parameter value to the query:
String sqlString = "SELECT asset FROM Asset WHERE asset.locationIdentifier = :inputstr";
Query query = entityManager.createQuery(sqlString);
query.setParameter("inputstr", someValue);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With