I have table structure as follows

table user: iduser, firstName, lastName, username, email, dateJoined, dateOfBirth, password
table tag : idtag, tagName
table post: idpost, title, content, iduser, date, dateCreated
table post_tag : idpost, idtag :-join table for many to many relationship for tag and post
table user_tag : iduser, idtag :-join table for many to many relationship for user and tag

what i want is the names of the tags for a specific user using JPA query. I also wanted to retrieve the post for the user and I could retrieve that using the following NamedQuery

SELECT p FROM Post p WHERE p.user = :user ORDER BY p.dateCreated DESC

using this, I was successfully able to retrieve posts for the user as Post and user are both entities and is a valid JPA query

But when I try to retrieve the tags for a specific just like I retrieved posts for user, so I tried to write a join query something like this

SELECT t.tagName FROM tag t JOIN user_tag ut ON ut.idtag = t.idtag JOIN user u ON ut.iduser = u.iduser WHERE u.username = :username

but eclipse complaints for this query as user_tag is not the entity type. I am not able to formulate a join query using JPA QL which can retrieve tags for a user

Any help is highly appreciated.

有帮助吗?

解决方案

I was able to formulate the query as follows

SELECT tag.tagName FROM Tag tag WHERE tag.users = :users

This was just like the query I used for Post and User

but I have another doubt that if we don't need to write a join query in this case, then in which scenarios JPA QL join queries are used?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top