我最近开始使用 Hibernate,并试图了解所有注释并确保我做事正确。我有两个类似于下面的表'user'和'user_friends'
+------+------+-------+------+
| 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,其中包含该好友的姓名和电子邮件,没有其他内容,而不是他们的注册时间等(通过使用 User 类进行映射)。我也很难确保它只返回接受请求的好友(ACCEPTED=1)
任何人都可以提供任何建议吗?