Salve galera, estou precisando executar um update com o Hibernate, mas como sou iniciante não estou conseguindo. Estou tentando usar alguns exemplos que encontrei pela internet mas msm assim não vai.
exemplos:
http://www.java2s.com/Code/Java/Hibernate/UpdateHQL.htm
http://www.mkyong.com/hibernate/hibernate-query-examples-hql/
Estou tentando fazer assim:
//classe que retorna a sessao do hibernate
package classesdecontrole;
import org.hibernate.Session;
import util.HibernateUtil;
public class RetornaSessaoHibernate{
public Session hibernateSession(){
Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
return sessao;
}
}
//classe mapeada
package classesbd;
// Generated Aug 2, 2011 3:38:17 PM by Hibernate Tools 3.2.1.GA
import java.util.HashSet;
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 static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* Yperfil generated by hbm2java
*/
@Entity
@Table(name="yperfil"
,catalog="ibgdb"
)
public class Yperfil implements java.io.Serializable {
private Long id;
private String perfil;
private String observacao;
private Set yusuarioses = new HashSet(0);
private Set yperfilmodulos = new HashSet(0);
public Yperfil() {
}
public Yperfil(String perfil) {
this.perfil = perfil;
}
public Yperfil(String perfil, String observacao, Set yusuarioses, Set yperfilmodulos) {
this.perfil = perfil;
this.observacao = observacao;
this.yusuarioses = yusuarioses;
this.yperfilmodulos = yperfilmodulos;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="id", unique=true, nullable=false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="perfil", nullable=false, length=50)
public String getPerfil() {
return this.perfil;
}
public void setPerfil(String perfil) {
this.perfil = perfil;
}
@Column(name="observacao", length=50)
public String getObservacao() {
return this.observacao;
}
public void setObservacao(String observacao) {
this.observacao = observacao;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="yperfil")
public Set getYusuarioses() {
return this.yusuarioses;
}
public void setYusuarioses(Set yusuarioses) {
this.yusuarioses = yusuarioses;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="yperfil")
public Set getYperfilmodulos() {
return this.yperfilmodulos;
}
public void setYperfilmodulos(Set yperfilmodulos) {
this.yperfilmodulos = yperfilmodulos;
}
}
//arquivo xml da classe
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 2, 2011 3:38:17 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="classesbd.Yperfil" table="yperfil" catalog="ibgdb">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<property name="perfil" type="string">
<column name="perfil" length="50" not-null="true" />
</property>
<property name="observacao" type="string">
<column name="observacao" length="50" />
</property>
<set name="yusuarioses" inverse="true">
<key>
<column name="idperfil" />
</key>
<one-to-many class="classesbd.Yusuarios" />
</set>
<set name="yperfilmodulos" inverse="true">
<key>
<column name="idperfil" />
</key>
<one-to-many class="classesbd.Yperfilmodulo" />
</set>
</class>
</hibernate-mapping>
//aqui o metodo onde estou tentando fazer o update, seguindo os exemplos que encontrei.
public void alterarPerfil(){
if(!perfilDesc.getText().isEmpty()){
String p = perfilDesc.getText();
//aqui começa o problema, minha instancia de Query não me traz os metodos setParameter() e executeUpdate(), e não estou entendendo o pq, sendo que usam isso no exemplo.
Query query = (Query)new RetornaSessaoHibernate().hibernateSession().createQuery("update Yperfil set setPerfil = :newPerfil, "
+ " setObservacao = :newObservacao"
+ " where setId = :codigo");
// --> minha instancia query nao traz esses metodos, setParameter e executeUpdate, coloquei aqui apenas para exibicao e vcs verem se realmente é assim que se faz
query.setParameter("newPerfil", perfilDesc.getText());
query.setParameter("newObservacao", perfilObs.getText());
query.setParameter("codigo", perfilCod.getText());
query.executeUpdate();
}
}
Não estou conseguindo fazer um update usando hibernate. Como fazer isso ?
obrigado
xml hell), como já esta com annotation não é preciso do xml. Só lembra de colocar no hibernate.cfg.xml o mapeamento da classe que contem o annotation, dai fechou essa parte.