2017-10-17 105 views
0

我試圖用Netbean構建一個應用程序。我使用Eclipse IDE & JPA API。該實體是這樣的:Ecplise JPA添加新的父對象

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爲空。所以我需要1個堅持& 1合併來完成這一點。除了使用我評論的那條線之外,還有更好的解決方案嗎? 謝謝。

+0

你確定這是你的代碼..?它看起來像它甚至不會編譯和嵌套實體是一件非常糟糕的事情,難道你不能爲每個實體分開類嗎? –

+0

它來自不同的.java文件。我實際上將每個實體寫入不同的文件。 –

回答

0

重新設計你的實體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); 
+0

如果您觀察,我已經從NATS.java中刪除了cap_id顯式變量並更改了註釋,它將按照您的預期工作。 – Tejas

+0

還需要爲City.java添加一個默認構造函數。 – Tejas

+0

謝謝。這是我一直在尋找的。 –

0

首先,嘗試使用縮進和格式化程序,因爲您的代碼很混亂。其次,您想要保存兩個實體(NATS和國家/地區),因此您必須取消註釋更新城市的行或逐個保留。如果你正在開發一個新的應用程序,我建議你使用Spring引導,並且它是SpringRepository接口來使這些事情變得更簡單和清晰。

+1

感謝您的回答。我在NATS實體上添加了標識,這是我發現需要更好標識的唯一部分。我假設你建議我保留「更新capitalId」部分。我非常感謝你的回答。 :) –