Problème d'ajout de deux objets
Bonjour,
j'ai un problème d'ajout de deux objets tant que j'ai besoin d'un 1
voila les différentes classe de mon projet
Player:
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 70 71 72 73
| @Entity
public class Player implements Serializable {
private int code;
private String firstName;
private String lastName;
private String nationality;
private Date dateOfBirth;
private List<Contract> contracts;
private static final long serialVersionUID = 1L;
public Player() {
super();
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getCode() {
return this.code;
}
public void setCode(int code) {
this.code = code;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@OneToMany(mappedBy = "player", cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
public List<Contract> getContracts() {
return contracts;
}
public void setContracts(List<Contract> contracts) {
this.contracts = contracts;
}
public void addContracts(List<Contract> contracts) {
for (Contract c : contracts) {
c.setPlayer(this);
}
this.contracts = contracts;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
} |
classe Team:
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 70 71 72 73
| @Entity
public class Team implements Serializable {
private int code;
private String name;
private String country;
private String division;
private Date established;
private List<Contract> contracts;
private static final long serialVersionUID = 1L;
public Team() {
super();
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getCode() {
return this.code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return this.country;
}
public void setCountry(String country) {
this.country = country;
}
public String getDivision() {
return this.division;
}
public void setDivision(String division) {
this.division = division;
}
public Date getEstablished() {
return this.established;
}
public void setEstablished(Date established) {
this.established = established;
}
@OneToMany(mappedBy = "team", cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
public List<Contract> getContracts() {
return contracts;
}
public void setContracts(List<Contract> contracts) {
this.contracts = contracts;
}
public void addContracts(List<Contract> contracts) {
for (Contract c : contracts)
c.setTeam(this);
this.contracts = contracts;
} |
et la classe Contract:
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 70 71 72 73 74 75 76
|
@Entity
public class Contract implements Serializable {
private int reference;
private String type;
private double duration;
private Date dateSignature;
private boolean state;
private Team team;
private Player player;
private static final long serialVersionUID = 1L;
public Contract() {
super();
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getReference() {
return this.reference;
}
public void setReference(int reference) {
this.reference = reference;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getDuration() {
return duration;
}
public void setDuration(double duration) {
this.duration = duration;
}
public Date getDateSignature() {
return dateSignature;
}
public void setDateSignature(Date dateSignature) {
this.dateSignature = dateSignature;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
@ManyToOne(cascade = CascadeType.ALL)
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
@ManyToOne(cascade = CascadeType.ALL)
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
} |
quand je fait cet test
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
| Player p1 = new Player();
p1.setDateOfBirth(dateOfBirth);
p1.setFirstName("p1");
p1.setLastName("p1");
p1.setNationality("Tunisienne");
Contract c1 = new Contract();
c1.setDateSignature(date);
c1.setDuration(2);
c1.setState(true);
c1.setType("free");
Player p2 = new Player();
p2.setDateOfBirth(dateOfBirth);
p2.setFirstName("p2");
p2.setLastName("p2");
p2.setNationality("Tunisienne");
Contract c2 = new Contract();
c2.setDateSignature(date);
c2.setDuration(3);
c2.setState(true);
c2.setType("free");
Team team = new Team();
team.setCountry("countryteam");
team.setName("name team");
team.setDivision("Ligue 1");
team.setEstablished(established);
c1.setTeam(team);
c2.setTeam(team);
List<Contract> cs1 = new ArrayList<Contract>();
cs1.add(c1);
List<Contract> cs2 = new ArrayList<Contract>();
cs2.add(c2);
p1.addContracts(cs1); // setContracts(cs1)
p2.addContracts(cs2); // setContracts(cs2)
playerBean.addPlayer(p1);
playerBean.addPlayer(p2); |
et voila le problème que j’espère qu'il une solution grâce a votre aide merci
l'ajout de deux objets du team.
http://www5.0zz0.com/2013/12/18/23/432577788.png