Issue
I have a property which is usually fetched lazily:
@Entity
class Version(...,
@Basic(fetch = FetchType.LAZY)
@Type(type = "text")
var mappings: String? = null) {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private val id = 0
}
But sometimes I need to fetch it eagerly. How can I do this with QueryDsl? This is what I currently have:
JPAQuery<Any>(entityManager).from(QVersion.version)
.where(...)
.select(QVersion.version)
.fetchOne()
But this results in a Exception when I try to access the property later:
org.hibernate.LazyInitializationException: Unable to perform requested lazy initialization [Version.mappings] - no session and settings disallow loading outside the Session
Solution
There is a very simple solution for this: .fetchAll()
JPAQuery<Any>(entityManager).from(QVersion.version)
.fetchAll() // <- here
.where(...)
.select(QVersion.version)
.fetchOne()
Related hibernate documentation:
If you are using property-level lazy fetching (with bytecode instrumentation), it is possible to force Hibernate to fetch the lazy properties in the first query immediately using fetch all properties.
Answered By - F43nd1r
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.