I have the tables: Post and UserSettings. userId is coming from external DB that’s why both entites are unrelated.
I am using hibernate 6. The left join with comments is already working.
@Entity
data class Post(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long,
val userId: Long,
val content: String,
@OneToMany(mappedBy = "post", fetch = FetchType.LAZY)
val comments: List<Comment> = emptyList()
)
@Entity
data class UserSettings(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long,
val userId: Long,
@Enumerated(EnumType.STRING) // Ensure correct enum mapping
val settings: Setting
)
enum class Setting{
ALL,
NONE,
CUSTOM
}
I want to have a LEFT join between Post
and UserSettings
to get back Post object and settings so I did the following inside the repository
@Query("""
select new com.example.dto.PostWithSettingsDTO(p, us.settings)
from Post p
LEFT JOIN FETCH p.comments c
LEFT JOIN UserSettings us
ON p.userId = us.userId
where us.settings= 'ALL'
""")
fun findPostsWithSettings(
): List<PostWithSettingsDTO>
data class PostWithSettingsDTO(
val post: Post,
val settings: String
)
When I did that, I got the
error
org.hibernate.query.SemanticException: Could not interpret path expression 'us.settings'
It did work with the WHERE instead of LEFT join but I need the LEFT join.