java – Problems opening connection with JSF and JPA

Question:

Is there something wrong with my persistence.xml ? I can't open the connection.

My Bean looks like this:

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.casadocodigo.jsfjpa.entities.Automovel;
import br.com.casadocodigo.jsfjpa.persistence.JPAUtil;

@ManagedBean
public class AutomovelBean {

    private Automovel automovel = new Automovel();

    private List<Automovel> automoveis;


    public Automovel getAutomovel() {
        return automovel;
    }   

    public void setAutomovel(Automovel automovel) {
        this.automovel = automovel;
    }

    public void salva(Automovel automovel) {

        EntityManager em = JPAUtil.getEntityManager();
        em.getTransaction().begin();

        em.persist(automovel);

        em.getTransaction().commit();
        em.close();

        System.out.println("Marca: " + automovel.getMarca());
    }

    public List<Automovel> getAutomoveis() {

        EntityManager em = JPAUtil.getEntityManager();

        Query q = em.createQuery("select a from Automovel a", Automovel.class);

        this.automoveis = q.getResultList();
        em.close();
        return automoveis;
    }

}

JPAUtil:

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

public class JPAUtil {

    private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");

    public static EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
}

Xhtml file:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>Cadastro de Automoveis</title>
    </h:head>

    <h:body>
        <h:form>
            <h:panelGrid columns="2">

                Marca: <h:inputText value="#{automovelBean.automovel.marca}" /><br/>

                Modelo: <h:inputText value="#{automovelBean.automovel.modelo}"/><br/>

                Ano de Fabricacão: <h:inputText value="#{automovelBean.automovel.anoFabricacao}"/><br/>

                Ano do Modelo: <h:inputText value="#{automovelBean.automovel.anoModelo}"/><br/>

                Observações: <h:inputTextarea value="#{automovelBean.automovel.observacoes}"/><br/>

                <h:commandButton value="Salvar" action="#{automovelBean.salva(automovelBean.automovel)}" />
            </h:panelGrid>
        </h:form>
    </h:body>
</html>

And the 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="default">
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/automoveis" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />

            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create" />

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

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

The error that is happening:

HTTP Status 500 - javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not open connection

type: Exception report

message: javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not open connection

description: The server encountered an internal error that prevented it from fulfilling this request.

exception: 

javax.servlet.ServletException: javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not open connection
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Answer:

There really are some issues with your persistence.xml file, but it's not clear if the issue is specifically with persistence.xml .

  • Check if everything is ok with your MySql Connection Driver;

    • Check if the database you created has the same name declared in your persistence.xml
    • Your persistence.xml is missing the declaration of its entities that will be tables in the database. you will have to add the fully
      qualified name
      within the tags <class></class> , beware the hierarchy of tags ;
    • Add the JPA Hibernate provider declaration (relative to the persistence.xml file version which is 2.0);

    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    • remove the static from your getEntityManager() method in the JPAUtil class.
Scroll to Top
AllEscort