Created
October 25, 2017 02:01
-
-
Save BrunoDSouza/c1fc2939d7ed060264c344740785d1e9 to your computer and use it in GitHub Desktop.
Relationship @onetomany and @manytoone
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 com.zieg.petcare.model; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.EnumType; | |
import javax.persistence.Enumerated; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.OneToMany; | |
import javax.persistence.Table; | |
import javax.validation.constraints.NotNull; | |
import javax.validation.constraints.Size; | |
import org.hibernate.annotations.Cascade; | |
import org.hibernate.annotations.CascadeType; | |
import org.hibernate.validator.constraints.NotBlank; | |
import org.hibernate.validator.constraints.br.CNPJ; | |
import org.springframework.format.annotation.NumberFormat; | |
import com.zieg.petcare.constraint.fornecedores.FornecedorConstraint; | |
import com.zieg.petcare.model.enums.TipoStatus; | |
/** | |
* @author Bruno D. Souza | |
* | |
*/ | |
@Entity | |
@Table(name="fornecedor") | |
@FornecedorConstraint | |
public class Fornecedor { | |
@Id | |
@Column(name="idfornecedor") | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Long codigo; | |
@NotBlank | |
@Column(name="nome") | |
@Size(message="O nome deve possuir entre 6 e 100 caracteres!", max = 100, min = 6) | |
private String nome; | |
@Column(name="cnpj") | |
@NumberFormat(pattern = "##.###.###/####-##00.000.000/0000-00") | |
@CNPJ(message="CNPJ é inválido!") | |
private String cnpj; | |
@NotNull | |
@Enumerated(EnumType.ORDINAL) | |
private TipoStatus cod_status = TipoStatus.ATIVADO; | |
@OneToMany(mappedBy="fornecedor", orphanRemoval=false) | |
@Cascade(CascadeType.ALL) | |
private List<Produto> produtos = new ArrayList<Produto>(); | |
/*Getters and Setters*/ | |
} |
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 com.zieg.petcare.model; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.EnumType; | |
import javax.persistence.Enumerated; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.JoinColumn; | |
import javax.persistence.ManyToOne; | |
import javax.persistence.OneToMany; | |
import javax.persistence.Table; | |
import javax.persistence.Transient; | |
import javax.validation.constraints.Min; | |
import javax.validation.constraints.NotNull; | |
import javax.validation.constraints.Size; | |
import org.hibernate.validator.constraints.NotBlank; | |
import org.springframework.format.annotation.NumberFormat; | |
import com.zieg.petcare.constraint.produtos.ProductConstraint; | |
import com.zieg.petcare.model.enums.TipoMedida; | |
import com.zieg.petcare.model.enums.TipoStatus; | |
/** | |
* @author Bruno D. Souza | |
* | |
*/ | |
@Entity | |
@Table(name="produto") | |
@ProductConstraint | |
public class Produto { | |
/** @Membros Privados **/ | |
@Id | |
@Column(name="idproduto", nullable = false) | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Long codigo; | |
@NotNull | |
@JoinColumn(name="cod_tipo", nullable = false) | |
@ManyToOne | |
private TipoProduto tipoproduto; | |
@NotNull | |
@ManyToOne | |
@JoinColumn(name="cod_fornecedor", nullable = false) | |
private Fornecedor fornecedor; | |
@NotBlank | |
@Column(name="descricao") | |
@Size(message="O tamanho do nome deve possuir no máximo 100 caracteres!", max = 100) | |
private String nome; | |
@Column(name="medida") | |
@NotNull | |
@Enumerated(EnumType.ORDINAL) | |
private TipoMedida medida; | |
@Column(name="vl_compra") | |
@NotNull | |
@NumberFormat(pattern = "#,##0.00") | |
@Min(message="O Valor de compra deve ser maior que R$0,00!", value = 1) | |
private Double vl_compra; | |
@Column(name="vl_venda") | |
@NotNull | |
@NumberFormat(pattern = "#,##0.00") | |
@Min(message="O Valor de venda deve ser maior que R$0,00", value = 1) | |
private Double vl_venda; | |
@Column(name="status_produto") | |
@NotNull | |
@Enumerated(EnumType.ORDINAL) | |
private TipoStatus status_produto = TipoStatus.ATIVADO; | |
/*Propriedades não mapeadas*/ | |
@Transient private Long qtd_estoque; | |
@Transient private Long estoque_min; | |
@Transient private double percentual; | |
@OneToMany(mappedBy="produto") | |
private List<Movimentacao> movimentacoes = new ArrayList<Movimentacao>(); | |
/** @Getter e @Setters **/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment