title: Foreign key problem in Java Persistance API with Netbeans 6.9.1
body: <p>I have an error in setting up Java Persistence in Netbeans. I have used MySQL as Back-End Database Server. </p>
  
  <p>My Code is as below,</p>
  
  <p><strong>Database</strong></p>
  
  <pre><code>CREATE TABLE `hub` (
   `hub_ID` INT(11) NOT NULL AUTO_INCREMENT,
   `hub_Name` VARCHAR(45) NOT NULL,
   PRIMARY KEY (`hub_ID`),
   UNIQUE INDEX `hub_Name` (`hub_Name`)
  )ENGINE=MyISAM
  
  CREATE TABLE `company` (
   `company_ID` INT(11) NOT NULL AUTO_INCREMENT,
   `company_Name` VARCHAR(45) NOT NULL,
   `company_hub_ID` INT(11) NOT NULL,
   PRIMARY KEY (`company_ID`),
   INDEX `fk_company_hub1` (`company_hub_ID`)
   )
  </code></pre>
  
  <p><strong>Persistence.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
    &lt;persistence-unit name="testJPAPU" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
      &lt;class&gt;testjpa.Hub&lt;/class&gt;
      &lt;class&gt;testjpa.Company&lt;/class&gt;
      &lt;properties&gt;
        &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/logistikumdispatch"/&gt;
        &lt;property name="javax.persistence.jdbc.password" value="rootroot"/&gt;
        &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/&gt;
        &lt;property name="javax.persistence.jdbc.user" value="root"/&gt;
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>Hub.java (Entity Class in Project)</strong></p>
  
  <pre><code>    /*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  
  package testjpa;
  
  import java.io.Serializable;
  import java.util.Collection;
  import javax.persistence.Basic;
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.NamedQueries;
  import javax.persistence.NamedQuery;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  
  /**
   *
   * @author Anjum
   */
  @Entity
  @Table(name = "hub")
  @NamedQueries({
      @NamedQuery(name = "Hub.findAll", query = "SELECT h FROM Hub h"),
      @NamedQuery(name = "Hub.findByHubID", query = "SELECT h FROM Hub h WHERE h.hubID = :hubID"),
      @NamedQuery(name = "Hub.findByHubName", query = "SELECT h FROM Hub h WHERE h.hubName = :hubName")})
  public class Hub implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "hub_ID")
      private Integer hubID;
      @Basic(optional = false)
      @Column(name = "hub_Name")
      private String hubName;
  
      @OneToMany(mappedBy="hub", cascade=CascadeType.ALL)
      @JoinColumn(name="company_hub_ID")
      private Collection&lt;Company&gt; companies;
  
      public Hub() {
      }
  
      public Hub(Integer hubID) {
          this.hubID = hubID;
      }
  
      public Hub(Integer hubID, String hubName) {
          this.hubID = hubID;
          this.hubName = hubName;
      }
  
      public Integer getHubID() {
          return hubID;
      }
  
      public void setHubID(Integer hubID) {
          this.hubID = hubID;
      }
  
      public String getHubName() {
          return hubName;
      }
  
      public void setHubName(String hubName) {
          this.hubName = hubName;
      }
  
      public Collection&lt;Company&gt; getCompanies()
      {
          return companies;
      }
  
      public void setCompanies(Collection&lt;Company&gt; companies)
      {
          this.companies = companies;
      }
  
      @Override
      public int hashCode() {
          int hash = 0;
          hash += (hubID != null ? hubID.hashCode() : 0);
          return hash;
      }
  
      @Override
      public boolean equals(Object object) {
          // TODO: Warning - this method won't work in the case the id fields are not set
          if (!(object instanceof Hub)) {
              return false;
          }
          Hub other = (Hub) object;
          if ((this.hubID == null &amp;&amp; other.hubID != null) || (this.hubID != null &amp;&amp; !this.hubID.equals(other.hubID))) {
              return false;
          }
          return true;
      }
  
      @Override
      public String toString() {
          return "testjpa.Hub[hubID=" + hubID + "]";
      }
  
  }
  </code></pre>
  
  <p><strong>Company.java (Entity Class in Project)</strong></p>
  
  <pre><code>package testjpa;
  
  import java.io.Serializable;
  import javax.persistence.Basic;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import javax.persistence.NamedQueries;
  import javax.persistence.NamedQuery;
  import javax.persistence.Table;
  
  /**
   *
   * @author Anjum
   */
  @Entity
  @Table(name = "company")
  @NamedQueries({
      @NamedQuery(name = "Company.findAll", query = "SELECT c FROM Company c"),
      @NamedQuery(name = "Company.findByCompanyID", query = "SELECT c FROM Company c WHERE c.companyID = :companyID"),
      @NamedQuery(name = "Company.findByCompanyName", query = "SELECT c FROM Company c WHERE c.companyName = :companyName"),
      @NamedQuery(name = "Company.findByCompanyhubID", query = "SELECT c FROM Company c WHERE c.companyhubID = :companyhubID")})
  public class Company implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "company_ID")
      private Integer companyID;
      @Basic(optional = false)
      @Column(name = "company_Name")
      private String companyName;
      @Basic(optional = false)
      @Column(name = "company_hub_ID")
      private int companyhubID;
      @ManyToOne()
      @JoinColumn(name="hub_ID", insertable=false, updatable=false)
      private Hub hub;
      public Company() {
      }
  
      public Company(Integer companyID) {
          this.companyID = companyID;
      }
  
      public Company(Integer companyID, String companyName, int companyhubID) {
          this.companyID = companyID;
          this.companyName = companyName;
          this.companyhubID = companyhubID;
      }
  
      public Integer getCompanyID() {
          return companyID;
      }
  
      public void setCompanyID(Integer companyID) {
          this.companyID = companyID;
      }
  
      public String getCompanyName() {
          return companyName;
      }
  
      public void setCompanyName(String companyName) {
          this.companyName = companyName;
      }
  
      public int getCompanyhubID() {
          return companyhubID;
      }
  
      public void setCompanyhubID(int companyhubID) {
          this.companyhubID = companyhubID;
      }
  
      public Hub getHub()
      {
          return hub;
      }
  
      public void setHub(Hub hub)
      {
          this.hub = hub;
      }
  
      @Override
      public int hashCode() {
          int hash = 0;
          hash += (companyID != null ? companyID.hashCode() : 0);
          return hash;
      }
  
      @Override
      public boolean equals(Object object) {
          // TODO: Warning - this method won't work in the case the id fields are not set
          if (!(object instanceof Company)) {
              return false;
          }
          Company other = (Company) object;
          if ((this.companyID == null &amp;&amp; other.companyID != null) || (this.companyID != null &amp;&amp; !this.companyID.equals(other.companyID))) {
              return false;
          }
          return true;
      }
  
      @Override
      public String toString() {
          return "testjpa.Company[companyID=" + companyID + "]";
      }
  
  }
  </code></pre>
  
  <p><strong>Main Program</strong></p>
  
  <pre><code>/*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package testjpa;
  
  import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.Persistence;
  
  /**
   *
   * @author Anjum
   */
  public class Main {
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          EntityManagerFactory emf = Persistence.createEntityManagerFactory("testJPAPU");
          EntityManager em = emf.createEntityManager();
  
          em.getTransaction().begin();
  
          Company c1 = new Company();
          c1.setCompanyName("ays");
  
  
          Hub h1 = new Hub();
          h1.setHubName("abc");
          em.persist(h1);
          c1.setHub(h1);
  
          em.persist(c1);
          em.flush();
  
      }
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>[EL Info]: 2010-09-03 14:39:02.639--ServerSession(3199106)--EclipseLink, version: Eclipse Persistence Services - 2.0.2.v20100323-r6872
  [EL Info]: 2010-09-03 14:39:03.371--ServerSession(3199106)--file:/C:/Users/Anjum/Documents/NetBeansProjects/testJPA/build/classes/_testJPAPU login successful
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>My problem is that, the foreign key company.company_hub_ID is updated in database with comapany.company_name = "ays". But there is always 0 in company.company_hub_ID.</p>
  
  <p>Hub is also updated properly. Means there is no connectivity or JPA problem. Problem lies within the relationship only.</p>
  
  <p>Thanks in advance for helping me.</p>