Advertisement
Guest User

Untitled

a guest
Jul 9th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <!DOCTYPE hibernate-configuration PUBLIC
  4. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  5. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  6.  
  7. <hibernate-configuration>
  8.  
  9. <session-factory>
  10.  
  11. <!-- Database connection settings -->
  12. <property name="connection.drive_class">com.mysql.jdbc.Driver</property>
  13. <property name="connection.url">jdbc:mysql://127.0.0.1:3306/testebanco</property>
  14. <property name="connection.username">root</property>
  15. <property name="connection.password">102030</property>
  16.  
  17. <!-- JDBC connection pool(use the built-in) -->
  18. <property name="connection.pool_size">1</property>
  19.  
  20. <!-- SQL dialect -->
  21. <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  22.  
  23. <!-- Enable Hibernate's automatic session context management -->
  24. <property name="current_session_context_class">thread</property>
  25.  
  26. <!-- Disable the second-level cache -->
  27. <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
  28.  
  29. <!-- Echo all executed SQL to stdout -->
  30. <property name="show_sql">true</property>
  31.  
  32. <!-- Drop and re-create the database schema on startup -->
  33. <property name="hbm2ddl.auto">create</property>
  34.  
  35. <!-- Mapeamento das entidades -->
  36. <mapping class="teste.DAO.Estado" />
  37.  
  38. </session-factory>
  39.  
  40. public class HibernateUtil {
  41. private static SessionFactory sf = createSessionFactory();
  42.  
  43. public static SessionFactory getSf() {
  44. return sf;
  45. }
  46.  
  47. private static SessionFactory createSessionFactory(){
  48. try{
  49. Configuration cfg = new Configuration().configure();
  50.  
  51. ServiceRegistry registro = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
  52.  
  53. SessionFactory f = cfg.buildSessionFactory(registro);
  54.  
  55. return f;
  56. }catch(Throwable ex){
  57. System.err.println("Erro ao criar a sessão " + ex);
  58. throw new ExceptionInInitializerError(ex);
  59. }
  60.  
  61. }
  62.  
  63. public class HibernateUtilTeste {
  64.  
  65. @Test
  66. public void conectar(){
  67. Session sessao = HibernateUtil.getSf().openSession();
  68. sessao.close();
  69. HibernateUtil.getSf().close();
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement