Problems with ejb3 EntityManager, find method
marianokm Dec 8, 2006 4:11 PMHi, i got the following exception when i run my code
Hibernate: select rol0_.ROL_ID as ROL1_6_0_, rol0_.ADMINISTRADOR as ADMINIST2_6_0_, rol0_.DESCRIPCION as DESCRIPC3_6_0_, rol0_.HABIL ITADO as HABILITADO6_0_, rol0_.NOTAS as NOTAS6_0_ from ROL rol0_ where rol0_.ROL_ID=? 17:21:47,640 ERROR [STDERR] java.lang.ClassCastException: ar.com.ebizlink.server.models.Rol 17:21:47,640 ERROR [STDERR] at $Proxy97.getRol(Unknown Source) 17:21:47,640 ERROR [STDERR] at ar.com.ebizlink.console.controllers.RolController.edit(Unknown Source)
My enviroment is:
jboss-4.0.5.GA, jems installer (ejb3 instalation)
j2sdk1.5.0_09
jboss-eclipse 1.5
and the files are listed next ...
Rol.java
package ar.com.ebizlink.server.models; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "ROL") public class Rol implements java.io.Serializable { private static final long serialVersionUID = 1L; // Fields private Long rolId; private String descripcion; private Boolean habilitado; private Boolean administrador; private String notas; // Constructors /** default constructor */ public Rol() { } /** minimal constructor */ public Rol(Long rolId) { this.rolId = rolId; } /** full constructor */ public Rol(Long rolId, String descripcion, Boolean habilitado, Boolean administrador, String notas) { this.rolId = rolId; this.descripcion = descripcion; this.habilitado = habilitado; this.administrador = administrador; this.notas = notas; } // Property accessors @Id @GeneratedValue @Column(name = "ROL_ID", unique = true, nullable = false, insertable = true, updatable = true) public Long getRolId() { return this.rolId; } public void setRolId(Long rolId) { this.rolId = rolId; } @Column(name = "DESCRIPCION", unique = false, nullable = true, insertable = true, updatable = true, length = 50) public String getDescripcion() { return this.descripcion; } public void setDescripcion(String descripcion) { this.descripcion = descripcion; } @Column(name = "HABILITADO", unique = false, nullable = true, insertable = true, updatable = true) public Boolean getHabilitado() { return this.habilitado; } public void setHabilitado(Boolean habilitado) { this.habilitado = habilitado; } @Column(name = "ADMINISTRADOR", unique = false, nullable = true, insertable = true, updatable = true) public Boolean getAdministrador() { return this.administrador; } public void setAdministrador(Boolean administrador) { this.administrador = administrador; } @Column(name = "NOTAS", unique = false, nullable = true, insertable = true, updatable = true, length = 100) public String getNotas() { return this.notas; } public void setNotas(String notas) { this.notas = notas; } }
RolManager.java
package ar.com.ebizlink.server.interfaces; import java.util.List; import javax.ejb.Local; import ar.com.ebizlink.server.models.Rol; @Local public interface RolManager { public Rol getRol(Long rolId); public List<Rol> getRoles(); public void save(Rol detached); public void delete(Long rolId); }
The bean instance
package ar.com.ebizlink.server.beans; import java.util.List; import javax.ejb.Stateless; import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import ar.com.ebizlink.server.interfaces.RolManager; import ar.com.ebizlink.server.models.Rol; @Stateless public class RolManagerBean implements RolManager { @PersistenceContext private EntityManager em; public Rol getRol(Long rolId) { return em.find(Rol.class, rolId); // Query query = em.createQuery("select r from Rol r where r.rolId = // :rolId"); // query.setParameter("rolId", rolId); // return (Rol)query.getSingleResult(); } @SuppressWarnings("unchecked") public List<Rol> getRoles() { return em.createQuery("from Rol").getResultList(); } @TransactionAttribute(TransactionAttributeType.REQUIRED) public void save(Rol detached) { Rol rol = getCurrent(detached.getRolId()); rol.setAdministrador(detached.getAdministrador()); rol.setDescripcion(detached.getDescripcion()); rol.setHabilitado(detached.getHabilitado()); rol.setNotas(detached.getNotas()); em.persist(rol); } @TransactionAttribute(TransactionAttributeType.MANDATORY) private Rol getCurrent(Long rolId) { Rol rol = getRol(rolId); if (rol == null) { rol = new Rol(); em.persist(rol); } return rol; } @TransactionAttribute(TransactionAttributeType.REQUIRED) public void delete(Long rolId) { Rol rol = getRol(rolId); em.remove(rol); } }
JSF CODE - RolController.java
package ar.com.ebizlink.console.controllers; import java.util.List; import ar.com.ebizlink.console.forms.RolForm; import ar.com.ebizlink.server.interfaces.RolManager; import ar.com.ebizlink.server.models.Rol; import ar.com.ulink.framework.commons.tokens.CommonsTokens; import ar.com.ulink.framework.ejb3.EJB3ServiceLocator; import ar.com.ulink.framework.mvc.jsf.JsfUtility; public class RolController { private RolForm rolForm; public final RolForm getRolForm() { return rolForm; } public final void setRolForm(RolForm rolForm) { this.rolForm = rolForm; } // THIS METHOD WORKS FINE !! public final List<Rol> getRoles() { List<Rol> roles = null; try { RolManager rolManager = (RolManager) EJB3ServiceLocator .getEjbInterface("ebizlink/RolManagerBean/local"); roles = rolManager.getRoles(); } catch (Exception e) { e.printStackTrace(); } return roles; } // HOWEVER, THIS ONE CRASH !! // when execute : Rol rol = rolManager.getRol(rolId); public final String edit() { // Status a devolver. String actionForward = CommonsTokens.EDIT_SUCCESS; // Tomo el parametro del rol a modificar. Long rolId = Long.valueOf(JsfUtility.getObjectFromContext("rolId") .toString()); System.out.println("************* RolId: " + rolId); try { // Obtengo el rol a modificar. RolManager rolManager = (RolManager) EJB3ServiceLocator .getEjbInterface("ebizlink/RolManagerBean/local"); System.out.println("************* paso 2, RolId: " + rolId); // Here's the exception Rol rol = rolManager.getRol(rolId); System.out.println("************* paso 3, RolId: " + rolId); // Asigno los datos al form. rolForm.setAdministrador(rol.getAdministrador()); rolForm.setDescripcion(rol.getDescripcion()); rolForm.setHabilitado(rol.getHabilitado()); rolForm.setNotas(rol.getNotas()); rolForm.setRolId(rol.getRolId()); } catch (Exception e) { actionForward = CommonsTokens.EDIT_FAILURE; e.printStackTrace(); } // Forward del formulario. return actionForward; } }
i have the exception mentioned earlier and i dont have any clue where the probles is ...
any ideas ??
Regards
Mariano