Using JPA with H2 Database

Asked

Viewed 1,791 times

1

I apologize for posting my question in the chaining of a similar one. So I’m posting as new topic, ok?

I am using the H2 and JPA database to create my tables.

Testing my application, I can create the tables, insert records, list the inserted records, but I can not view it in the Console through the browser using the address localhost:8082, which opens the window for connection to the Database.

Below is my persistence.xml, log4j.properties, the class that generates the tables and log4j output in the Console:

persistence.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<persistence-unit name="h2_pu" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="connection.driver_class" value="org.h2.Driver" />
        <property name="hibernate.connection.url" value="jdbc:h2:C:/Users/Augusto/workspace/h2database/db/bd_test" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />

        <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>

My log4j:

log4j.rootCategory=INFO, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%r [%t] %-5p %c - %m%n
log4j.logger.org.hibernate=ERROR
log4j.logger.org.hibernate.type=TRACE
log4j.logger.org.hibernate.tool.hbm2ddl=TRACE

My class for table generation:

package testes;

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

public class GerarTabelas {
    public static void main(String[] args) {
        try {
            EntityManagerFactory factory = Persistence.createEntityManagerFactory("h2_pu");
            factory.close();
            System.out.println("Tabelas geradas com sucesso !!!");
        } catch (Exception e) {
            System.out.println("Erro ao gerar as tabelas !!! Verificar as entidades !!!");
            e.printStackTrace();
        }
    }
}

The result of the query via Java code is correct, as below:

1 - Andrea Lopes -- Obs 01 
2 - Beatriz Sampaio -- Nenhuma obs
3 - Carlos Carvalho -- Obs 03
4 - Daniel Carneiro -- Obs 04
5 - Edson Rabelo -- Obs 05
6 - Fernanda Monteiro -- Obs 06
7 - Guilherme Fontes -- Obs 07
8 - Helena Gomes -- Obs 08
9 - Ivan Reis -- Obs 09
10 - Janaina Ferreira -- Décima obs

In the browser, when I connect to the database, I try to access the Client table for checks, queries and etc. manually, using the command:

SELECT * FROM PUBLIC.BD_TEST.CLIENTE; 

and getting error as below:

SELECT * FROM PUBLIC.BD_TEST.CLIENTE;
Table "BD_TEST" not found; SQL statement:
SELECT * FROM PUBLIC.BD_TEST.CLIENTE [42102-180] 42S02/42102 (Help)

What is the correct way to execute this query in H2?

Thanking in advance any help,

yours sincerely

Augusto Cesar

1 answer

1

To command grammar SELECT H2 does not have the database. You can only have the table name, prefixed or not with the schema. The examples of Quickstart corroborate this.

Think that the base is already defined in the connection, so it’s not like in other Sgbdrs.

So try to run:

SELECT * FROM PUBLIC.CLIENTE;

Or:

SELECT * FROM CLIENTE;

Browser other questions tagged

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