Guest User

Untitled

a guest
Jun 10th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.89 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC
  2. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>
  4.  
  5. <session-factory>
  6.  
  7. <!-- Configurações da conexão com o Banco -->
  8. <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
  9. <property name="connection.url">jdbc:mysql://localhost:3306/vendas?useTimezone=true&serverTimezone=UTC</property>
  10. <property name="connection.username">root</property>
  11. <property name="connection.password">xxxxx</property>
  12. <property name="hibernate.connection.autocommit">false</property>
  13.  
  14. <!-- Config da conexao JDBC Mysql -->
  15. <property name="connection.pool_size">1</property>
  16.  
  17. <!-- SQL dialect -->
  18. <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  19.  
  20. <!-- Gerenciamento da sessão mysql -->
  21. <property name="current_session_context_class">thread</property>
  22.  
  23. <!-- Desabilita o cache -->
  24. <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
  25.  
  26. <!-- Exibe os comando SQL -->
  27. <property name="show_sql">true</property>
  28.  
  29. <!-- Criação das tabelas -->
  30. <property name="hbm2ddl.auto">update</property>
  31.  
  32. <mapping class="br.com.livraria.domain.Fornecedores" />
  33. <mapping class="br.com.livraria.domain.Funcionarios" />
  34.  
  35. <!-- <mapping class="br.com.livraria.domain.Produtos" />
  36. <mapping class="br.com.livraria.domain.Vendas" />
  37. <mapping class="br.com.livraria.domain.Item" /> -->
  38.  
  39. </session-factory>
  40.  
  41. </hibernate-configuration>
  42.  
  43. private static final SessionFactory sessionFactory = buildSessionFactory();
  44.  
  45. private static SessionFactory buildSessionFactory() {
  46. try {
  47. // Cria uma conexão a partir do hibernate.cfg.xml
  48.  
  49. Configuration configuration = new Configuration();
  50. configuration.configure();
  51.  
  52. ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
  53. .applySettings(configuration.getProperties()).build();
  54.  
  55.  
  56. SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  57.  
  58. return sessionFactory;
  59.  
  60. //return new Configuration().configure().buildSessionFactory(
  61. // new StandardServiceRegistryBuilder().build() );
  62. }
  63. catch (Throwable ex) {
  64. // Mensagem de erro ao conectar
  65. System.err.println("Erro na conexão." + ex);
  66. throw new ExceptionInInitializerError(ex);
  67. }
  68. }
  69. public static SessionFactory getSessionFactory() {
  70. return sessionFactory;
  71. }}
  72.  
  73. private static final SessionFactory sessionFactory = buildSessionFactory();
  74.  
  75. private static SessionFactory buildSessionFactory() {
  76. try {
  77. // Cria uma conexão a partir do hibernate.cfg.xml
  78.  
  79. Configuration configuration = new Configuration();
  80. configuration.configure();
  81.  
  82. ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
  83. .applySettings(configuration.getProperties()).build();
  84.  
  85.  
  86. SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  87.  
  88. return sessionFactory;
  89.  
  90. //return new Configuration().configure().buildSessionFactory(
  91. // new StandardServiceRegistryBuilder().build() );
  92. }
  93. catch (Throwable ex) {
  94. // Mensagem de erro ao conectar
  95. System.err.println("Erro na conexão." + ex);
  96. throw new ExceptionInInitializerError(ex);
  97. }
  98. }
  99. public static SessionFactory getSessionFactory() {
  100. return sessionFactory;
  101. }}
  102.  
  103. jun 02, 2018 7:06:38 PM org.hibernate.Version logVersion
  104. INFO: HHH000412: Hibernate Core {5.3.1.Final}
  105.  
  106. jun 02, 2018 7:06:38 PM org.hibernate.cfg.Environment <clinit>
  107. INFO: HHH000206: hibernate.properties not found
  108.  
  109. jun 02, 2018 7:06:39 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
  110. INFO: HCANN000001: Hibernate Commons Annotations {5.0.3.Final}
  111.  
  112. jun 02, 2018 7:06:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  113. WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
  114.  
  115. jun 02, 2018 7:06:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
  116. INFO: HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/vendas?useTimezone=true&serverTimezone=UTC]
  117.  
  118. jun 02, 2018 7:06:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
  119. INFO: HHH10001001: Connection properties: {user=root, password=****}
  120.  
  121. jun 02, 2018 7:06:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
  122. INFO: HHH10001003: Autocommit mode: false
  123.  
  124. jun 02, 2018 7:06:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
  125. INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
  126.  
  127. Sat Jun 02 19:06:40 BRT 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
  128.  
  129. jun 02, 2018 7:06:40 PM org.hibernate.dialect.Dialect <init>
  130. INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
  131.  
  132. jun 02, 2018 7:06:40 PM org.hibernate.resource.beans.spi.ManagedBeanRegistryInitiator resoveBeanContainer
  133. INFO: HHH10005002: No explicit CDI BeanManager reference was passed to Hibernate, but CDI is available on the Hibernate ClassLoader.
  134.  
  135. jun 02, 2018 7:06:41 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
  136. INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@3f6db3fb] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
  137.  
  138. jun 02, 2018 7:06:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
  139. INFO: HHH10001008: Cleaning up connection pool[jdbc:mysql://localhost:3306/vendas?useTimezone=true&serverTimezone=UTC]
  140.  
  141. <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  142. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  143. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  144. http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
  145. version="2.1">
  146.  
  147. <persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
  148. <description> Hibernate JPA Configuration Example</description>
  149. <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  150. <properties>
  151. <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> <!-- DB Driver ALTERE-->
  152. <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://172.17.0.2/academy" /> <!-- BD ALTERE -->
  153. <property name="javax.persistence.jdbc.user" value="postgres" /> <!-- DB User -->
  154. <property name="javax.persistence.jdbc.password" value="123456" /> <!-- DB Password -->
  155.  
  156. <property name="hibernate.dialect" value="org.hibernate.dialect.PostgresPlusDialect"/> <!-- Altere pro mysql -->
  157. <property name="hibernate.hbm2ddl.auto" value="update" /> <!-- create / create-drop / update -->
  158.  
  159. <property name="hibernate.show_sql" value="true" /> <!-- Show SQL in console -->
  160. <property name="hibernate.format_sql" value="true" /> <!-- Show SQL formatted -->
  161. </properties>
  162. </persistence-unit>
  163.  
  164. @Entity
  165. public class Pessoa {
  166. @Id @GeneratedValue
  167. private Long id;
  168.  
  169. private String nome;
  170.  
  171. public String getNome() {
  172. return nome;
  173. }
  174.  
  175. public void setNome(String name) {
  176. this.nome = name;
  177. }
  178.  
  179. public Long getId() {
  180. return id;
  181. }
  182.  
  183. public void setId(Long id) {
  184. this.id = id;
  185. }
  186.  
  187. public Pessoa(String nome, Long id) {
  188. this.nome = nome;
  189. this.id = id;
  190. }
  191.  
  192. public Pessoa(String nome){
  193. this(nome,null);
  194. }
  195.  
  196. public Pessoa(){}
  197.  
  198. }
  199.  
  200. import javax.persistence.EntityManager;
  201. import javax.persistence.EntityManagerFactory;
  202. import javax.persistence.Persistence;
  203.  
  204. public class Main {
  205. private static EntityManager em;
  206.  
  207. public static void main(String[] args) {
  208. EntityManagerFactory emf = Persistence
  209. .createEntityManagerFactory("PU");
  210. em = emf.createEntityManager();
  211. em.getTransaction().begin();
  212. em.persist(new Pessoa("NOMEQUALQUER"));
  213. em.getTransaction().commit();
  214. }
  215. }
Add Comment
Please, Sign In to add comment