title: Ecplise JPA Adding new parent object
body: <p>I'm trying to build an application with Netbean. I use Eclipse IDE &amp; JPA API. the Entities is like this:</p>
  
  <p>NATS.java</p>
  
  <pre><code>@Entity
  @Table(name = "NATS")
  Public Class NATS implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer code;
    private String nName;
    @Column(name = "cap_id") // for foreign key purpose
    private Integer capId;
    @OneToOne(cascade=CascadeType.PERSIST)
    @PrimaryKeyJoinColumn(name = "cap_id" )
    private City capital;
    public City getCapital(){
        return this.capital;
    }
    public void setCapital (City newcapital){
        this.capital = newcapital;
    }
    ... some gets &amp; sets methods
  }
  </code></pre>
  
  <p>City.java</p>
  
  <pre><code>@Entity
  public class City implements Serializable {
    private static final long serialVersionUID = 1L;
    private String cityName;    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    private Integer cityId;
    public String getCityName() {
        return cityName;
    }
    public void setCityName(String newcityName) {
        this.cityName = newcityName;
    }    
    public City(String ctname) {
        this.cityName = ctname;
    }
    public Integer getcityId() {
        return cityId;
    }
    public void setcityId(Integer ctId) {
        this.cityId = ctId;
    }
  
  }
  </code></pre>
  
  <p>When i want to add new pair of NATS &amp; City, i use this :</p>
  
  <p>somebean.java</p>
  
  <pre><code>@Stateless
  public class somebean {
    @PersistenceContext(unitName = "TestLocal")
    private EntityManager em;
  
    public NATs insertNewCC(String capitalname, String countryname){
      City newcapital = new City(capitalname );
      NATS newcountry = new NATS();
      newcountry.setNationName(countryname);
      newcountry.setCapital(newcapital);
      em.persist(newcountry); // both objects persisted well, with "capId" still null
      return newcountry;
    }
  
    public void updateCapitalId(Nations country){
      country.setCapitalId(country.getCapital().getcityId());
      em.merge(country);
    }
  }
  </code></pre>
  
  <p>Here is the service:</p>
  
  <p>genericResource.java</p>
  
  <pre><code>@Path("generic")
  public class GenericResource {
  
  @Context
  private UriInfo context;
  @EJB
  private somebean r;
  
  @GET
  @Path("/inscountry")
  @Produces("application/json")
  public List&lt;NATS&gt; insCountry( @QueryParam("countryname") String countryname, @QueryParam("capitalname") String capitalname){      
      NATS newcountry = r.insertNewCC(capitalname, countryname);
      //r.updateCapitalId(newcountry);   &lt;-- i want to avoid using this line
      List&lt;NATS&gt; result= r.getListNATS();
      return result;
  }
  </code></pre>
  
  <p>When i comment the line : r.updateCapitalId(newcountry);
  i get a pair of country and capital city with a relation displayed correctly in JSON, but when the session closes. it losses the relation because the foreign key is not saved. the capId in NATs entity is null after persist completed. So i need 1 persist &amp; 1 merge to complete this. is there a better solution beside using that line i commented? 
  Thank you.</p>