Advertisement
Guest User

Untitled

a guest
Jul 14th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. import java.util.List;
  2.  
  3. import javax.faces.bean.ManagedBean;
  4. import javax.persistence.EntityManager;
  5. import javax.persistence.Query;
  6.  
  7. import br.com.casadocodigo.jsfjpa.entities.Automovel;
  8. import br.com.casadocodigo.jsfjpa.persistence.JPAUtil;
  9.  
  10. @ManagedBean
  11. public class AutomovelBean {
  12.  
  13. private Automovel automovel = new Automovel();
  14.  
  15. private List<Automovel> automoveis;
  16.  
  17.  
  18. public Automovel getAutomovel() {
  19. return automovel;
  20. }
  21.  
  22. public void setAutomovel(Automovel automovel) {
  23. this.automovel = automovel;
  24. }
  25.  
  26. public void salva(Automovel automovel) {
  27.  
  28. EntityManager em = JPAUtil.getEntityManager();
  29. em.getTransaction().begin();
  30.  
  31. em.persist(automovel);
  32.  
  33. em.getTransaction().commit();
  34. em.close();
  35.  
  36. System.out.println("Marca: " + automovel.getMarca());
  37. }
  38.  
  39. public List<Automovel> getAutomoveis() {
  40.  
  41. EntityManager em = JPAUtil.getEntityManager();
  42.  
  43. Query q = em.createQuery("select a from Automovel a", Automovel.class);
  44.  
  45. this.automoveis = q.getResultList();
  46. em.close();
  47. return automoveis;
  48. }
  49.  
  50. }
  51.  
  52. import javax.persistence.EntityManager;
  53. import javax.persistence.EntityManagerFactory;
  54. import javax.persistence.Persistence;
  55.  
  56. public class JPAUtil {
  57.  
  58. private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
  59.  
  60. public static EntityManager getEntityManager() {
  61. return emf.createEntityManager();
  62. }
  63. }
  64.  
  65. <html xmlns="http://www.w3.org/1999/xhtml"
  66. xmlns:h="http://java.sun.com/jsf/html">
  67.  
  68. <h:head>
  69. <title>Cadastro de Automoveis</title>
  70. </h:head>
  71.  
  72. <h:body>
  73. <h:form>
  74. <h:panelGrid columns="2">
  75.  
  76. Marca: <h:inputText value="#{automovelBean.automovel.marca}" /><br/>
  77.  
  78. Modelo: <h:inputText value="#{automovelBean.automovel.modelo}"/><br/>
  79.  
  80. Ano de Fabricacão: <h:inputText value="#{automovelBean.automovel.anoFabricacao}"/><br/>
  81.  
  82. Ano do Modelo: <h:inputText value="#{automovelBean.automovel.anoModelo}"/><br/>
  83.  
  84. Observações: <h:inputTextarea value="#{automovelBean.automovel.observacoes}"/><br/>
  85.  
  86. <h:commandButton value="Salvar" action="#{automovelBean.salva(automovelBean.automovel)}" />
  87. </h:panelGrid>
  88. </h:form>
  89. </h:body>
  90. </html>
  91.  
  92. <?xml version="1.0" encoding="UTF-8"?>
  93.  
  94. <persistence version="2.0"
  95. xmlns="http://java.sun.com/xml/ns/persistence"
  96. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  97. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
  98. http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  99.  
  100. <persistence-unit name="default">
  101. <properties>
  102. <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/automoveis" />
  103. <property name="javax.persistence.jdbc.user" value="root" />
  104. <property name="javax.persistence.jdbc.password" value="root" />
  105.  
  106. <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
  107. <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
  108. <property name="hibernate.hbm2ddl.auto" value="create" />
  109.  
  110. <property name="hibernate.show_sql" value="true" />
  111. <property name="hibernate.format_sql" value="true" />
  112.  
  113. </properties>
  114. </persistence-unit>
  115. </persistence>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement