Problème de mapping avec table d'association
bonjour,
étant néophyte avec JPA, j'essaie de créer des exemples de tests afin de me perfectionner dessus.
Dans ce but, j'ai crée 3 tables : livres, abonnes et emprunts. Chaque abonne peut emprunter plusieurs livres et chaque livre peut être emprunté plusieurs fois. Ma table d'association est donc emprunts.
Voilà mes entités :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
package com.entities;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* The persistent class for the abonnes database table.
*
*/
@Entity
@Table(name="abonnes")
public class Abonne implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String nom;
private String prenom;
@OneToMany(cascade=CascadeType.ALL, mappedBy="pk.abonne_id")
private List<Emprunt> emprunts = new ArrayList<Emprunt>();
public Abonne() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getNom() {
return this.nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return this.prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public List<Emprunt> getEmprunts() {
return this.emprunts;
}
public void setEmprunts(List<Emprunt> emprunts) {
this.emprunts = emprunts;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
package com.entities;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* The persistent class for the livres database table.
*
*/
@Entity
@Table(name="livres")
public class Livre implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String libelle;
@OneToMany(cascade=CascadeType.ALL, mappedBy="pk.livre_id")
private List<Emprunt> emprunts = new ArrayList<Emprunt>();
public Livre() {
}
public Livre(String libelle){
this.libelle = libelle;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getLibelle() {
return this.libelle;
}
public void setLibelle(String libelle) {
this.libelle = libelle;
}
public List<Emprunt> getEmprunt() {
return emprunts;
}
public void setEmprunt(List<Emprunt> emprunts) {
this.emprunts = emprunts;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
package com.entities;
import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
/**
* Entity implementation class for Entity: Emprunt
*
*/
@Entity
public class Emprunt implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private EmpruntId pk;
public Emprunt() {
super();
}
public EmpruntId getPk() {
return pk;
}
public void setPk(EmpruntId pk) {
this.pk = pk;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
package com.entities;
import java.io.Serializable;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Embeddable
public class EmpruntId implements Serializable{
private static final long serialVersionUID = 1L;
@ManyToOne(fetch=FetchType.EAGER, optional=false)
@JoinColumn(name="abonne_id", referencedColumnName="id")
private Abonne abonne_id;
@ManyToOne(fetch=FetchType.EAGER, optional=false)
@JoinColumn(name="livre_id", referencedColumnName="id")
private Livre livre_id;
public Abonne getAbonne_id() {
return abonne_id;
}
public void setAbonne_id(Abonne abonne_id) {
this.abonne_id = abonne_id;
}
public Livre getLivre_id() {
return livre_id;
}
public void setLivre_id(Livre livre_id) {
this.livre_id = livre_id;
}
} |
J'ai 1 erreur dans chaque entité Livre et Abonne :
Citation:
In attribute 'emprunts', the "mapped by" value 'pk.abonne_id' cannot be resolved to an attribute on the target entity.
Citation:
In attribute 'emprunts', the "mapped by" value 'pk.livre_id' cannot be resolved to an attribute on the target entity.
Je n'arrive pas à faire le pont entre mes entité et l'id correspondant à la clé composite de mes 2 id d'entité...Comment faire cela ?
Merci beaucoup de votre aide