Hello,
I am creating a small website about scoring of interventions by speakers. I want to communicate with a database (MySQL with WAMP).
I want that a speaker has one or more interventions, and intervention has only one speaker. So this is a link One-to-many/Many-to-one. So, I created my two entities: Intervention and Speaker with their getter() and setter() method and with their @ManytoOne and @OnetoMany attribute like that:
Intervention Entity:
Speaker Entity:
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 @Entity @Table(name="INTERVENTION") public class Intervention implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String campus; private String subject; private String begin_date; private String end_date; private String description; private String status; private int number_of_mark; private float speaker_mark; private float slides_mark; private float global_event_mark; @ManyToOne @JoinColumn(name="speaker_fk") private Speaker speaker; public Speaker getSpeaker() { return speaker; } public void setSpeaker(Speaker speaker) { this.speaker = speaker; } public int getId() { return id; } public void setId(int id) { this.id = id; } [......]
Then I want to make it possible for a speaker connected to create an intervention. The creation of the intervention is done with a classical form
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 @Entity @Table(name="SPEAKER") public class Speaker implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String email; private String password; private String first_name; private String last_name; @OneToMany(mappedBy="speaker") private java.util.Collection<Intervention> interventionList; public java.util.Collection<Intervention> getInterventionList() { return interventionList; } public void setInterventionList( java.util.Collection<Intervention> interventionList) { this.interventionList = interventionList; } public int getId() { return id; } public void setId(int id) { this.id = id; } [........]
like that:
When this form is accepted, the servlet addIntervention.java is enabled. In it, I get the form parameters and I send it in my mysql database.
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 <form name="FormAddIntervention" method="post" action="addIntervention" onsubmit="return valider()"> <table> <tr> <td>Subject</td><td><input type="text" name="subject"></td> </tr> <tr> <td>Campus</td><td><select name="ListCampus"><option value="bordeaux">BORDEAUX</option><option value="bruxelles">BRUXELLES</option></select></td> </tr> <tr> <td>From</td><td><input type="text" name="begin_date"></td> </tr> <tr> <td>To</td><td><input type="text" name="end_date"></td> </tr> <tr> <td>Description</td><td><input type="text" name="description"></td> </tr> <tr> <td>Speaker: </td><td><%= request.getSession().getAttribute("last_name") %></td> </tr> <tr> <td></td><td><input type="submit" value="Submit"> <input type="button" value="Cancel" onclick="self.location.href='index.jsp'"></td> </tr> </table> </form>
I want the foreign key "speaker_fk" in the table intervention to be filled by the last-name of the speaker currently connected (request.getSession (). getAttribute ("last_name")). But I don't know how ?!
AddInterventionServlet.java
Can anyone help me please ? In french it's ok !
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 [........] protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub EntityManager em = emf.createEntityManager(); Intervention intervention = new Intervention(); intervention.setCampus(request.getParameter("campus")); intervention.setSubject(request.getParameter("subject")); intervention.setBegin_date(request.getParameter("begin_date")); intervention.setEnd_date(request.getParameter("end_date")); intervention.setDescription(request.getParameter("description")); intervention.setStatus("In progress"); //Non achevé... intervention.setNumber_of_mark(Integer.parseInt("0")); intervention.setSpeaker(request.getSession().getAttribute("last_name"); // DON'T WORK !!!! Because setSpeaker want an Object attribute.. EntityTransaction t = em.getTransaction(); try { t.begin(); em.persist(intervention); t.commit(); } finally { if (t.isActive()) t.rollback(); em.close(); } response.sendRedirect(getServletContext().getContextPath() + "/index.jsp"); [.........]
Thanks!
Partager