Ecplise JPA添加新的父对象

时间:2017-10-17 04:43:51

标签: java eclipse jpa entity-relationship cascade

我正在尝试使用Netbean构建应用程序。我使用Eclipse IDE& JPA API。实体是这样的:

NATS.java

@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 & sets methods
}

City.java

@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;
  }

}

当我想添加一对新的NATS&城市,我用这个:

somebean.java

@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);
  }
}

这是服务:

genericResource.java

@Path("generic")
public class GenericResource {

@Context
private UriInfo context;
@EJB
private somebean r;

@GET
@Path("/inscountry")
@Produces("application/json")
public List<NATS> insCountry( @QueryParam("countryname") String countryname, @QueryParam("capitalname") String capitalname){      
    NATS newcountry = r.insertNewCC(capitalname, countryname);
    //r.updateCapitalId(newcountry);   <-- i want to avoid using this line
    List<NATS> result= r.getListNATS();
    return result;
}

当我评论该行时:r.updateCapitalId(newcountry); 我得到一对国家和首都城市,其关系在JSON中正确显示,但会议结束时。它会丢失关系因为没有保存外键。持久化完成后,NAT实体中的capId为null。所以我需要1个坚持&amp; 1合并完成此操作。有没有更好的解决方案旁边使用我评论的那条线? 谢谢。

2 个答案:

答案 0 :(得分:1)

像这样重新设计您的实体NATS:

package com;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "NATS")

public class NATS
    implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer code;
    private String name;
    // for foreign key purpose
    @OneToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "cap_id")
    private City capital;

    public City getCapital()
    {
        return this.capital;
    }

    public void setCapital(City newcapital)
    {
        this.capital = newcapital;
    }

    public Integer getCode()
    {
        return this.code;
    }

    public void setCode(Integer code)
    {
        this.code = code;
    }

    public String getName()
    {
        return this.name;
    }

    public void setName(String name)
    {
        this.name = name;
    }
}

然后执行代码将保持不变:

    City newcapital = new City(capitalname);
    NATS newcountry = new NATS();
    newcountry.setName(countryname);
    newcountry.setCapital(newcapital);
    this.entityManager.persist(newcountry);

答案 1 :(得分:0)

首先,尝试使用缩进和格式化程序,因为您的代码很乱。其次,您要保存两个实体(NATS和国家/地区),因此您必须取消注释更新城市的行或逐个保留的行。如果你正在开始一个新的应用程序,我建议你使用Spring引导程序和它的SpringRepository接口来使这些事情变得更容易和清晰。