Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. <dependency>
  2. <groupId>org.hibernate</groupId>
  3. <artifactId>hibernate-core</artifactId>
  4. <version>5.0.6.Final</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.hibernate</groupId>
  8. <artifactId>hibernate-c3p0</artifactId>
  9. <version>5.0.6.Final</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>commons-dbcp</groupId>
  13. <artifactId>commons-dbcp</artifactId>
  14. <version>1.4</version>
  15. </dependency>
  16.  
  17. <?xml version="1.0" encoding="utf-8"?>
  18. <!DOCTYPE hibernate-configuration PUBLIC
  19. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  20. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  21.  
  22. <hibernate-configuration>
  23. <session-factory>
  24. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  25. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  26.  
  27. <property name="hibernate.connection.url">jdbc:mysql://localhost/testprojectdatabase</property>
  28. <property name="hibernate.connection.username">username_here</property>
  29. <property name="hibernate.connection.password">password_here</property>
  30.  
  31. <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
  32. <property name="hibernate.c3p0.min_size">5</property>
  33. <property name="hibernate.c3p0.max_size">20</property>
  34. <property name="hibernate.c3p0.timeout">300</property>
  35. <property name="hibernate.c3p0.max_statements">50</property>
  36.  
  37. <mapping resource="milkiv/mytestproject/models/UserInfo.hbm.xml"/>
  38. </session-factory>
  39. </hibernate-configuration>
  40.  
  41. public class UserInfoManager implements ManagerAdd<UserInfo> {
  42. private final SessionFactory factory;
  43.  
  44. public UserInfoManager() {
  45. factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
  46. }
  47.  
  48. public int add(UserInfo user) {
  49. Transaction transaction = null;
  50. Integer userId = null;
  51. try (Session session = factory.openSession()){
  52. transaction = session.beginTransaction();
  53. userId = (Integer) session.save(user);
  54. transaction.commit();
  55. } catch (HibernateException he) {
  56. if (transaction != null) {
  57. transaction.rollback();
  58. }
  59. }
  60. return userId;
  61. }
  62. }
  63.  
  64. <mapping resource="/milkiv/mytestproject/models/UserInfo.hbm.xml"/>
  65.  
  66. <mapping resource="UserInfo.hbm.xml"/>
  67.  
  68. public UserInfoManager() {
  69. System.out.println(UserInfoManager.class
  70. .getResource("/milkiv/mytestproject/models/UserInfo.hbm.xml"));
  71.  
  72. System.out.println(ClassLoader.getSystemClassLoader().getResource(
  73. "milkiv/mytestproject/models/UserInfo.hbm.xml"));
  74. }
  75.  
  76. new UserInfoManager();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement