Created
December 6, 2012 17:09
-
-
Save sednem/4226163 to your computer and use it in GitHub Desktop.
Classes mapeada com JPA
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 br.ufpe.nti.entity; | |
import java.io.Serializable; | |
import java.util.List; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.ManyToMany; | |
import javax.persistence.Table; | |
import javax.validation.constraints.Size; | |
@Entity | |
@Table(name="autor") | |
public class Autor implements Serializable { | |
private static final long serialVersionUID = 1L; | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private long id; | |
@Size(min=1,message="{validation.campo.obrigatorio}") | |
private String nome; | |
@ManyToMany(mappedBy="autores") | |
private List<Livro> livros; | |
public Autor(){} | |
@Override | |
public boolean equals(Object obj) { | |
return toString().equals(obj.toString()); | |
} | |
@Override | |
public int hashCode() { | |
return toString().hashCode(); | |
} | |
@Override | |
public String toString() { | |
return this.id + ":" + this.nome; | |
} | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public List<Livro> getLivros() { | |
return livros; | |
} | |
public void setLivros(List<Livro> livros) { | |
this.livros = livros; | |
} | |
} |
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 br.ufpe.nti.entity; | |
import java.io.Serializable; | |
import java.util.Date; | |
import java.util.List; | |
import javax.persistence.CascadeType; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
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.Temporal; | |
import javax.persistence.TemporalType; | |
@Entity | |
@Table(name="compra") | |
public class Compra implements Serializable { | |
private static final long serialVersionUID = 1L; | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private long id; | |
@ManyToOne | |
@JoinColumn(name="ID_USUARIO", nullable=false) | |
private Usuario usuario; | |
@Temporal(TemporalType.TIMESTAMP) | |
@Column(name="DATA_COMPRA") | |
private Date dataCompra; | |
@OneToMany(mappedBy="compra", cascade=CascadeType.ALL) | |
private List<ItemCompra> itensCompra; | |
public Compra(){ | |
} | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public Usuario getUsuario() { | |
return usuario; | |
} | |
public void setUsuario(Usuario usuario) { | |
this.usuario = usuario; | |
} | |
public Date getDataCompra() { | |
return dataCompra; | |
} | |
public void setDataCompra(Date dataCompra) { | |
this.dataCompra = dataCompra; | |
} | |
public List<ItemCompra> getItensCompra() { | |
return itensCompra; | |
} | |
public void setItensCompra(List<ItemCompra> itensCompra) { | |
this.itensCompra = itensCompra; | |
} | |
} |
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 br.ufpe.nti.entity; | |
import java.io.Serializable; | |
import java.util.List; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.OneToMany; | |
import javax.persistence.Table; | |
@Entity | |
@Table(name="editora") | |
public class Editora implements Serializable { | |
private static final long serialVersionUID = 1L; | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private long id; | |
private String nome; | |
@OneToMany(mappedBy="editora") | |
private List<Livro> livros; | |
public Editora(){} | |
@Override | |
public boolean equals(Object obj) { | |
return toString().equals(obj.toString()); | |
} | |
@Override | |
public int hashCode() { | |
return toString().hashCode(); | |
} | |
@Override | |
public String toString() { | |
return this.id + ":" + this.nome; | |
} | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public List<Livro> getLivros() { | |
return livros; | |
} | |
public void setLivros(List<Livro> livros) { | |
this.livros = livros; | |
} | |
} |
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 br.ufpe.nti.entity; | |
import java.io.Serializable; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.OneToOne; | |
import javax.persistence.Table; | |
@Entity | |
@Table(name="endereco") | |
public class Endereco implements Serializable{ | |
private static final long serialVersionUID = 4284567237862794696L; | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private long id; | |
private String logradouro; | |
@OneToOne(mappedBy="endereco") | |
private Pessoa pessoa; | |
public Endereco() {} | |
@Override | |
public boolean equals(Object obj) { | |
return toString().equals(obj.toString()); | |
} | |
@Override | |
public int hashCode() { | |
return toString().hashCode(); | |
} | |
@Override | |
public String toString() { | |
return this.id + ":" + this.logradouro; | |
} | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public String getLogradouro() { | |
return logradouro; | |
} | |
public void setLogradouro(String logradouro) { | |
this.logradouro = logradouro; | |
} | |
public Pessoa getPessoa() { | |
return pessoa; | |
} | |
public void setPessoa(Pessoa pessoa) { | |
this.pessoa = pessoa; | |
} | |
} |
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 br.ufpe.nti.entity; | |
import java.io.Serializable; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.IdClass; | |
import javax.persistence.JoinColumn; | |
import javax.persistence.ManyToOne; | |
import javax.persistence.Table; | |
@Entity | |
@Table(name="item_compra") | |
@IdClass(ItemCompraPK.class) | |
public class ItemCompra implements Serializable { | |
private static final long serialVersionUID = 1L; | |
@Id | |
@ManyToOne | |
@JoinColumn(name="ID_COMPRA") | |
private Compra compra; | |
@Id | |
@ManyToOne | |
@JoinColumn(name="ID_LIVRO") | |
private Livro livro; | |
@Column(name="QUANTIDADE") | |
private int quantidade; | |
public ItemCompra(){ | |
} | |
public Compra getCompra() { | |
return compra; | |
} | |
public void setCompra(Compra compra) { | |
this.compra = compra; | |
} | |
public Livro getLivro() { | |
return livro; | |
} | |
public void setLivro(Livro livro) { | |
this.livro = livro; | |
} | |
public int getQuantidade() { | |
return quantidade; | |
} | |
public void setQuantidade(int 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 br.ufpe.nti.entity; | |
import java.io.Serializable; | |
import javax.persistence.Column; | |
import javax.persistence.Embeddable; | |
@Embeddable | |
public class ItemCompraPK implements Serializable { | |
private static final long serialVersionUID = 1L; | |
@Column | |
private long livro; | |
@Column | |
private long compra; | |
public ItemCompraPK(){ | |
} | |
@Override | |
public boolean equals(Object obj) { | |
return this.hashCode() == obj.hashCode(); | |
} | |
@Override | |
public int hashCode() { | |
String hash = | |
this.getCompra()+":"+this.getLivro(); | |
return hash.hashCode(); | |
} | |
public long getLivro() { | |
return livro; | |
} | |
public void setLivro(long livro) { | |
this.livro = livro; | |
} | |
public long getCompra() { | |
return compra; | |
} | |
public void setCompra(long compra) { | |
this.compra = compra; | |
} | |
} |
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 br.ufpe.nti.entity; | |
import java.io.Serializable; | |
import java.util.Date; | |
import java.util.List; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.JoinColumn; | |
import javax.persistence.JoinTable; | |
import javax.persistence.ManyToMany; | |
import javax.persistence.ManyToOne; | |
import javax.persistence.Table; | |
import javax.persistence.Temporal; | |
import javax.persistence.TemporalType; | |
import javax.validation.constraints.NotNull; | |
import javax.validation.constraints.Size; | |
import org.hibernate.validator.constraints.Range; | |
import br.ufpe.nti.entity.beanValidation.ISBN; | |
@Entity | |
@Table(name="livro") | |
public class Livro implements Serializable { | |
private static final long serialVersionUID = 1L; | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private long id; | |
@ISBN | |
private String isbn; //Deve conter 13 dígitos. | |
@NotNull(message="{validation.campo.obrigatorio}") | |
@ManyToMany | |
@JoinTable( name = "livro_autor", | |
joinColumns = {@JoinColumn(name = "id_livro")}, | |
inverseJoinColumns = {@JoinColumn(name = "id_autor")}) | |
private List<Autor> autores; | |
@ManyToOne | |
@JoinColumn(name="ID_EDITORA", nullable=false) | |
@NotNull(message="{validation.campo.obrigatorio}") | |
private Editora editora; | |
@Size(min=1,message="{validation.campo.obrigatorio}") | |
private String titulo; | |
@NotNull(message="{validation.campo.obrigatorio}") | |
@Range(min=0L, max=999L, | |
message="O valor informado deve ser entre 0 e 999") | |
private Float preco; | |
@Temporal(value=TemporalType.DATE) | |
@NotNull(message="{validation.campo.obrigatorio}") | |
private Date dataPublicacao; | |
public Livro(){} | |
@Override | |
public boolean equals(Object obj) { | |
return toString().equals(obj.toString()); | |
} | |
@Override | |
public int hashCode() { | |
return toString().hashCode(); | |
} | |
@Override | |
public String toString() { | |
return this.id + ":" + this.isbn | |
+ ":" + this.titulo | |
+ ":" + this.preco | |
+ ":" + this.dataPublicacao | |
+ ":{" + this.autores + "}" | |
+ ":{" + this.editora + "}"; | |
} | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public String getIsbn() { | |
return isbn; | |
} | |
public void setIsbn(String isbn) { | |
this.isbn = isbn; | |
} | |
public List<Autor> getAutores() { | |
return autores; | |
} | |
public void setAutores(List<Autor> autores) { | |
this.autores = autores; | |
} | |
public Editora getEditora() { | |
return editora; | |
} | |
public void setEditora(Editora editora) { | |
this.editora = editora; | |
} | |
public String getTitulo() { | |
return titulo; | |
} | |
public void setTitulo(String titulo) { | |
this.titulo = titulo; | |
} | |
public Float getPreco() { | |
return preco; | |
} | |
public void setPreco(Float preco) { | |
this.preco = preco; | |
} | |
public Date getDataPublicacao() { | |
return dataPublicacao; | |
} | |
public void setDataPublicacao(Date dataPublicacao) { | |
this.dataPublicacao = dataPublicacao; | |
} | |
} |
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 br.ufpe.nti.entity; | |
import java.io.Serializable; | |
import java.util.Date; | |
import javax.persistence.CascadeType; | |
import javax.persistence.DiscriminatorColumn; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.Inheritance; | |
import javax.persistence.InheritanceType; | |
import javax.persistence.JoinColumn; | |
import javax.persistence.OneToOne; | |
import javax.persistence.Table; | |
import javax.persistence.Temporal; | |
import javax.persistence.TemporalType; | |
import javax.validation.constraints.NotNull; | |
import javax.validation.constraints.Pattern; | |
import javax.validation.constraints.Size; | |
@Entity | |
@Table(name="pessoa") | |
@Inheritance(strategy=InheritanceType.SINGLE_TABLE) | |
@DiscriminatorColumn(name="TIPO") | |
public class Pessoa implements Serializable { | |
private static final long serialVersionUID = 1L; | |
@Id | |
@Size(min=1,message="{validation.campo.obrigatorio}") | |
private String cpf; | |
@Size(min=1,message="{validation.campo.obrigatorio}") | |
private String nome; | |
@Pattern(regexp="^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,6})$") | |
private String email; | |
private String sexo; | |
@OneToOne(cascade=CascadeType.ALL) | |
@JoinColumn(name="ID_ENDERECO", nullable=false) | |
//@PrimaryKeyJoinColumn | |
private Endereco endereco; | |
@Temporal(value=TemporalType.DATE) | |
@NotNull(message="{validation.campo.obrigatorio}") | |
private Date dataNascimento; | |
public Pessoa(){} | |
@Override | |
public boolean equals(Object obj) { | |
return toString().equals(obj.toString()); | |
} | |
@Override | |
public int hashCode() { | |
return toString().hashCode(); | |
} | |
@Override | |
public String toString() { | |
return this.cpf + ":" + this.nome | |
+ ":" + this.email | |
+ ":" + this.sexo | |
+ ":" + this.dataNascimento | |
+ ":" + this.endereco; | |
} | |
/* | |
* Getters and Setters | |
*/ | |
public String getCpf() { | |
return cpf; | |
} | |
public void setCpf(String cpf) { | |
this.cpf = cpf; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public String getSexo() { | |
return sexo; | |
} | |
public void setSexo(String sexo) { | |
this.sexo = sexo; | |
} | |
public Date getDataNascimento() { | |
return dataNascimento; | |
} | |
public void setDataNascimento(Date dataNascimento) { | |
this.dataNascimento = dataNascimento; | |
} | |
public Endereco getEndereco() { | |
return endereco; | |
} | |
public void setEndereco(Endereco endereco) { | |
this.endereco = endereco; | |
} | |
} |
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 br.ufpe.nti.entity; | |
import java.util.List; | |
import javax.persistence.Entity; | |
import javax.persistence.OneToMany; | |
import javax.persistence.Table; | |
import javax.validation.constraints.Size; | |
@Entity | |
@Table(name="usuario") | |
public class Usuario extends Pessoa { | |
private static final long serialVersionUID = 1L; | |
@Size(min=1,message="{validation.campo.obrigatorio}") | |
private String login; | |
@Size(min=1,message="{validation.campo.obrigatorio}") | |
private String senha; | |
@OneToMany(mappedBy="usuario") | |
private List<Compra> compras; | |
public Usuario(){} | |
@Override | |
public boolean equals(Object obj) { | |
return toString().equals(obj.toString()); | |
} | |
@Override | |
public int hashCode() { | |
return toString().hashCode(); | |
} | |
@Override | |
public String toString() { | |
return super.toString() | |
+ ":" + this.login; | |
} | |
/* | |
* Getters and Setters | |
*/ | |
public String getLogin() { | |
return login; | |
} | |
public void setLogin(String login) { | |
this.login = login; | |
} | |
public String getSenha() { | |
return senha; | |
} | |
public void setSenha(String senha) { | |
this.senha = senha; | |
} | |
public List<Compra> getCompras() { | |
return compras; | |
} | |
public void setCompras(List<Compra> compras) { | |
this.compras = compras; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment