[英]TransientPropertyValueException in unidirectional ManyToOne relationship
在閱讀其他主題后,進行了一些更改后,出現此錯誤。 例如,實體Neighbourhood具有@ManyToOne(optional = false, fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
注釋的實體City @ManyToOne(optional = false, fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
。
代碼例如:
@PostConstruct
public void init() {
this.enterprise = new Enterprise();
this.country = new Country();
this.state = new State();
this.city = new City();
this.zipCode = new ZipCode();
this.street = new Street();
this.neighbourhood = new Neighbourhood();
}
public void save() {
if (this.enterprise.getId() == null) {
Country cou = this.countryBean.findByName("Brazil").get(0);
this.state.setCountry(cou);
this.city.setState(this.state);
this.neighbourhood.setZipCode(this.zipCode);
this.neighbourhood.setCity(this.city);
this.street.setNeighbourhood(this.neighbourhood);
this.enterprise.setCountry(cou);
this.enterprise.setState(this.state);
this.enterprise.setCity(this.city);
this.enterprise.setZipCode(this.zipCode);
this.enterprise.setNeighbourhood(this.neighbourhood);
this.enterprise.setStreet(this.street);
this.enterpriseBean.add(this.enterprise);
} else {
this.neighbourhood = this.neighbourhoodBean.findById(this.enterprise.getNeighbourhood().getId());
this.city = this.cityBean.findById(this.enterprise.getCity().getId());
this.enterpriseBean.edit(this.enterprise);
}
this.enterprise = new Enterprise();
}
然后,我得到TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : br.com.shelfpix.shelfboard.entities.Enterprise.street -> br.com.shelfpix.shelfboard.entities.Street
和IllegalStateException
與此this.enterpriseBean.add(this.enterprise);
編輯1:添加了以下代碼。
if (streetBean.searchStreetByName(this.street.getName())) {
streetBean.edit(street);
} else {
streetBean.add(street);
}
現在,它拋出PersistentObjectException: detached entity passed to persist: br.com.shelfpix.shelfboard.entities.Country
在搜索現有行之后,通過設置托管bean對象來使其工作。
完整方法如下:
public void save() {
if (this.enterprise.getId() == null) {
this.country = this.countryBean.findByName("Brazil").get(0);
if (this.stateBean.searchStateByAbbreviation(this.state.getAbbreviation())) {
Map<String, Object> map = new HashMap<>();
map.put("abbreviation", this.state.getAbbreviation());
State sta = (State) this.stateBean.findWithNamedQuery("State.findByAbbreviation", map).get(0);
this.state = sta;
} else {
this.state.setCountry(this.country);
this.stateBean.add(this.state);
}
this.city.setState(this.state);
if (this.cityBean.searchCityByName(this.city.getName())) {
Map<String, Object> map = new HashMap<>();
map.put("name", this.city.getName());
City cit = (City) this.cityBean.findWithNamedQuery("City.findByNome", map).get(0);
this.city = cit;
} else {
this.cityBean.add(this.city);
}
if (this.zipCodeBean.searchZipCodeByNumber(this.zipCode.getNumber())) {
Map<String, Object> map = new HashMap<>();
map.put("number", this.zipCode.getNumber());
ZipCode zip = (ZipCode) this.zipCodeBean.findWithNamedQuery("ZipCode.findByNumber", map).get(0);
this.zipCode = zip;
} else {
this.zipCodeBean.add(this.zipCode);
}
this.neighbourhood.setCity(this.city);
this.neighbourhood.setZipCode(this.zipCode);
if (this.neighbourhoodBean.searchNeighbourhoodByName(this.neighbourhood.getName())) {
Map<String, Object> map = new HashMap<>();
map.put("nome", this.neighbourhood.getName());
Neighbourhood ngb = (Neighbourhood) this.bairroBean.findWithNamedQuery("Neighbourhood.findByName", map).get(0);
this.neighbourhood = ngb;
} else {
this.neighbourhoodBean.add(this.neighbourhood);
}
this.street.setNeighbourhood(this.neighbourhood);
if (this.streetBean.searchStreetByName(this.street.getName())) {
Map<String, Object> map = new HashMap<>();
map.put("name", this.street.getNome());
Street str = (Street) this.streetBean.findWithNamedQuery("Street.findByNome", map).get(0);
this.street = str;
} else {
this.streetBean.add(this.street);
}
this.enterprise.setCountry(this.country);
this.enterprise.setState(this.state);
this.enterprise.setCity(this.city);
this.enterprise.setZipCode(this.zipCode);
this.enterprise.setNeighbourhood(this.neighbourhood);
this.enterprise.setStreet(this.street);
this.enterpriseBean.add(this.enterprise);
} else {
this.country = this.countryBean.findByEnterpriseId(this.enterprise.getId());
this.state = this.stateBean.findByEnterpriseId(this.enterprise.getId());
this.city = this.cityBean.findByEnterpriseId(this.enterprise.getId());
this.zipCode = this.zipCodeBean.findByEnterpriseId(this.enterprise.getId());
this.neighbourhood = this.neighbourhoodBean.findByEnterpriseId(this.enterprise.getId());
this.street = this.streetBean.findByEnterpriseId(this.enterprise.getId());
this.enterpriseBean.edit(this.enterprise);
}
this.enterprise = new Enterprise();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.