Bonjour,
Alors voila j'ai deux tables "Person" et "Phone" qui sont liees par le biai d'une cle externe pour chacune.
Une persone peut avoir plusieurs num de telephone et inversement. J'ai donc des annotations de type @ManyToMany pour les 2 et je rajoute mon "mappedBy" dans la classe Phone.
L'utilisateur sera ammene a entrer des infos propre a la table Person et son numero de telephone qui sera propre a la classe Phone.
Donc lors de la validation des infos entree, mon action cree une personne (new Person()), ajoute les infos dedans, fait de meme avec Phone et ensuite je gere le lien en recuperant ma liste de personne de Phone auquel j'ajoute ma personne actuelle (ph.getPersonPhone().add(p); et ca me fait rentrer dans l'exception).
Ca devrait marcher mais lors de l'execution je rentre dans l'execption JavaNullPointer Exception.
Donc voila je ne comprends pas du tout ce qui ne va pas.
Voici la partie pertinente de mon code pour illustrer :
Person :
Phone :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 @Entity public class Person implements Serializable { private static final long serialVersionUID = 1L; private Long id; private String forename; private String surname; private String username; private String password; private Set<Phone> phone; @ManyToMany public Set<Phone> getPhone() { return phone; } public void setPhone(Set<Phone> phone) { this.phone = phone; } public void setForename(String forename) { this.forename = forename; } public void setPassword(String password) { this.password = password; } public void setSurname(String surname) { this.surname = surname; } public void setUsername(String username) { this.username = username; } @Column public String getForename() { return forename; } @Column public String getPassword() { return password; } @Column public String getSurname() { return surname; } @Column public String getUsername() { return username; } public void setId(Long id) { this.id = id; } @Id @GeneratedValue(strategy = GenerationType.AUTO) public Long getId() { return id; } ...
RegisterService :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 @Entity public class Phone implements Serializable { private static final long serialVersionUID = 1L; private Long id; private String number; private Set<Person> personPhone; @ManyToMany(mappedBy="phone") public Set<Person> getPersonPhone() { return personPhone; } public void setPersonPhone(Set<Person> person) { this.personPhone = person; } public void setNumber(String number) { this.number = number; } @Column public String getNumber() { return number; } public void setId(Long id) { this.id = id; } @Id @GeneratedValue(strategy = GenerationType.AUTO) public Long getId() { return id; } ...
RegisterAction :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public void InsertPerson(String forename, String surname, String username, String password, String number) throws BusinessException { try { EntityManager em = EntityUtil.getEntityManager("SUMSPU"); em.getTransaction().begin(); //Add a person Person p = new Person(); em.persist(p); p.setForename(forename); p.setSurname(surname); p.setUsername(username); p.setPassword(password); //Add the phone number of the person Phone ph = new Phone(); em.persist(ph); ph.setNumber(number); ph.getPersonPhone().add(p); // ICI JE RENTRE DANS L'EXCEPTION em.getTransaction().commit(); } catch (Exception e) { throw new BusinessException("testFunction failed :"+e,e); } }
Voila, merci de m'aider
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class RegisterAction extends ActionSupport{ private String forename; private String surname; private String username; private String password; private String phone; public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public void setForename(String forename) { this.forename = forename; } public void setPassword(String password) { this.password = password; } public void setSurname(String surname) { this.surname = surname; } public void setUsername(String username) { this.username = username; } public String getForename() { return forename; } public String getPassword() { return password; } public String getSurname() { return surname; } public String getUsername() { return username; } @Override public String execute() throws Exception { //Default login mechanism is to allow the user in try{ RegisterService rs = new RegisterService(); rs.InsertPerson(this.getForename(), this.getSurname(), this.getUsername(), this.getPassword(), this.getPhone()); return SUCCESS; }catch (Exception e) { return ERROR; } }
Andreas
Partager