Java Hibernate helps

Asked

Viewed 67 times

0

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>Funcionario.Funcionario</class>
    <class>Estoque.Estoque</class>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>
Entitymanagerprovider.java

public class EntityManagerProvider {
    private static EntityManagerFactory emf = null;
    private static EntityManager em = null; 
    private EntityManagerProvider() { 
    } 
    public static EntityManagerFactory getEMF() { 
        if (getEmf() == null) { 
            Properties prop = new Properties(); 
            prop.setProperty("javax.persistence.jdbc.url", "jdbc:mysql://localhost/Mercado"); 
            prop.setProperty("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver"); 
            prop.setProperty("javax.persistence.jdbc.password", "root"); 
            prop.setProperty("javax.persistence.jdbc.user", "root"); 
            setEmf(Persistence.createEntityManagerFactory("PU", prop)); 
        } 
        return getEmf(); 
    } 
    public static EntityManager getEM() { 
        if (getEm() == null) { 
            setEm(getEMF().createEntityManager()); 
        } 
        return getEm(); 
    } 
}

Functionjava.

public class FuncionarioDAO {
    EntityManager em; 
    public FuncionarioDAO() { 
        em = EntityManagerProvider.getEM(); 
    } 
    public void salva(Funcionario f) { 
        em.getTransaction().begin(); 
        if (f.getCodigo()== 0) { 
            em.persist(f); 
        } else { 
            em.merge(f); 
        } 
        em.getTransaction().commit(); 
    } 
    public Funcionario localiza(int codigo) { 
        Funcionario f = em.find(Funcionario.class, codigo); 
        return f; 
    } 
    public void exclui(Funcionario f) { 
        em.getTransaction().begin(); 
        em.remove(f); 
        em.getTransaction().commit(); 
    } 
    public List<Funcionario> pesquisa() { 
        Query q = em.createQuery("select f from Funcionario as f order by f.nome"); 
        List<Funcionario> lista = q.getResultList(); 
        return lista; 
    } 
    public List<Funcionario> pesquisa(String nome) { 
        Query q = em.createNativeQuery("select * from funcionario where nome like ? order by nome", Funcionario.class); 
        q.setParameter(1, '%' + nome + '%'); 
        List<Funcionario> lista = q.getResultList(); 
        return lista; 
    }    
    
}

ERROR

Exception in thread "AWT-EventQueue-0" javax.persistence.PersistenceException: No Persistence provider for EntityManager named PU
	at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
	at persistencia.EntityManagerProvider.getEMF(EntityManagerProvider.java:27)
	at persistencia.EntityManagerProvider.getEM(EntityManagerProvider.java:33)
	at persistencia.FuncionarioDAO.<init>(FuncionarioDAO.java:18)
	at Tela.Gerenciar.<init>(Gerenciar.java:23)
	at Tela.Gerenciar$10.run(Gerenciar.java:383)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

  • How is your project’s classpath?. eclipselink.jar on it?

  • Voce should have put the properties in the persistence file, not directly in the code, so Voce is breaking several project patterns.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.