Advertisement
Guest User

Untitled

a guest
Apr 10th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 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. <hibernate-configuration>
  6. <session-factory>
  7. <!-- Database connection settings -->
  8. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  9. <property name="connection.url">jdbc:mysql://localhost/UserDB</property>
  10. <property name="connection.username">root</property>
  11. <property name="connection.password"></property>
  12.  
  13. <!-- JDBC connection pool (use the built-in) -->
  14. <property name="connection.pool_size">1</property>
  15.  
  16. <!-- SQL dialect -->
  17. <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
  18.  
  19. <!-- Enable Hibernate's automatic session context management -->
  20. <property name="current_session_context_class">thread</property>
  21.  
  22. <!-- Disable the second-level cache -->
  23. <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
  24.  
  25. <!-- Echo all executed SQL to stdout -->
  26. <property name="show_sql">true</property>
  27. <property name="format_sql">true</property>
  28.  
  29. <!-- Drop and re-create the database schema on startup -->
  30. <property name="hbm2ddl.auto">create</property>
  31.  
  32. <!-- Mapping files -->
  33. <mapping class="com.stackoverflow.model.Student" />
  34. <mapping class="com.stackoverflow.model.School" />
  35.  
  36. </session-factory>
  37. </hibernate-configuration>
  38.  
  39. public class TestDao {
  40.  
  41. private static SessionFactory sessionFactory;
  42. private Session session;
  43.  
  44. @BeforeClass
  45. public static void setUp() throws Exception {
  46. // A SessionFactory is set up once for an application!
  47. final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
  48. .configure("hibernate.cfg.xml") // configures settings from hibernate.cfg.xml
  49. .build();
  50. try {
  51. sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
  52. }
  53. catch (Exception e) {
  54. // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
  55. // so destroy it manually.
  56. StandardServiceRegistryBuilder.destroy( registry );
  57. }
  58. assertNotNull(sessionFactory);
  59. }
  60.  
  61. @AfterClass
  62. public static void tearDown() throws Exception {
  63. sessionFactory.close();
  64. }
  65.  
  66. @Before
  67. public void beforeEach() {
  68. session = sessionFactory.openSession();
  69. session.beginTransaction();
  70.  
  71. }
  72. @After
  73. public void afterEach() {
  74. session.getTransaction().commit();
  75. session.close();
  76. }
  77.  
  78. @Test
  79. public void testSave() {
  80. School school= new School("Ivanofrankivsk, Myk town, We str");
  81. Student student1 = new Student("student1",LocalDate.of(1988, 8, 12),school);
  82. Student student2 = new Student("student2",LocalDate.of(1977, 2, 1),school);
  83. Student student3 = new Student("student3",LocalDate.of(1999, 1, 1),school);
  84.  
  85. school.addStudent(student1);
  86. school.addStudent(student2);
  87. school.addStudent(student3);
  88.  
  89. session.save(school);
  90. }
  91. }
  92.  
  93. java.lang.AssertionError: null
  94. at org.junit.Assert.fail(Assert.java:86)
  95. at org.junit.Assert.assertTrue(Assert.java:41)
  96. at org.junit.Assert.assertNotNull(Assert.java:621)
  97. at org.junit.Assert.assertNotNull(Assert.java:631)
  98. at com.stackoverflow.dao.TestDao.setUp(TestDao.java:47)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement