Created
January 3, 2024 14:45
-
-
Save giovannicandido/9e41c25385773b8219f0cb77cc5066f3 to your computer and use it in GitHub Desktop.
Exemplo de @manytomany usando uma classe customizada para tabela central
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import jakarta.persistence.Column; | |
import jakarta.persistence.Entity; | |
import jakarta.persistence.GeneratedValue; | |
import jakarta.persistence.GenerationType; | |
import jakarta.persistence.Id; | |
import java.time.LocalDateTime; | |
@Entity | |
public class Atendimento { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Integer id; | |
@Column(length = 60) | |
private String nomeAtendente; | |
private LocalDateTime dataCriacao; | |
public Integer getId() { | |
return id; | |
} | |
public void setId(Integer id) { | |
this.id = id; | |
} | |
public String getNomeAtendente() { | |
return nomeAtendente; | |
} | |
public void setNomeAtendente(String nomeAtendente) { | |
this.nomeAtendente = nomeAtendente; | |
} | |
public LocalDateTime getDataCriacao() { | |
return dataCriacao; | |
} | |
public void setDataCriacao(LocalDateTime dataCriacao) { | |
this.dataCriacao = dataCriacao; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import jakarta.persistence.Column; | |
import jakarta.persistence.EmbeddedId; | |
import jakarta.persistence.Entity; | |
import jakarta.persistence.JoinColumn; | |
import jakarta.persistence.ManyToOne; | |
import jakarta.persistence.MapsId; | |
import java.time.LocalDateTime; | |
@Entity | |
public class AtendimentoProduto { | |
@EmbeddedId | |
private AtendimentoProdutoId id; | |
@ManyToOne | |
@MapsId("produto_id") | |
@JoinColumn(name = "produto_id", referencedColumnName = "id") | |
private Produto produto; | |
@ManyToOne | |
@MapsId("atendimento_id") | |
@JoinColumn(name = "atendimento_id", referencedColumnName = "id") | |
private Atendimento atendimento; | |
@Column(nullable = false) | |
private LocalDateTime dataEntrada; | |
@Column(nullable = false) | |
private Integer quantidade; | |
public AtendimentoProdutoId getId() { | |
return id; | |
} | |
public void setId(AtendimentoProdutoId id) { | |
this.id = id; | |
} | |
public Produto getProduto() { | |
return produto; | |
} | |
public void setProduto(Produto produto) { | |
this.produto = produto; | |
} | |
public Atendimento getAtendimento() { | |
return atendimento; | |
} | |
public void setAtendimento(Atendimento atendimento) { | |
this.atendimento = atendimento; | |
} | |
public LocalDateTime getDataEntrada() { | |
return dataEntrada; | |
} | |
public void setDataEntrada(LocalDateTime dataEntrada) { | |
this.dataEntrada = dataEntrada; | |
} | |
public Integer getQuantidade() { | |
return quantidade; | |
} | |
public void setQuantidade(Integer quantidade) { | |
this.quantidade = quantidade; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import jakarta.persistence.Column; | |
import jakarta.persistence.Embeddable; | |
import java.io.Serializable; | |
@Embeddable | |
public class AtendimentoProdutoId implements Serializable { | |
@Column(name = "produto_id") | |
private Integer produtoId; | |
@Column(name = "atendimento_id") | |
private Integer atendimentoId; | |
public Integer getProdutoId() { | |
return produtoId; | |
} | |
public void setProdutoId(Integer produtoId) { | |
this.produtoId = produtoId; | |
} | |
public Integer getAtendimentoId() { | |
return atendimentoId; | |
} | |
public void setAtendimentoId(Integer atendimentoId) { | |
this.atendimentoId = atendimentoId; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import jakarta.persistence.Column; | |
import jakarta.persistence.Entity; | |
import jakarta.persistence.GeneratedValue; | |
import jakarta.persistence.GenerationType; | |
import jakarta.persistence.Id; | |
@Entity | |
public class Produto { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Integer id; | |
@Column(length = 50) | |
private String nome; | |
public Integer getId() { | |
return id; | |
} | |
public void setId(Integer id) { | |
this.id = id; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment