Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.casadocodigo.jsfjpa.controllers;
- 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;
- }
- }
- package br.com.casadocodigo.jsfjpa.persistence;
- 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();
- }
- }
- <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>
- <?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>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement