嗨
我正在處理的場景與twitter類似。我想要做的是獲得使用hibernate標準API的後續用戶的雕像。
類使用:
User
(這其中有一個多對多的關係,本身如下表映射) Status
(這其中有用戶的外鍵誰把那個)
如果我把它寫在這是一個普通的舊SQL。將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;
}
}
我已添加來源。那麼我已經對用戶實體做了一些操作。所以不知道我是否能夠改變設計。但我也會看。如果你能指出我必須做的改變,我會很高興。 – sYl3r 2011-12-25 04:24:20