Java Ecplise JPA添加新的父对象
我正在尝试用Netbean构建一个应用程序。我使用EclipseIDE和JPAAPI。实体如下所示: javaJava Ecplise JPA添加新的父对象,java,eclipse,jpa,entity-relationship,cascade,Java,Eclipse,Jpa,Entity Relationship,Cascade,我正在尝试用Netbean构建一个应用程序。我使用EclipseIDE和JPAAPI。实体如下所示: java @Entity @Table(name = "NATS") Public Class NATS implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private
@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;
}
}
当我想添加一对新的NAT&City时,我使用以下方法:
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;
}
@Path(“通用”)
公共类通用资源{
@上下文
私有信息上下文;
@EJB
二等兵;
@得到
@路径(“/inscountry”)
@生成(“应用程序/json”)
公共列表insCountry(@QueryParam(“countryname”)字符串countryname,@QueryParam(“capitalname”)字符串capitalname){
NATS newcountry=r.insertNewCC(大写名称,国家名称);
//r、 updateCapitalId(newcountry);首先,尝试使用缩进和格式化程序,因为您的代码乱七八糟。其次,您希望保存两个实体(NAT和Country)因此,您必须逐个取消注释更新城市或持久化的行。如果您正在启动一个新应用程序,我建议您使用Spring boot及其SpringRepository界面,使这些内容更简单、更清晰。重新设计实体NAT,如下所示:
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);
你确定这是你的代码吗?看起来它甚至不会编译&嵌套实体是一件非常糟糕的事情,你不能为每个实体分别创建类吗?它引用了不同的.java文件。我实际上在不同的文件中编写每个实体。谢谢你的回答。我在NAT实体上添加了标识,这是我发现的唯一部分这需要更好的识别。我想你建议我保留“update capitalId”部分。我真的很感谢你的回答。:)如果你注意到了,我已经从NATS.java和change annotation中删除了cap_id显式变量,它将按照你的期望工作。还可以根据需要向City.java添加一个默认构造函数。谢谢。这就是我一直在寻找的。