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);
}