gpt4 book ai didi

java - JPA ManyToMany where 注释

转载 作者:太空宇宙 更新时间:2023-11-04 06:21:03 25 4
gpt4 key购买 nike

我正在使用 JPA/EclipseLink 2.5.2,并且想要向我的 ManyToMany 映射添加一个额外的 where 子句。 SQL 代码如下所示:

SELECT * FROM Friends f
INNER JOIN User u ON f.friend_id = u.id
WHERE f.verified=1;

所以我设法通过以下方式进行 JoinMapping:

@Entity
@NamedQueries({
@NamedQuery(name="User.findAll", query="SELECT u FROM User u"),
@NamedQuery(name="User.login", query="SELECT a FROM User a WHERE a.username = :name and a.password = :password"),
@NamedQuery(name="User.findId", query="SELECT a FROM User a WHERE a.id = :id"),
@NamedQuery(name="User.findName", query="SELECT a FROM User a WHERE a.username = :name")

})
public class User implements Serializable {
...
@ManyToMany
@JoinTable(
name="Friends"
, joinColumns={
@JoinColumn(name="user_id")
}
, inverseJoinColumns={
@JoinColumn(name="friend_id")
}
)
List<User> friends;
}

但我不知道如何添加WHERE f.verified=1

如何实现这一点?

提前致谢,卡西迪

最佳答案

正如我的评论中所建议的,您不能使用@ManyToMany,因为您需要在连接表中添加一个附加列来指示是否已验证。

然后,您需要将 @OneToMany 与附加实体(例如友谊)一起使用。我们可以使用已验证列作为鉴别器,并使用简单的类层次结构来区分未确认和已确认的 friend 。

这将如下所示(尚未完全测试)。

注意,我用 Hibernate 对此进行了测试,但存在问题,因此需要再次查看。这些帖子表明该问题可能是 Hibernate 特有的:

因此 EclipseLink 可能值得尝试。

@Entity
@Table(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;

@OneToMany(mappedBy = "user")
private Set<ConfirmedFriendship> confirmedFriendships;

@OneToMany(mappedBy = "user")
private Set<UnconfirmedFriendship> unconfirmedFriendships;

public List<User> getConfirmedFriends() {
return getFriends(confirmedFriendships);
}

public List<User> getUnconfirmedFriends() {
return getFriends(unconfirmedFriendships);
}

private List<User> getFriends(Set<? extends Friendship> friendships){
List<User> friends = new ArrayList<User>();

for(Friendship friendship : friendships) {
friends.add(friendship.getFriend());
}

return friends;
}
}

友谊的基础实体:

@Entity
@Table(name = "friendships")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "verified")
public abstract class Friendship {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

@ManyToOne
@JoinColumn(name = "friend_id")
private User friend;

@Column(name = "verified")
private boolean verified;

public int getId() {
return id;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public User getFriend() {
return friend;
}

public void setFriend(User friend) {
this.friend = friend;
}

public boolean isVerified() {
return verified;
}

public void setVerified(boolean verified) {
this.verified = verified;
}
}

使用已验证列作为鉴别器的两个子类:

@Entity
@DiscriminatorValue(value = "1")
public class ConfirmedFriendship extends Friendship {

}

@Entity
@DiscriminatorValue(value = "0")
public class UnconfirmedFriendship extends Friendship {

}

关于java - JPA ManyToMany where 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27411156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com