2

私はこのモデルを持っています。2つのエンティティだけです。1つはキー用の埋め込み可能なエンティティで、もう1つはそのキーがidフィールドとして存在するエンティティです。

「誰かの名前の人のすべての機能を私に与えてください」の「ID5の人のすべての機能を私に与えてください」のような簡単なクエリを書く方法を知りたいです。

その埋め込み可能なキーがあるときにこれらの情報にアクセスする方法がわかりません...

コードの周りにたくさんのものを書き直さなければならないので、モデルを書き直すのをためらっています。

その関連付けテーブルからいくつかのものを削除するにはどうすればよいですか?その問題を攻撃するためにどの「方法」を取るべきか、私は本当に知りません。

ヒントをありがとう

@Entity
@Table(name = "PERSON")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "person_id")
    private Long id;

    @Column(name = "name", unique = true)
    private String name;
    // .. getters and setters

@Entity
@Table(name = "FUNC")
public class Function {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "function_id")
    private Long id;

    @Column(name = "name")
    private String name;
    // .. getters and setter

@Embeddable
public class PersonFunctionPK {

    @Column(name = "person_id")
    private Long personId;

    @Column(name = "function_id")
    private Long functionId;

    public PersonFunctionPK() {
    }

    PersonFunctionPK(Long personId, Long functionId) {
        this.personId = personId;
        this.functionId = functionId;
    }
    // .. getters and setter

@Entity
@Table(name = "PERSON_FUNC")
public class PersonFunction {

    @EmbeddedId
    protected PersonFunctionPK personFunctionPK;

    public PersonFunction() {}

    public PersonFunction(PersonFunctionPK personFunctionPK) {
        this.personFunctionPK = personFunctionPK;
    }

    public PersonFunction(Long personId, Long functionId) {
        this.personFunctionPK = new PersonFunctionPK(personId, functionId);
    }

    // .. getters and setter for personFunctionPK
4

1 に答える 1

2

これらを単一のスタンドアロンエンティティとしてマッピングしているようです。エンティティ間の関係をマップする場合は、getメソッドを呼び出すだけでほとんどのクエリを実行できるはずです(jpqlは不要)

@Entity
@Table(name = "PERSON")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "person_id")
    private Long id;

    @Column(name = "name", unique = true)
    private String name;

    @ManyToMany(mappedBy = "persons", cascade=CascadeType.ALL) 
    private Collection<Function> functions;

    // .. getters and setters

@Entity
@Table(name = "FUNC")
public class Function {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "function_id")
    private Long id;

    @Column(name = "name")
    private String name;

    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name = "PERSON_FUNC",
    joinColumns = {@JoinColumn(name = "function_id", referencedColumnName = "id")}, 
    inverseJoinColumns = {@JoinColumn(name = "person_id", referencedColumnName = "id")}) 
    private Collection<Person> persons;

    // .. getters and setter

これで、IDが5の人を取得した場合、単純なゲッターを呼び出して、この人だけの関数を取得できます。Stefanと呼ばれるすべての人に割り当てられた関数のコレクションが必要な場合は、JPQLを使用する必要があります。ただし、JPQLではオブジェクトの関係(基になるDBではない)を指定するため、@ManyToManyをマップする必要があります。

select distinct f from Function f inner join f.persons p where p.name = "Stefan"

私はこのコードをテストしていませんが、大まかに正しいはずです。

于 2012-11-30T12:04:45.100 に答える