JPA implementando o DAO

opa!

galera, tem alguma coisa errada nesses arquivos

package dao;

import java.util.List;

public interface GenericDAO<E> {
	
	public E salvar(E object);
	public List<E> todos();
	

}
package dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import bean.TabelaBean;

public class TabelaDAO implements GenericDAO<TabelaBean> {

	private EntityManager entityManager = null;
	
	public TabelaDAO(){
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("teste");
		entityManager = factory.createEntityManager();		
	}
	
	public TabelaBean salvar(TabelaBean object) {
		entityManager.getTransaction().begin();
		entityManager.persist(object);
		entityManager.getTransaction().commit();
		return object;
	}

	@SuppressWarnings("unchecked")
	public List<TabelaBean> todos() {
		List resultList = entityManager.createQuery("SELECT obj FROM tabela1 obj").getResultList();
		return resultList;		
	}

}

este arquivo esta dentro da pasta META-INF

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 
	xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
	
	<persistence-unit name="teste">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="hibernate.archive.autodetection" value="class, hbm"/>
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost/jpa"/>
			<property name="hibernate.connection.username" value="jpa"/>
			<property name="hibernate.connection.password" value="jpa"/>	
			
			<property name="hibernate.show_sql" value="true"/>
			<property name="hibernate.format_sql" value="true"/>
			<property name="use_sql_comments" value="true"/>
			
		</properties>
	</persistence-unit>
	
</persistence>
package bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="tabela1")
public class TabelaBean implements Serializable {
	
	private static final long serialVersionUID = 6398963610524254994L;
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private long id;
	
	@Column(name="nome")
	private String nome;
	
	@Column(name="email")
	private String email;
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result + ((nome == null) ? 0 : nome.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof TabelaBean)) {
			return false;
		}
		TabelaBean other = (TabelaBean) obj;
		if (email == null) {
			if (other.email != null) {
				return false;
			}
		} else if (!email.equals(other.email)) {
			return false;
		}
		if (id != other.id) {
			return false;
		}
		if (nome == null) {
			if (other.nome != null) {
				return false;
			}
		} else if (!nome.equals(other.nome)) {
			return false;
		}
		return true;
	}
	
	
}

esse exemplo é apenas para entender o funcionamento do JPA e como implementá-lo, o banco de dados só tem uma tabela que se chama “tabela1”

espero que alguem me ajude

o Erro que aparece é

EntityManagerFactory factory = Persistence.createEntityManagerFactory("teste");

Acho que esse erro é pq vc nao tem um Entity com o nome teste, tenta criar um EntityBean com esse nome.

então ao inves de “teste”

você sugere que eu coloque “TabelaBean”

é isso?

edit

mesmo erro

A pasta META-INF não estava dentro da src no eclipse, por isso que não dava certo

agora deu

tem como vc mandar o projeto completo

Por curiosidade, como ficou seu DAO Genérico?

Deu certo, aqui o problema era com o meu banco de dados