Tenhi visto muito falarem sobre hibernate + JPA, mas nao to conseguindo ver o porque de colocar o JPA se ja existe o hibernate. Com o hibernate, nos temos os DTOs e os objetos mapeados no hibernate.cfj.xml e os hbs, pra que preciso do JPA? onde posso achar um exemplo de uma arquitetura usando JPA e hibernate?
Utilizamos JPA no Hibernate Annotation, onde não é mais preciso parametrizar .hbm’s .xml e todos os outros . que existirem. Apenas o Hibernate.cfg.xml, que será onde indicaremos os caminhos das classes beans.
Nos beans toda descrição de tabelas e de campos do banco é feita usando Annotation, segue abaixo um exemplo de bean e DAO generico para persistir qualquer tipo de objeto.
Este é um BEAN com ANNOTATION gerada automaticamento pelo NetBeans
[code]/*
Cidade.java
Created on September 8, 2007, 3:41 PM
To change this template, choose Tools | Template Manager
@author Fabio
*/ @Entity @Table(name = “cadcid”) @NamedQueries( { @NamedQuery(name = “Cidade.findByTodos”, query = “SELECT c FROM Cidade c”), @NamedQuery(name = “Cidade.findByCidId”, query = “SELECT c FROM Cidade c WHERE c.cidId = :cidId”), @NamedQuery(name = “Cidade.findByCidNome”, query = “SELECT c FROM Cidade c WHERE c.cidNome = :cidNome”), @NamedQuery(name = “Cidade.findByCidUf”, query = “SELECT c FROM Cidade c WHERE c.cidUf = :cidUf”), @NamedQuery(name = “Cidade.findByCidSituacao”, query = “SELECT c FROM Cidade c WHERE c.cidSituacao = :cidSituacao”)
})
public class Cidade implements Serializable {
@Column(name = “CID_SITUACAO”)
private Short cidSituacao;
/** Creates a new instance of Cidade */
public Cidade() {
}
/**
Creates a new instance of Cidade with the specified values.
@param cidId the cidId of the Cidade
*/
public Cidade(Integer cidId) {
this.cidId = cidId;
}
/**
Creates a new instance of Cidade with the specified values.
@param cidId the cidId of the Cidade
@param cidNome the cidNome of the Cidade
@param cidUf the cidUf of the Cidade
*/
public Cidade(Integer cidId, String cidNome, String cidUf) {
this.cidId = cidId;
this.cidNome = cidNome;
this.cidUf = cidUf;
}
/**
Gets the cidId of this Cidade.
@return the cidId
*/
public Integer getCidId() {
return this.cidId;
}
/**
Sets the cidId of this Cidade to the specified value.
@param cidId the new cidId
*/
public void setCidId(Integer cidId) {
this.cidId = cidId;
}
/**
Gets the cidNome of this Cidade.
@return the cidNome
*/
public String getCidNome() {
return this.cidNome;
}
/**
Sets the cidNome of this Cidade to the specified value.
@param cidNome the new cidNome
*/
public void setCidNome(String cidNome) {
this.cidNome = cidNome;
}
/**
Gets the cidUf of this Cidade.
@return the cidUf
*/
public String getCidUf() {
return this.cidUf;
}
/**
Sets the cidUf of this Cidade to the specified value.
@param cidUf the new cidUf
*/
public void setCidUf(String cidUf) {
this.cidUf = cidUf;
}
/**
Gets the cidSituacao of this Cidade.
@return the cidSituacao
*/
public Short getCidSituacao() {
return this.cidSituacao;
}
/**
Sets the cidSituacao of this Cidade to the specified value.
@param cidSituacao the new cidSituacao
*/
public void setCidSituacao(Short cidSituacao) {
this.cidSituacao = cidSituacao;
}
/**
Returns a hash code value for the object. This implementation computes
a hash code value based on the id fields in this object.
@return a hash code value for this object.
*/ @Override
public int hashCode() {
int hash = 0;
hash += (this.cidId != null ? this.cidId.hashCode() : 0);
return hash;
}
/**
Determines whether another object is equal to this Cidade. The result is
true if and only if the argument is not null and is a Cidade object that
has the same id field values as this object.
@param object the reference object with which to compare
@returntrue if this object is the same as the argument;
false otherwise.
*/ @Override
public boolean equals(Object object) {
// TODO: Warning - this method won’t work in the case the id fields are not set
if (!(object instanceof Cidade)) {
return false;
}
Cidade other = (Cidade)object;
if (this.cidId != other.cidId && (this.cidId == null || !this.cidId.equals(other.cidId))) return false;
return true;
}
/**
Returns a string representation of the object. This implementation constructs
that representation based on the id fields.
@return a string representation of the object.
*/ @Override
public String toString() {
// return “beans.Cidade[cidId=” + cidId + “]”;
return this.getCidId()+" - "+this.getCidNome();
}
}
[/code]
No exemplo acima a SQL utilizada para gerar a tabela CIDADE foi
CREATE TABLE `cadcid` (
`CID_ID` int(11) NOT NULL auto_increment,
`CID_NOME` varchar(70) NOT NULL,
`CID_UF` varchar(2) NOT NULL,
`CID_SITUACAO` decimal(2,0) default NULL,
PRIMARY KEY (`CID_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Este é um DAO GENERICO para persistir, excluir, alterar, listar e buscar qualquer objeto anotado e persitido no banco de dados