References lost at the time of persistence

0

Below are my entities and relationships:

Order:

@Entity
@Table(name = "encomenda")
@XmlRootElement(name = "objeto")
@XmlAccessorType(XmlAccessType.FIELD)
public class Encomenda {

    @Getter
    @Setter
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Getter
    @Setter
    @Pattern(regexp = "[A-z]{2}\d{9}[A-z]{2}", message = "O código não é válido")
    @Size(min = 13, max = 13, message = "É permitido 13 caracteres")
    @Column(nullable = false, length = 13)
    @NotBlank(message = "Nome não pode estar em branco")
    @XmlElement(name = "numero")
    private String codigo;

    @Getter
    @Setter
    @XmlElement(name = "evento")
    @OneToMany(mappedBy = "encomenda", targetEntity = Evento.class, cascade = CascadeType.ALL)
    private Collection<Evento> eventos;

    @Getter
    @Setter
    @Column
    private String loja;

    @Getter
    @Setter
    @XmlElement
    @Column
    private String erro;

Event:

@EqualsAndHashCode
@Entity
@Table(name = "evento")
@XmlRootElement(name="evento")
@XmlAccessorType(XmlAccessType.FIELD)
public class Evento {

    @Getter
    @Setter
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Getter
    @Setter
    @Column
    @XmlElement(name="data")
    private String dataOcorrencia;

    @Getter
    @Setter
    @Column
    @XmlElement(name="hora")
    private String horaOcorrencia;

    @Getter
    @Setter
    @Column
    @XmlElement
    private String descricao;

    @Getter
    @Setter
    @Column
    @XmlElement
    private String local;

    @Getter
    @Setter
    @Column
    @XmlElement
    private String cidade;

    @Getter
    @Setter
    @Column
    @XmlElement
    private String uf;

    @Getter
    @Setter
    @OneToOne(mappedBy="evento",cascade = CascadeType.ALL)
    @XmlElement(name="destino")
    private Destino destino;

    @Getter
    @Setter
    @ManyToOne
    private Encomenda encomenda;

In the database I have the tables event and order . In the event table I have order_id which is FK. The big problem is that when persisting the order object is generated its id normally, just as in the event table the id is generated, however the order_id field is null. Does anyone know how I can resolve?

The object is populated by an xml, the controller looks like this:

@ManagedBean
@ViewScoped
public class EncomendaController {

    @Getter
    @Setter
    private Encomenda encomenda;

    @Getter
    @Setter
    private Collection<Encomenda> objetos;

    @Inject
    private EncomendaService service;

    @PostConstruct
    public void novo() {
        encomenda = new Encomenda();
    }

    public void salvar() throws IOException, SOAPException {
        if (encomenda.getId() == null) {
            try {
                popularObjeto(encomenda.getCodigo());
                service.insert(encomenda);
                JsfUtils.addInfoMessage("encomenda.cadastrada");
                encomenda = new Encomenda();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            service.update(encomenda);
        }
    }

    private Encomenda popularObjeto(String codigo) throws IOException, SOAPException, JAXBException {
        String xml = RoboCorreios.consultarCodigo(codigo);

        JAXBContext context = JAXBContext.newInstance(Encomenda.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Encomenda objeto = (Encomenda) unmarshaller.unmarshal(new StringReader(xml));
        final boolean objetoExiste = objeto.getErro() == null ? true : false;

        if (objetoExiste) {
            setEncomenda(objeto);
            return encomenda;
        } else {
            encomenda.setErro("Objeto não localizado");
            return encomenda;
        }
    }

}

My Service:

@Stateless
@LocalBean
public class EncomendaService extends ServiceBase {

    public void insert(Encomenda objeto) {
        this.em.persist(objeto);
    }

    public void update(Encomenda objeto) {
        this.em.merge(objeto);
    }
    
asked by anonymous 22.03.2017 / 13:11

1 answer

2

The problem is that you did not associate the order reference in the event list. One way to do this is to add a foreach to load the object into each item in the event list:

public void salvar() throws IOException, SOAPException {
        if (encomenda.getId() == null) {
            try {
                popularObjeto(encomenda.getCodigo());

                for (Evento evento : encomenda.getEventos()) {
                    evento.setEncomenda(encomenda);
                }
                service.insert(encomenda);
                JsfUtils.addInfoMessage("encomenda.cadastrada");
                encomenda = new Encomenda();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            service.update(encomenda);
        }
    }

With this change the same order created will be associated with each of the events.

    
22.03.2017 / 15:12