0
Hello, I have a relationship ManyToMany
amid envio
and carro
, and would like to understand how do I save in the same table, which is created through this relationship, the entity id Local
.
Entidade
Shipping ( through this relationship is created a table called ship_car, with car id and id
of sending.)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idEnvio")
private Integer id;
@Column(name = "dataEnvio")
@Temporal(javax.persistence.TemporalType.DATE)
private Date dataEnvio;
@Column(name = "dataRecebimentoLocal")
@Temporal(javax.persistence.TemporalType.DATE)
private Date dataRecebimentoLocal;
@Column(name = "dataDevolucao")
@Temporal(javax.persistence.TemporalType.DATE)
private Date dataDevolucao;
@ManyToOne
@JoinColumn(name = "envioCarro", referencedColumnName = "idCarro")
private Carro carro;
@ManyToOne
@JoinColumn(name = "envioLocall", referencedColumnName = "idLocal")
private Locall locall;
@ManyToMany
@JoinTable(name = "envio_carro", joinColumns
= @JoinColumn(name = "envio_id"),
inverseJoinColumns
= @JoinColumn(name = "carro_id"))
private List<Carro> carros;
Entidade
Car
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idCarro")
private Integer id;
@Column(name = "numeroCarro")
@ManyToMany(mappedBy = "carros")
private List<Envio> envios;
Entidade
Local
@Entity
@DynamicUpdate(value=true)
@NamedQueries({
@NamedQuery(name = “Locall.findAll”, query = “SELECT distinct s FROM Locall s”),
@NamedQuery(name = “Locall.findByLocallId”, query = “SELECT s FROM Locall s WHERE s.id = :id”),
@NamedQuery(name = “Locall.findByLocallDescr”, query = “SELECT s FROM Locall s WHERE s.descricao = :descricao”)})
public class Locall implements Serializable, EntidadeBase {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idLocal")
private Integer id;
@Column(name = "descricaoLocal")
private String descricao;
@OneToMany(mappedBy = "carro")
private List<Envio> envios;
I managed through the examples right here, to create the ManyToMany
, the doubt now is: is possible ( or still , is correct) in this table ( originated by ManyToMany
) include the id
of Local
, for I have several carros
, being sent to several locais
and each car is linked to a envio
, or I have to change the approach?
If you already have the
local
Inside shipping, wouldn’t you better pick him up? If you have a relationship manyToMany then you have a car and shipping, in this relationship you can get the shipment and then the location of the same– DiegoAugusto
I may have made a mistake in relationships so far, because I make one
envio
for alocal
. However later users can "redirect" thecarro
sent from this firstlocal
, to a newlocal
. When I create theenvio
all goes to the same place– Rodrigo
From what’s described, when he redirects to another location, it would be like editing the local attribute, correct? If so, what would be the cost of creating an associative entity between these entities? Who knows an entity
DestinoEnvioCarro
maybe. Then it would be centered on a point only your control on shipping.– Weslley Tavares