Solucionado (ver solução)
Solucionado
(ver solução)
4
respostas

Unknown entity: org.hibernate.internal.SessionImpl

Quando executo a main aparece o seguinte erro:

Erro

jan. 07, 2022 11:07:05 AM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation INFO: HHH000204: Processing PersistenceUnitInfo [name: loja] jan. 07, 2022 11:07:05 AM org.hibernate.Version logVersion INFO: HHH000412: Hibernate ORM core version 5.4.27.Final jan. 07, 2022 11:07:06 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations {5.1.2.Final} jan. 07, 2022 11:07:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) jan. 07, 2022 11:07:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql:loja] jan. 07, 2022 11:07:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {password=****, user=postgres} jan. 07, 2022 11:07:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false jan. 07, 2022 11:07:06 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections INFO: HHH000115: Hibernate connection pool size: 20 (min=1) jan. 07, 2022 11:07:06 AM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect jan. 07, 2022 11:07:06 AM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: org.hibernate.internal.SessionImpl at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:723) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:706) at br.com.alura.loja.testes.CadastroDeProduto.main(CadastroDeProduto.java:22)

Alguem pode me ajudar?

4 respostas

Códigos

SQL(PostgreSql)

CREATE TABLE produtos(
    id serial PRIMARY KEY,
    nome varchar(20),
    descricao varchar(50),
    preco decimal
)

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</groupId>
    <artifactId>loja</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.4.27.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.3.1</version>
        </dependency>

    </dependencies>

</project>

persistence.xml

<persistence 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"
             version="2.1">

    <persistence-unit name="loja" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> <!-- DB Driver -->
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql:loja" /> <!-- BD Mane -->
            <property name="javax.persistence.jdbc.user" value="postgres" /> <!-- DB User -->
            <property name="javax.persistence.jdbc.password" value="----" /> <!-- DB Password -->

            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> <!-- DB Dialect -->
            <property name="hibernate.show_sql" value="true"/>
        </properties>

    </persistence-unit>

</persistence>

Classe Produto

package br.com.alura.loja.modelo;

import java.math.BigDecimal;

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

@Entity
@Table(name = "produtos")
public class Produto {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String nome;
    private String descricao;
    private BigDecimal preco;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public BigDecimal getPreco() {
        return preco;
    }

    public void setPreco(BigDecimal preco) {
        this.preco = preco;
    }
}

Main

package br.com.alura.loja.testes;

import java.math.BigDecimal;

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

import br.com.alura.loja.modelo.Produto;

public class CadastroDeProduto {

    public static void main(String[] args) {
        Produto produto = new Produto();
        produto.setNome("Xiaomi Redmi");
        produto.setDescricao("Muito Bom");
        produto.setPreco(new BigDecimal("800"));

        EntityManagerFactory factory = Persistence
                .createEntityManagerFactory("loja");
        EntityManager em = factory.createEntityManager();
        em.persist(em);
        em.close();
    }

}
solução!

Oi Anderson,

Está tudo certinho os códigos.

Testa novamente o main delimitando uma transação:

Produto produto = new Produto();
produto.setNome("Xiaomi Redmi");
produto.setDescricao("Muito Bom");
produto.setPreco(new BigDecimal("800"));

EntityManagerFactory factory = Persistence.createEntityManagerFactory("loja");
EntityManager em = factory.createEntityManager();

em.getTransaction().begin();
em.persist(em);
em.getTransaction().commit();

em.close();

o problema era isso aqui : em.persist(em); assei o entity manager como parametro no persist ao invés de passar o produto.

Ah verdade! nem tinha reparado hehe.

Bons estudos!