Convert a SQL query to Hibernate Criteria
-
Hi Scenario I'm working on is similar to twitter. What I want to do is get get the statues of followed users using hibernate criteria API. Classes used: User (This one has a many to many relationship to itself mapped by follow table) Status (This one has a foreign key of user who put that) If I'm to write it in a plain old sql this would be it. select * from status where status.user_id IN(select follow_id from follow where follow.user_id = 1) How to achieve this result using Hibernate Criteria API? @javax.persistence.Table(name = "user", uniqueConstraints = { @UniqueConstraint(columnNames = "user_name")}) @Entity public class TweeUser implements Serializable { private int id; @javax.persistence.Column(name = "id") @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } private String userName; @Column(name = "user_name") @Basic public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName = userName; } private String fullName; @javax.persistence.Column(name = "full_name") @Basic public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } private String email; @javax.persistence.Column(name = "email") @Basic public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } private String password; @javax.persistence.Column(name = "password") @Basic public String getPassword() { return password; } private Set<TweeUser> followingUsers; @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "follow", joinColumns = {@JoinColumn(name = "user_id")}, inverseJoinColumns = {@JoinColumn(name = "follow_id")}, uniqueConstraints = {@UniqueConstraint(columnNames = {"user_id", "follow_id"})}) public Set<TweeUser> getFollowingUsers() { return followingUsers; } public void setFollowingUsers(Set<TweeUser> followingUsers) { this.followingUsers = followingUsers; } } Status Entity @javax.persistence.Table(name = "status") @Entity public class TweeStatus implements Serializable{ private int id; @javax.persistence.Column(name = "id") @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } private String rawStatus; @javax.persistence.Column(name = "raw_status") @Basic public String getRawStatus() { return rawStatus; } public void setRawStatus(String rawStatus) { this.rawStatus = rawStatus; } private Date createTime; @Temporal(TemporalType.TIMESTAMP) @javax.persistence.Column(name = "create_time") @Basic public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } private TweeUser tweeUser; @ManyToOne @JoinColumn(name = "user_id") public TweeUser getTweeUser(){ return tweeUser; } public void setTweeUser(TweeUser tweeUser){ this.tweeUser = tweeUser; } private Set<TweeHashTag> tweeHashTags; @ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL) @JoinTable(name = "status_hash", joinColumns = { @JoinColumn(name = "status_id") }, inverseJoinColumns = { @JoinColumn(name = "hash_id")},uniqueConstraints = { @UniqueConstraint( columnNames = { "status_id", "hash_id" } ) }) public Set<TweeHashTag> getTweeHashTags(){ return tweeHashTags; } public void setTweeHashTags(Set<TweeHashTag> tweeHashTags){ this.tweeHashTags = tweeHashTags; } }
-
Answer:
I think I got the answer to this question. It was fairly simple. So here it goes. Criteria criteria = session.createCriteria(TweeStatus.class); criteria.add(Restrictions.in("tweeUser",tweeUser.getFollowingUsers())); List statuses = criteria.list();
sYl3r at Stack Overflow Visit the source
Other answers
You failed to provide the code of your Entities. If you do, maybe we can help you better. My initial thoughts are that it is generally not a good idea to have an Entity with @ManyToMany relations to itself. My first suggestion is to take a look at this post and maybe reconsider your db design: http://stackoverflow.com/questions/4207935/how-to-define-many-to-many-to-itself-in-jpa
baba
Related Q & A:
- How to convert my SQL query to MS Access query?Best solution by Stack Overflow
- How to output XML from a regular SQL query?Best solution by Stack Overflow
- Is it possible to execute big SQL query in Hibernate?Best solution by Stack Overflow
- How to convert a SQL query into hibernate criteria?Best solution by Stack Overflow
- How to convert SQL query to LINQ query?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.