Guest User

Untitled

a guest
Oct 23rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. return new Configuration().configure().buildSessionFactory();
  2.  
  3. <?xml version='1.0' encoding='utf-8'?>
  4. <!DOCTYPE hibernate-configuration PUBLIC
  5. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  6. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  7.  
  8. <hibernate-configuration>
  9. <session-factory>
  10. <!-- Database connection settings -->
  11. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  12. <property name="connection.url">jdbc:mysql://localhost:3306/racingleague</property>
  13. <property name="connection.username">username</property>
  14. <property name="connection.password">password</property>
  15. <property name="hibernate.format_sql">true</property>
  16. <!-- JDBC connection pool (use the built-in) -->
  17. <property name="connection.pool_size">1</property>
  18. <!-- SQL dialect -->
  19. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  20. <!-- Enable Hibernate's automatic session context management -->
  21. <property name="current_session_context_class">thread</property>
  22. <!-- Disable the second-level cache -->
  23. <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
  24. <!-- Echo all executed SQL to stdout -->
  25. <property name="hibernate.show_sql">true</property>
  26. <!-- Drop and re-create the database schema on startup -->
  27. <property name="hibernate.hbm2ddl.auto">update</property>
  28. <!--property name="hbm2ddl.auto">update</property-->
  29. <mapping resource="com/jr/model/hibernateMappings/user.hbm.xml"/>
  30. </session-factory>
  31. </hibernate-configuration>
  32.  
  33. package com.jr.utils;
  34.  
  35. import org.hibernate.SessionFactory;
  36. import org.hibernate.cfg.Configuration;
  37.  
  38. public class HibernateUtils {
  39.  
  40. private static final SessionFactory sessionFactory = buildSessionFactory();
  41.  
  42. public static SessionFactory buildSessionFactory() {
  43. try {
  44. // Create the SessionFactory from hibernate.cfg.xml
  45. return new Configuration().configure().buildSessionFactory();
  46. }
  47. catch (Throwable ex) {
  48. // Make sure you log the exception, as it might be swallowed
  49. System.err.println("Initial SessionFactory creation failed." + ex);
  50. throw new ExceptionInInitializerError(ex);
  51. }
  52. }
  53.  
  54. }
  55.  
  56. package com.jr.db;
  57.  
  58. import org.hibernate.SessionFactory;
  59. import org.hibernate.classic.Session;
  60.  
  61. import com.jr.utils.HibernateUtils;
  62.  
  63. public abstract class DbWrapper<T> {
  64.  
  65. private static SessionFactory sessionFactory = null;
  66. private static Session session;
  67.  
  68. public DbWrapper() {
  69. setSessionFactory();
  70. }
  71.  
  72. private void setSessionFactory() {
  73. sessionFactory = HibernateUtils.buildSessionFactory();
  74. session = sessionFactory.getCurrentSession();
  75. }
  76.  
  77. public boolean addNewItem(T dbItem) {
  78.  
  79. try {
  80. session.getTransaction().begin();
  81. session.save(dbItem);
  82. session.getTransaction().commit();
  83. } catch (Exception e) {
  84. System.err.println("error exception when adding new item to table"
  85. + e);
  86. } finally {
  87.  
  88. session.close();
  89. sessionFactory.close();
  90. }
  91.  
  92. return false;
  93.  
  94. }
  95.  
  96. public abstract boolean removeItem(String uid);
  97.  
  98. public abstract boolean modifyItem(String uid, T item);
  99.  
  100. }
  101.  
  102. private Logger logger = Logger.getLogger(UserController.class);
  103.  
  104. private UserDb userDb;
  105.  
  106. @RequestMapping(value = "/user/registerSuccess", method = RequestMethod.POST)
  107. public String submitRegisterForm(@Valid User user, BindingResult result) {
  108.  
  109. // validate the data recieved from user
  110. logger.info("validate the data recieved from user");
  111. if (result.hasErrors()) {
  112. logger.info("form has "+result.getErrorCount()+" errors");
  113.  
  114. return "account/createForm";
  115. } else{
  116. // if everthings ok, add user details to database
  117. logger.info("if everthings ok, add user details to database");
  118.  
  119. userDb = new UserDb();
  120.  
  121. userDb.addNewItem(user);
  122.  
  123. // display success and auto log the user to the system.
  124. return "account/main";
  125. }
  126.  
  127. }
  128.  
  129. Configuration con = new Configuration().configure("hibernate.cfg.xml");
Add Comment
Please, Sign In to add comment