Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
  8. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  9. <property name="hibernate.connection.password">*********</property>
  10. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
  11. <property name="hibernate.connection.username">root</property>
  12. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  13. <property name="show_sql">true</property>
  14. <mapping resource="com/sample/common/Stock.hbm.xml"></mapping>
  15. </session-factory>
  16. </hibernate-configuration>
  17.  
  18. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  20. <modelVersion>4.0.0</modelVersion>
  21. <groupId>com.sample.common</groupId>
  22. <artifactId>HibernateMavenMySQLXMLSample</artifactId>
  23. <packaging>jar</packaging>
  24. <version>1.0-SNAPSHOT</version>
  25. <name>HibernateMavenMySQLXMLSample</name>
  26. <url>http://maven.apache.org</url>
  27. <dependencies>
  28.  
  29. <!-- JUnit 4.12 -->
  30. <dependency>
  31. <groupId>junit</groupId>
  32. <artifactId>junit</artifactId>
  33. <version>4.12</version>
  34. <scope>test</scope>
  35. </dependency>
  36.  
  37. <!-- MySQL database driver -->
  38. <dependency>
  39. <groupId>mysql</groupId>
  40. <artifactId>mysql-connector-java</artifactId>
  41. <version>5.1.9</version>
  42. </dependency>
  43.  
  44. <!-- Hibernate framework -->
  45. <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate -->
  46. <dependency>
  47. <groupId>org.hibernate</groupId>
  48. <artifactId>hibernate</artifactId>
  49. <version>3.2.3.ga</version>
  50. </dependency>
  51.  
  52. <!-- Hibernate library dependecy start -->
  53. <dependency>
  54. <groupId>dom4j</groupId>
  55. <artifactId>dom4j</artifactId>
  56. <version>1.6.1</version>
  57. </dependency>
  58.  
  59. <dependency>
  60. <groupId>commons-logging</groupId>
  61. <artifactId>commons-logging</artifactId>
  62. <version>1.1.1</version>
  63. </dependency>
  64.  
  65. <dependency>
  66. <groupId>commons-collections</groupId>
  67. <artifactId>commons-collections</artifactId>
  68. <version>3.2.1</version>
  69. </dependency>
  70.  
  71. <dependency>
  72. <groupId>cglib</groupId>
  73. <artifactId>cglib</artifactId>
  74. <version>2.2</version>
  75. </dependency>
  76.  
  77. <dependency>
  78. <groupId>javax.transaction</groupId>
  79. <artifactId>jta</artifactId>
  80. <version>1.1</version>
  81. </dependency>
  82. <!-- Hibernate library dependecy end -->
  83.  
  84. </dependencies>
  85.  
  86. <build>
  87. <plugins>
  88. <plugin>
  89. <artifactId>maven-assembly-plugin</artifactId>
  90. <version>2.6</version>
  91. <configuration>
  92. <descriptorRefs>
  93. <descriptorRef>jar-with-dependencies</descriptorRef>
  94. </descriptorRefs>
  95. <archive>
  96. <manifest>
  97. <mainClass>com.sample.common.App</mainClass>
  98. </manifest>
  99. </archive>
  100. </configuration>
  101. <executions>
  102. <execution>
  103. <id>make-assembly</id> <!-- this is used for inheritance merges -->
  104. <phase>package</phase> <!-- bind to the packaging phase -->
  105. <goals>
  106. <goal>single</goal>
  107. </goals>
  108. </execution>
  109. </executions>
  110. </plugin>
  111. </plugins>
  112. </build>
  113.  
  114. </project>
  115.  
  116. package com.sample.persistence;
  117.  
  118. import org.hibernate.SessionFactory;
  119. import org.hibernate.cfg.Configuration;
  120.  
  121. public class HibernateUtil {
  122.  
  123. private static final SessionFactory sessionFactory = buildSessionFactory();
  124.  
  125. private static SessionFactory buildSessionFactory() {
  126. try {
  127. // Create the SessionFactory from hibernate.cfg.xml
  128. return new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
  129. }
  130. catch (Throwable ex) {
  131. // Make sure you log the exception, as it might be swallowed
  132. System.err.println("Initial SessionFactory creation failed." + ex);
  133. throw new ExceptionInInitializerError(ex);
  134. }
  135. }
  136.  
  137. public static SessionFactory getSessionFactory() {
  138. return sessionFactory;
  139. }
  140.  
  141. public static void shutdown() {
  142. // Close caches and connection pools
  143. getSessionFactory().close();
  144. }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement