Guest User

Untitled

a guest
Jun 11th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 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://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5.  
  6. <hibernate-configuration>
  7.  
  8. <session-factory>
  9. <!-- Database connection settings -->
  10. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  11. <property name="connection.url">jdbc:mysql://localhost:3306/country</property>
  12. <property name="connection.username">root</property>
  13. <property name="connection.password">password</property>
  14.  
  15. </session-factory>
  16.  
  17. </hibernate-configuration>
  18.  
  19. <util:properties id="db" location="classpath:db.properties" />
  20.  
  21. <hibernate-configuration>
  22.  
  23. <session-factory>
  24. <!-- Database connection settings -->
  25. <property name="driverClassName" value="#{db['driverClassName']}"></property>
  26. <property name="url" value="#{db['url']}"></property>
  27. <property name="username" value="#{db['username']}"></property>
  28. <property name="password" value="#{db['password']}"></property>
  29.  
  30. </session-factory>
  31.  
  32. </hibernate-configuration>
  33.  
  34. org.dom4j.DocumentException: Error on line 8 of document : The prefix "util" for element "util:properties" is not bound. Nested exception: The prefix "util" for element "util:properties" is not bound.
  35. at org.dom4j.io.SAXReader.read(SAXReader.java:482)
  36. at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2155)
  37.  
  38. driverClassName=com.mysql.jdbc.Driver
  39.  
  40. url=jdbc:mysql://localhost:3306/country
  41.  
  42. username=root
  43.  
  44. password=password
  45.  
  46. java.util.Properties properties = new Properties();
  47. properties.load(new FileInputStream("db.properties"));
  48.  
  49. Configuration configuration = new Configuration();
  50.  
  51. configuration.configure("hibernate.cfg.xml").addProperties(properties);;
  52.  
  53. ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
  54. .applySettings(configuration.getProperties()).build();
  55.  
  56. SessionFactory sessionFactory = configuration
  57. .buildSessionFactory(serviceRegistry);
  58.  
  59. hibernate.connection.url=jdbc:mysql://localhost:3306/country
  60. hibernate.connection.driver_class=com.mysql.jdbc.Driver
  61. hibernate.connection.username=root
  62. hibernate.connection.password=123
  63.  
  64. import java.util.Properties;
  65.  
  66. import org.hibernate.SessionFactory;
  67. import org.hibernate.cfg.AnnotationConfiguration;
  68.  
  69. public class HibernateUtil {
  70.  
  71. private static final SessionFactory sessionFactory = buildSessionFactory();
  72.  
  73. private static SessionFactory buildSessionFactory() {
  74. try {
  75.  
  76. Properties dbConnectionProperties = new Properties();
  77. try {
  78. dbConnectionProperties.load(HibernateUtil.class.getClassLoader().getSystemClassLoader().getResourceAsStream("hibernate.properties"));
  79. } catch(Exception e) {
  80. e.printStackTrace();
  81. // Log
  82. }
  83.  
  84. return new AnnotationConfiguration().mergeProperties(dbConnectionProperties).configure("hibernate.cfg.xml").buildSessionFactory();
  85.  
  86.  
  87. } catch (Throwable ex) {
  88. ex.printStackTrace();
  89. // throw new ExceptionInInitializerError(ex);
  90. }
  91. return null;
  92. }
  93.  
  94. public static SessionFactory getSessionFactory() {
  95. return sessionFactory;
  96. }
  97.  
  98. }
  99.  
  100. <?xml version='1.0' encoding='utf-8'?>
  101. <!DOCTYPE hibernate-configuration PUBLIC
  102. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  103. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  104.  
  105. <hibernate-configuration>
  106. <session-factory>
  107.  
  108. <!-- Database connection settings -->
  109.  
  110. <!-- JDBC connection pool (use the built-in) -->
  111. <property name="connection.pool_size">1</property>
  112.  
  113. <!-- SQL dialect -->
  114. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  115.  
  116. <!-- Enable Hibernate's automatic session context management -->
  117. <property name="current_session_context_class">thread</property>
  118.  
  119. <!-- Disable the second-level cache -->
  120. <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
  121.  
  122. <!-- Echo all executed SQL to stdout -->
  123. <property name="show_sql">false</property>
  124.  
  125. <property name="hibernate.hbm2ddl.auto">update</property>
  126. <!--<mapping class="net.viralpatel.hibernate.Employee"/>-->
  127. <!--<mapping class="net.viralpatel.hibernate.PersonEntity"/>-->
  128. <mapping class="mobin.FavaEmail.entities.PersonEntity"/>
  129. <mapping class="mobin.FavaEmail.entities.OrgEntity"/>
  130. <mapping class="mobin.FavaEmail.entities.User"/>
  131.  
  132.  
  133. </session-factory>
  134. </hibernate-configuration>
  135.  
  136. <!DOCTYPE hibernate-configuration PUBLIC
  137. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  138. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  139.  
  140. <hibernate-configuration>
  141. <session-factory>
  142. <property name="hibernate.connection.driver_class"></property>
  143. <property name="hibernate.connection.url"></property>
  144. <property name="hibernate.connection.username"></property>
  145. <property name="hibernate.connection.password"></property>
  146. <property name="hibernate.connection.pool_size"></property>
  147. <property name="hibernate.dialect"></property>
  148. <property name="show_sql"></property>
  149. <property name="format_sql"></property>
  150. <property name="hbm2ddl.auto"></property>
  151.  
  152. <mapping class="SourceFields"/>
  153. </session-factory>
  154. </hibernate-configuration>
  155.  
  156. hibernate.connection.driver_class=org.hsqldb.jdbcDriver
  157. hibernate.connection.url=jdbc:hsqldb:hsql://localhost:9191/Register
  158. hibernate.connection.username=username
  159. hibernate.connection.password=password
  160. hibernate.connection.pool_size=10
  161. hibernate.dialect=org.hibernate.dialect.HSQLDialect
  162. show_sql=true
  163. format_sql=true
  164. hbm2ddl.auto=update
  165.  
  166. public static Properties propertyLoad() {
  167. Properties properties = null;
  168. if (properties == null) {
  169. properties = new Properties();
  170. try {
  171. properties.load(PropertiesUtil.class
  172. .getResourceAsStream("/config.properties"));
  173. } catch (Exception e) {
  174. e.printStackTrace();
  175. }
  176. }
  177. return properties;
  178. }
  179.  
  180. public class HibernateBaseDB {
  181.  
  182. private static Properties properties = new Properties();
  183.  
  184. public static SessionFactory getSessionFactory() {
  185.  
  186. properties = PropertiesUtil.propertyLoad();
  187. Configuration configuration = new Configuration();
  188.  
  189. configuration.configure("Hibrnateconfig.cfg.xml")
  190. .addProperties(properties);
  191.  
  192. ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
  193. .applySettings(configuration.getProperties())
  194. .buildServiceRegistry();
  195.  
  196. SessionFactory sessionFactory = configuration
  197. .buildSessionFactory(serviceRegistry);
  198.  
  199. return sessionFactory;
  200.  
  201. }
  202.  
  203. SessionFactory sessionFactory = HibernateBaseDB.getSessionFactory();
  204. Session session = sessionFactory.openSession();
Add Comment
Please, Sign In to add comment