3

JPAで問題が発生しています。ユーザーが他のユーザーをフォローし、フォローされることを可能にするデータベースを実装しようとしています。私は次のようなものが(要約して)必要だと思います:

USER_TABLE: id | userName
RELATIONSHIP_TABLE: id | follower | followed | acceptation

私は2つのエンティティを持っています(これも要約されています):

@Entity
public class User implements Serializable {

@Id
private Long id;

private String userName;

@OneToMany
private Collection<Relationship> followings;

}


@Entity
public class Relationship implements Serializable {

@Id
private Long id;

private User follower;

private User followed;

private boolean accepted;

}

私の問題は、必要な 2 つよりも多くのテーブルを取得するため、これが可能かどうかわからないことです。

誰でも私を助けることができますか?私の英語に感謝し、申し訳ありません。

4

1 に答える 1

3

関連付けを双方向にしなかったため、より多くのテーブルを取得できます。あなたが言わなければ、 JPAはそれRelationship.followerが反対側であることを知る方法User.followingsがありません:

@Entity
public class User implements Serializable {

    @OneToMany(mappedBy = "follower")
    private Collection<Relationship> followings;

    // ...
}


@Entity
public class Relationship implements Serializable {

    @ManyToOne
    @JoinColumn(name = "follower")
    private User follower;

    @ManyToOne
    @JoinColumn(name = "followed")
    private User followed;

    // ...
}

もちろん、ドキュメントはそれがどのように機能するかを説明しています。

于 2012-08-07T12:56:16.153 に答える