3
There’s something wrong with mine persistence.xml
? I can’t open the connection.
My Bean is 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 mistake 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)
Dude, you added the connection driver to your
lib
? in addition, you must declare your entities to thepersistence.xml
– Marcos Sousa