I can’t connect Jpa to Postgresql at all

Asked

Viewed 1,064 times

3

Good I’m trying to connect the postgresql, however, without success. My application will be an application jsf and so I’m using persistence.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">

    <persistence-unit name="comunicaVisual"
        transaction-type="RESOURCE_LOCAL">
        <jar-file>lib/postgresql-9.3-1101.jdbc3.jar</jar-file>
        <class>entys.Pessoa</class>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <validation-mode>AUTO</validation-mode>
        <properties>
                <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
                <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres" />
                <property name="javax.persistence.jdbc.user" value="testepg" />
                <property name="javax.persistence.jdbc.password" value="336445" />
                <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
                <property name="hibernate.connection.shutdown" value="true" />
                <property name="hibernate.hbm2ddl.auto" value="update" />
                <property name="hibernate.show_sql" value="false" />
                <property name="hibernate.format_sql" value="false" />

                <property name="hibernate.cache.use_query_cache" value="true"/>
                <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
        </properties>
    </persistence-unit>

</persistence>

The error that is happening is as follows. inserir a descrição da imagem aqui I hope someone’s been there.

1 answer

2

A generic connection failure error can have numerous causes. However, the ideal is for you to search the application logs to see if there are other error stacks that have been written but don’t appear on the screen. Look logs is something important, but often ignored by developers.

However, I can list the most common causes of this type of error:

  • Database server is not started. You have installed it, but it is not running. Maybe the service is starting manually.
  • Server does not accept remote connections. Postgresql has been installed on another machine and does not accept connections from other Ips. I’m not an expert on Postgre, but this is a common problem in Mysql.
  • Network blocking. There is a firewall or proxy blocking the connection.
  • Error in configuration. Although I can see its configuration, the correct is to validate using some other program to see if no data has been entered improperly.
  • Missing driver. For some reason the application is unable to find or load the driver from the database. Is the jar really in the application? Can your application server load the driver from there? You may need to consult the server manual and maybe install the jar as a module or put inside the folder lib.
  • Incorrect version of driver. I already helped some two people with a similar problem and the cause was that the driver version was in trouble or outdated (the installation of Postgresql was more recent). The solution is to get the most up-to-date jar possible. Being with a outdated version can occur with existing projects or when someone copies a Maven dependency statement from some site without consulting the central repository.

Looking at the posted configuration, I saw that the configuration is used jar-file to indicate the Postgre jar. That is wrong!!!

I don’t know if it is the cause of the error, but this setting serves to indicate the jar where the JPA entities are. This is used when the configuration xml is in a different package, by exeplo, in the case of JEE applications that contains a jar with Ejbs.

Therefore, the most likely cause of the error is the missing jar in the correct location. Remember, Java finds automatically the jars that are in the folder WEB-INF\lib of a web application, in addition to the application server library folders.

  • Good afternoon pleasure and thanks for the reply however the jars are added in the project lib and in the path also now the error that is happening is another [Hibernate: select nextval ('producto_id_seq') Hibernate: Insert into person (name, id) values (?, ?) ] I hope someone can help

  • @Andremartins This mistake there probably has something to do with the SEQUENCE that you created in the bank. You posted part of the log, but there is no error there. It may be a problem in the database, such as the SEQUENCE does not exist or the user used does not have permission. It may also be an error in mapping, such as incorrect annotation or incorrect SEQUENCE name. Give a check and if you fail, post another question by placing your entity and pertinent details about the bank’s structure. Hug.

  • thanks, what was happening was that the table in the bank had a field more... however I managed to solve... I’m with another question now is to get change the scheme in the application... mysql is easy now postgresql is very complicated.

  • @Andremartins How to change the scheme? If you want to use tables of different schemes, just use the attribute schema in the annotation @Table.

Browser other questions tagged

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