单向ManyToOne关系中的TransientPropertyValueException

时间:2014-08-13 01:29:13

标签: java jpa

在阅读完其他主题后,我在做了一些更改后得到了这个错误。例如,实体邻域具有用@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

IllegalStateExceptionthis.enterpriseBean.add(this.enterprise);

一致if (streetBean.searchStreetByName(this.street.getName())) { streetBean.edit(street); } else { streetBean.add(street); }

编辑1: 添加了以下代码。

PersistentObjectException: detached entity passed to persist: br.com.shelfpix.shelfboard.entities.Country

现在它正在投掷{{1}}

1 个答案:

答案 0 :(得分:0)

在搜索现有行之后,设置托管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();

}