Results 1 to 2 of 2

Thread: Foreign Key

  1. #1
    tristars is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Foreign Key

    Hello,

    First of all, I am French and I apologize for my bad English.

    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:
    XML Code:
    	@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;
    		}
    
    [......]
    Speaker Entity:
    XML Code:
    @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;
    	}
    
    [........]
    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
    like that:
    XML Code:
    	<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>
    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.

    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
    XML Code:
    [........]
    
    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");
    
    [.........]
    Can anyone help me please ?

    Thanks!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Foreign Key

    You can't.
    You need to get the Speaker object itself, presumably based on the last_name (though that does not strike me as unique). Why can you not send the speaker id?

Similar Threads

  1. JPA Primary Key from two foreign key
    By peterbo in forum New To Java
    Replies: 6
    Last Post: 06-29-2014, 10:30 AM
  2. Replies: 5
    Last Post: 07-13-2011, 12:34 PM
  3. Hibernate 2 Foreign keys
    By Freaky123 in forum New To Java
    Replies: 0
    Last Post: 04-25-2011, 04:15 PM
  4. jdbc foreign keys problem
    By axenos in forum JDBC
    Replies: 7
    Last Post: 03-15-2011, 11:07 AM
  5. foreign keys in jtable gui
    By blackpearlmoni in forum New To Java
    Replies: 0
    Last Post: 10-31-2009, 04:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •