JPA ManyToMany哪里标注 [英] JPA ManyToMany where annotation

查看:81
本文介绍了JPA ManyToMany哪里标注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JPA/EclipseLink 2.5.2,并想在我的ManyToMany映射中添加一个附加的where子句. SQL代码如下所示:

I'm using JPA / EclipseLink 2.5.2 and want to add an additional where clause to my ManyToMany Mapping. The SQL Code would look like this:

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

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

So I managed to do the JoinMapping with:

@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

But I dont know how to add the WHERE f.verified=1

这怎么实现?

预先感谢, 卡西迪

推荐答案

如我的评论所建议,您不能使用@ManyToMany,因为您需要在联接表中增加一列以指示是否已通过验证.

As advised in my comment you cannot use @ManyToMany as you require an additional column in the join table to indicate whether verified or not.

然后,您需要将@OneToMany与其他实体一起使用,例如Friendship.我们可以使用已验证"列作为区分符,并使用简单的类层次结构来区分未确认"和已确认"朋友.

You then need to use a @OneToMany with an additional Entity, say Friendship. We can use the verified column as a discriminator and use a simple class hierarchy to distinguish between Unconfirmed and Confirmed friends.

这将类似于以下内容(尚未对其进行全面测试).

This will then look something like the below (haven't tested it fully).

注意,我已经使用Hibernate测试了此功能,但是存在问题,因此需要再次查看.这些帖子表明该问题可能是特定于Hibernate的:

  • https://stackoverflow.com/a/1631055/1356423.
  • Why @OneToMany does not work with inheritance in 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;
    }
}

使用已验证列作为区分符的两个子类:

Two subclassses which use the verified column as discriminator:

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

}

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

}

这篇关于JPA ManyToMany哪里标注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆