Fórum Listar cadastros num DataTable (JPA,JSF,Hibernate) #402770
09/06/2011
0
package faces;
import entity.HibernateUtil;
import entity.Usuario;
import entity.dao.UsuarioJpaController;
import java.util.List;
import javax.management.Query;
public class UsuarioFace {
public UsuarioFace() {
}
public UsuarioJpaController userDAO = new UsuarioJpaController();
public Usuario selectedUser;
public String doAddUser(){
selectedUser = new Usuario();
return "gotoAddNewUser";
}
public String finishAddUser(){
userDAO.create(selectedUser);
return "teste";
}
public Usuario getSelectedUser() {
return selectedUser;
}
public void setSelectedUser(Usuario selectedUser) {
this.selectedUser = selectedUser;
}
}
[/code ]
***************************************
Este é meu JPA Controller
[code]
package entity.dao;
import entity.Usuario;
import entity.dao.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.List;
import javax.faces.convert.Converter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.transaction.UserTransaction;
public class UsuarioJpaController implements Serializable {
public UsuarioJpaController(){
emf = Persistence.createEntityManagerFactory("CompletaAppPU");
}
private UserTransaction utx = null;
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Usuario usuario) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(usuario);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Usuario usuario) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
usuario = em.merge(usuario);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Long id = usuario.getId();
if (findUsuario(id) == null) {
throw new NonexistentEntityException("The usuario with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(Long id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Usuario usuario;
try {
usuario = em.getReference(Usuario.class, id);
usuario.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The usuario with id " + id + " no longer exists.", enfe);
}
em.remove(usuario);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Usuario> findUsuarioEntities() {
return findUsuarioEntities(true, -1, -1);
}
public List<Usuario> findUsuarioEntities(int maxResults, int firstResult) {
return findUsuarioEntities(false, maxResults, firstResult);
}
private List<Usuario> findUsuarioEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
Query q = em.createQuery("select object(o) from Usuario as o");
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Usuario findUsuario(Long id) {
EntityManager em = getEntityManager();
try {
return em.find(Usuario.class, id);
} finally {
em.close();
}
}
public int getUsuarioCount() {
EntityManager em = getEntityManager();
try {
Query q = em.createQuery("select count(o) from Usuario as o");
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
public class Usuario implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String nome;
private int idade;
private String telefone;
public int getIdade() {
return idade;
}
public void setIdade(int idade) {
this.idade = idade;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@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 Usuario)) {
return false;
}
Usuario other = (Usuario) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "entity.Usuario[ id=" + id + " ]";
}
}
public class HibernateUtil {
private static final long serialVersionUID = 1L;
private static HibernateUtil me;
private SessionFactory sessionFactory;
public static final ThreadLocal<Session> session = new ThreadLocal<Session>();
private HibernateUtil(){
sessionFactory = new AnnotationConfiguration()
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
.setProperty("hibernate.connection.url", "jdbc:mysql://200*******")
.setProperty("hibernate.connection.username", "****")
.setProperty("hibernate.connection.password", "1234")
.setProperty("hibernate.hbm2ddl.auto", "none")
.setProperty("hibernate.show_sql", "true")
.setProperty("hibernate.format_sql", "true")
.setProperty("hibernate.c3p0.acquire_increment", "1")
.setProperty("hibernate.c3p0.idle_test_period", "100")
.setProperty("hibernate.c3p0.max_size", "10")
.setProperty("hibernate.c3p0.max_statements", "0")
.setProperty("hibernate.c3p0.min_size", "5")
.setProperty("hibernate.c3p0.timeout", "100")
.addAnnotatedClass(Usuario.class)
.buildSessionFactory();
}
public Session getSession(){
Session toReturn = sessionFactory.openSession();
toReturn.beginTransaction();
return toReturn;
}
public static HibernateUtil getInstance() {
if (me == null) {
me = new HibernateUtil();
}
return me;
}
}
******************************************************
e fora isso tenho as telas main..usuario onde qro listar ...
Claudio Ramone
Curtir tópico
+ 0Posts
10/06/2011
Davi Costa
Jsf?
Att Davi
Gostei + 0
10/06/2011
Davi Costa
Veja esses exemplos
http://www.roseindia.net/jsf/dataTable.shtml
http://www.javabeat.net/tips/46-hdatatable-java-server-faces-jsf.html
Att Davi
Gostei + 0
10/06/2011
Claudio Ramone
Gostei + 0
10/06/2011
Claudio Ramone
Gostei + 0
10/06/2011
Davi Costa
Espero ter ajudado
Att Davi
Gostei + 0
15/06/2011
Dyego Carmo
Se sim , poderia fechar ?
Valeu !
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)