Problem with Hibernate -JPA

Asked

Viewed 56 times

-1

mar 04, 2021 5:12:57 PM org.hibernate.jpa.boot.internal.PersistenceXmlParser doResolve
INFO: HHH000318: Could not find any META-INF/persistence.xml file in the classpath
javax.persistence.PersistenceException: No Persistence provider for EntityManager named java-hibernate:  
No META-INF/persistence.xml was found in classpath.

at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at org.example.HibernateUtil.init(HibernateUtil.java:18)
at org.example.HibernateUtil.<clinit>(HibernateUtil.java:12)
at org.example.TesteHibernate.testeHibernate(TesteHibernate.java:9)

java.lang.NullPointerException
at org.example.HibernateUtil.getEntityManager(HibernateUtil.java:26)
at org.example.TesteHibernate.testeHibernate(TesteHibernate.java:9)

I had stopped using Eclipse because I had a problem I couldn’t fix, so I switched to intelliJ. I’ve already created a folder with META-INF and persistence.xml as already said in other questions, but it still hasn’t solved the problem Pastas

1 answer

1


Try to rewrite your persistence.xml and make sure your META-INF folder was actually created in the bin, here’s a Persistence.xml skeleton, configure according to your needs

<persistence 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_2_0.xsd"
    version="2.0">

    <persistence-unit name="contas">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>br.com.alura.jpa.modelo.Conta</class>
        <class>br.com.alura.jpa.modelo.Movimentacao</class>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/alura_jpa" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="caminha123" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />

            <property name="hibernate.hbm2ddl.auto" value="update" />

        </properties>
    </persistence-unit>
</persistence>

It is also worth checking the settings of pom.xml, if they are in accordance with the settings of your drivers, here goes another skeleton of pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>br.com.alura.jpa</groupId>
  <artifactId>projeto-jpa</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-agroal</artifactId>
        <version>5.4.28.Final</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.23</version>
    </dependency>
    
  </dependencies>
</project>

if you have questions about the driver settings of your database go to: https://mvnrepository.com/

if it still doesn’t work, try to put the meta-inf folder in a path like this /src/main/Resources/META-INF

Browser other questions tagged

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