2016-09-01 7 views
0

こんにちは、JPAコンポジットプライマリキーについてお尋ねします。 私のプロジェクトのために私は私の持っているMySQLデータベース異なるテーブルのユーザー、仕事と参加。そのためには、ユーザー、作業および参加のクラスを作成しました。テーブルへの参加には、プライマリキーと作業プライマリキーを参照するコンポジットプライマリキーがあります。 クラスに関わる次のコードをまとめるには、参加MyKeyParticipationオブジェクトをコンポジットプライマリキーとして使用する

@Entity 
@ManagedBean 
@IdClass(value=MyParticipationKey.class) 
public class Participation { 
@Id 
@Column(name="pkIduser") 
private User user; 
@Id 
@Column(name="pkIdet") 
private Work work; 
public User getUser() { 
    return user; 
} 
public void setUser(User user) { 
    this.user = user; 
} 
public Work getWork() { 
    return work; 
} 
public void setWork(Work work) { 
    this.work = work; 
}} 

public class MyParticipationKey implements Serializable { 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
private User user; 
private Work work; 
public User getUser() { 
    return user; 
} 
public void setUser(User user) { 
    this.user = user; 
} 
public Work getWork() { 
    return work; 
} 
public void setWork(Work work) { 
    this.work = work; 
} 
} 

次の行は、クラスのユーザーに関係し、

@Entity 
@ManagedBean 
public class User implements Serializable{ 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Long pkIduser; 
@NotNull(message="Please enter your firstname") 
@Size(min=3,message="Min size 3 characters!") 
private String firstname; 
@NotNull(message="Please enter your lastname") 
@Size(min=3,message="Min size 3 characters!") 
private String lastname; 
@NotNull(message="Phone number field must not be null") 
@Size(min=5,message="Phone number must have !") 
private String phone; 
private String statut; 
@NotNull(message="Enter valid email") 
@Pattern(regexp="([^[email protected]]+)(\\.[^[email protected]]+)*@([^[email protected]]+\\.)+ ([^[email protected]]+)",message="please enter valid email") 
private String email; 
@NotNull(message="please enter password") 
@Size(min=3,message="Password must have 3 characters") 
private String password; 
public Long getPkIduser() { 
    return pkIduser; 
} 
public void setPkIduser(Long pkIduser) { 
    this.pkIduser = pkIduser; 
} 
public String getFirstname() { 
    return firstname; 
} 
public void setFirstname(String firstname) { 
    this.firstname = firstname; 
} 
public String getEmail() { 
    return email; 
} 
public void setEmail(String email) { 
    this.email = email; 
} 
public String getLastname() { 
    return lastname; 
} 
public void setLastname(String lastname) { 
    this.lastname = lastname; 
} 
public String getPhone() { 
    return phone; 
} 
public void setPhone(String phone) { 
    this.phone = phone; 
} 
public String getStatut() { 
    return statut; 
} 
public void setStatut(String statut) { 
    this.statut = statut; 
} 
public String getPassword() { 
    return password; 
} 
public void setPassword(String password) { 
    this.password = password; 
} 


} 

@Entity 
@ManagedBean 
public class Work implements Serializable{ 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Long pkIdet; 
@NotNull(message="Enter the name") 
@Size(min=3,message="Name must have 3 characters!") 
private String name; 
private String type; 
private String description; 
private String statut; 
@ManyToOne 
@JoinColumn(name="pkIduser") 
private User user; 

public User getUser() { 
    return user; 
} 
public void setUser(User user) { 
    this.user = user; 
} 
public String getStatut() { 
    return statut; 
} 
public void setStatut(String statut) { 
    this.statut = statut; 
} 
public Long getPkIdet() { 
    return pkIdet; 
} 
public void setPkIdet(Long pkIdet) { 
    this.pkIdet = pkIdet; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public String getType() { 
    return type; 
} 
public void setType(String type) { 
    this.type = type; 
} 
public String getDescription() { 
    return description; 
} 
public void setDescription(String description) { 
    this.description = description; 
} 
} 

を作業終了する私のIDEのEclipseは私に言うので、私はここで間違っているかを理解していないその 埋め込みIDクラス[クラスgct.entities.MyParticipationKey]からの[作業]は、このクラスの無効なマッピングです。埋め込みID仕様(ソース[クラスgct.entities.Participation]の属性[myParticipationKey])で使用される埋め込み可能なクラスには、基本的なマッピングしか含めることができません。非基本マッピングを削除するか、埋め込み元の埋め込みID仕様を変更してください。 私のお手伝いをしてください、ありがとうございます。感謝!

+0

何が間違っていますか?いくつかの文書を見てください。http://www.datanucleus.org/products/accessplatform_5_0/jpa/orm/compound_identity.html#a1_1_uni –

+0

@NeilStockton大丈夫です。私は訪問しましたが、私が欲しいものは見つけられません。私の質問は、 "参加"という名前のテーブルを生成する関係N-Nによって結合された2つのテーブルに関するものです。 – SANDWIDI

+0

そのページには、エンティティをPKの一部として持つ方法が記載されています。あなたが何を望んでいるのかを言わなかったので、どのようにして誰を助けることができますか? –

答えて

0

@IdClassには、ターゲットエンティティの@Idフィールドに一致するフィールドが含まれている必要があります。

public class MyParticipationKey implements Serializable { 
    private Long user; 
    private Long work; 
    ... 
} 

注通常@IdClassのように、フィールド名が企業の対応するフィールド名と一致する必要があります、(userworkを、この場合);:MyParticipationKeyは次のようになります。フィールドのタイプはターゲットエンティティの主キーフィールド(この場合はLong)のタイプと一致する必要があります。

派生したアイデンティティについては、JPA 2.1仕様、セクション2.4.1で説明します。

+0

明示していただけますか? – SANDWIDI

+0

あなたの質問に 'User'と' Work'のコード(と必要に応じて対応する '@ IdClass')を追加すると、' MyParticipationKey 'コードを使えるようになるかもしれません。 –

+0

いいえ私はUserとWorkのコードを追加しようとしています – SANDWIDI

関連する問題