Results 1 to 2 of 2

Thread: Foreign Key

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

    Default Foreign Key


    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:
    	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;
    		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) { = id;
    Speaker Entity:
    XML Code:
    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;
    	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) { = 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()">
    	<td>Subject</td><td><input type="text" name="subject"></td>
    	<td>Campus</td><td><select name="ListCampus"><option value="bordeaux">BORDEAUX</option><option value="bruxelles">BRUXELLES</option></select></td>
    	<td>From</td><td><input type="text" name="begin_date"></td>
    	<td>To</td><td><input type="text" name="end_date"></td>
    	<td>Description</td><td><input type="text" name="description"></td>
    	<td>Speaker: </td><td><%= request.getSession().getAttribute("last_name") %></td>
    	<td></td><td><input type="submit" value="Submit"> <input type="button" value="Cancel" onclick="self.location.href='index.jsp'"></td>
    When this form is accepted, the servlet 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 ?!
    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.setStatus("In progress"); //Non achevé...
    intervention.setSpeaker(request.getSession().getAttribute("last_name"); // DON'T WORK !!!! Because setSpeaker want an Object attribute..
    		EntityTransaction t = em.getTransaction();
    		try {
    		} finally {
    			if (t.isActive())
    				+ "/index.jsp");
    Can anyone help me please ?


  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Rep Power

    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