gpt4 book ai didi

java - Eclipse JPA 添加新的父对象

转载 作者:行者123 更新时间:2023-11-30 06:27:43 25 4
gpt4 key购买 nike

我正在尝试使用 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
}

城市.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 和 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;
}

当我评论这一行时:r.updateCapitalId(newcountry);我得到一对国家和首都,并且在 JSON 中正确显示了关系,但是当 session 关闭时。由于未保存外键,它会丢失关系。持久完成后,NAT 实体中的 capId 为空。所以我需要 1 次持久化和 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);

关于java - Eclipse JPA 添加新的父对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46782623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com