将SQL查询转换为Hibernate Criteria

时间:2011-12-24 20:39:09

标签: hibernate criteria-api

你好我正在研究的场景与twitter类似。我想要做的是使用hibernate criteria API获取被跟踪用户的雕像。
使用的课程:
User(这个与自己有多对多的关系,由下表映射)
Status(这个有外国用户的外键)
如果我要用普通的旧sql编写它,那就是它。

select * from status where status.user_id IN(select follow_id from follow where follow.user_id = 1)

如何使用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;
    }
}

状态实体

@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;
        }

    }

2 个答案:

答案 0 :(得分:1)

您未能提供实体的代码。如果你这样做,也许我们可以帮助你更好。 我最初的想法是,拥有一个与@ManyToMany关系的实体通常不是一个好主意。 我的第一个建议是看看这篇文章并重新考虑你的数据库设计:

How to define many-to-many to itself in JPA?

答案 1 :(得分:1)

我想我得到了这个问题的答案。这很简单。所以就这样了。

Criteria criteria = session.createCriteria(TweeStatus.class);
criteria.add(Restrictions.in("tweeUser",tweeUser.getFollowingUsers()));
List statuses = criteria.list();