olá pessoal, estou dando uma olhada no hibernate annotation e gostaria de saber se tem como gerar o banco de dados atraves das anotações. Atualmente eu uso Hibernate + Xdoclet e faz um bom tempo que nao escrevo uma linha de hbm e nem me preoculpo com a construção do banco, faço tudo via ant.
Usando Hibernate Annotation eu teria que criar o banco na mão?
abrigado.
Hibernate Annotation X Xdoclet
2 Respostas
Configura a conexao e executa a fabrica
package com.amigos.fabrica;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class TabelasFactory {
public static void create(AnnotationConfiguration cfg) {
new SchemaExport(cfg).create(false, true);
}
public static void main(String[] args) {
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.addAnnotatedClass(com.amigos.entidades.Amigo.class);
cfg.addAnnotatedClass(com.amigos.entidades.Telefone.class);
create(cfg);
}
}
package com.amigos.entidades;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.LazyToOne;
@Entity
@Table(name="amigos")
public class Amigo implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="cod_amigo")
private Integer codigoAmigo;
@Column(name="dsc_email", length=70, unique=true, nullable=false)
private String email;
@Column(name="dsc_nome", length=40)
private String nome;
@Column(name="dat_nascimento")
private Date nascimento;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="cod_amigo", referencedColumnName="cod_amigo")
private Set<Telefone> telefone;
public Set<Telefone> getTelefone() {
return telefone;
}
public void setTelefone(Set<Telefone> telefone) {
this.telefone = telefone;
}
public void setCodigoAmigo(Integer codigoAmigo) {
this.codigoAmigo = codigoAmigo;
}
public Integer getCodigoAmigo() {
return codigoAmigo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getNascimento() {
return nascimento;
}
public void setNascimento(Date nascimento) {
this.nascimento = nascimento;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((codigoAmigo == null) ? 0 : codigoAmigo.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Amigo other = (Amigo) obj;
if (codigoAmigo == null) {
if (other.codigoAmigo != null)
return false;
} else if (!codigoAmigo.equals(other.codigoAmigo))
return false;
return true;
}
}
package com.amigos.entidades;
import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="telefones")
public class Telefone implements Serializable {
@EmbeddedId
private TelefonePK id;
public TelefonePK getId() {
return id;
}
public void setId(TelefonePK id) {
this.id = id;
}
}
package com.amigos.entidades;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
public class TelefonePK implements Serializable {
@Column(name="nro_telefone")
private Integer telefone;
@ManyToOne
@JoinColumn (name="cod_amigo", referencedColumnName="cod_amigo", insertable=false, updatable=false)
private Amigo amigo;
public Amigo getAmigo() {
return amigo;
}
public void setAmigo(Amigo amigo) {
this.amigo = amigo;
}
public Integer getTelefone() {
return telefone;
}
public void setTelefone(Integer telefone) {
this.telefone = telefone;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((amigo == null) ? 0 : amigo.hashCode());
result = PRIME * result + ((telefone == null) ? 0 : telefone.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final TelefonePK other = (TelefonePK) obj;
if (amigo == null) {
if (other.amigo != null)
return false;
} else if (!amigo.equals(other.amigo))
return false;
if (telefone == null) {
if (other.telefone != null)
return false;
} else if (!telefone.equals(other.telefone))
return false;
return true;
}
}
valeu aí hein… agora acho que nao tem mais desculpa pra nao usar as annotations.
se eu tiver algum problema eu posto de novo aqui.
obrigado.
Criado 4 de setembro de 2006
Ultima resposta 4 de set. de 2006
Respostas 2
Participantes 2