I have a 3 tables as follows
Post
pid pname
TagPost
tagid postId postTime
Tag
tagid tagNam
One post can have multiple tags and one tag can have multiple posts. Basically a n*n mapping.
Now how can I model following into HQL queries using session.createQuery()
- Select all challenges which has for a given tag order by creation time of post I can do post.getTagSet(), but I want only n newest posts.
- Select all post which has
javaas one of its tags? Order by postTime. -
Is this right way to model this kind of situation in terms of performance? Or should I have a following structure?
postid pname postTime tags
Value of tags column in this case can be comma seperated.
Sample value of tags column = java,hibernate
Your fist design with three tables is the better one considering all the situations. You can run queries efficiently. These kinds of queries are very common in HQL.
to get n newest posts you can have a query like :
this one is also simple:
In both the cases, you need to pass your object parameters to the query before calling
query.list();(first casepost, second casetag).Thanks and happy coding!