我最近開始使用Hibernate和我試圖讓周圍所有的註解我的頭,並確保我做的事情正確。我有兩個表「用戶」和「user_friends」類似於下面Java的Hibernate的註解(@JoinTable,@Filter)
+------+------+-------+------+
| id | name | email | etc. |
+------+------+-------+------+
和用戶的朋友表
+--------+---------+----------+
| userid | buddyid | accepted |
+--------+---------+----------+
現在,在SQL我跑,看起來類似於
查詢SELECT u.id AS id, u.name AS username, u.email AS email FROM user_friends
INNER JOIN users AS u ON u.id = '1' WHERE buddyid = '2' AND ACCEPTED = 1 UNION ALL
SELECT u.id as id, u.name AS username, u.email AS email FROM user_friends
INNER JOIN users AS u ON u.id = '2' WHERE buddyid = '1' AND ACCEPTED = 1;
我在Java中設置了一個時尚與此類似
@Entity
@Table(name="users")
public class User {
@Id
@GeneratedValue
private int id;
private int name;
private int email;
private DateTime registerDate;
private DateTime lastActivity;
private int currency;
private int seasonCurrency;
@OneToMany(fetch=UserBuddy.class, mappedBy="user", fetch=FetchType.LAZY)
@JoinColumn(name="userid")
@Filter(name="messengerBuddyFilter", condition="accepted=1")
private Set<UserBuddy> _buddies;
}
@Entity
@Table(name="user_friends")
public class UserBuddy {
private int id;
private int name;
private int email;
}
有兩個班
我之前問過這個問題,但仍然沒有能夠得到這個工作如何我喜歡它。我需要能夠返回一組UserBuddy.class,其中包含該Buddy的名稱和電子郵件,而不是其他任何內容,而不是其註冊時間等(通過使用User類映射)。我也遇到了困難,確保它只返回那些已經接受該請求(○= 1)
誰能提供任何建議,哥們?
如果'接受'總是反身關係(朋友必須相互接受),那麼可以通過總是存儲buddy1
2012-07-13 19:00:35
從來沒有想過要這樣做。永遠不能擺脫某種東西要告訴我他們是否是朋友的心態(例如,'接受'的專欄)! – DominicEU 2012-07-13 19:05:56