|
| 1 | +package org.baeldung.persistence.hibernate; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.baeldung.persistence.model.Bar; |
| 6 | +import org.baeldung.persistence.model.Foo; |
| 7 | +import org.hibernate.HibernateException; |
| 8 | +import org.hibernate.Session; |
| 9 | +import org.hibernate.SessionFactory; |
| 10 | +import org.hibernate.Transaction; |
| 11 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 12 | +import org.hibernate.cfg.AvailableSettings; |
| 13 | +import org.hibernate.cfg.Configuration; |
| 14 | +import org.hibernate.service.ServiceRegistry; |
| 15 | + |
| 16 | +import com.google.common.collect.Lists; |
| 17 | + |
| 18 | +public class FooSortingPersistenceServiceData { |
| 19 | + private static ServiceRegistry serviceRegistry; |
| 20 | + private static SessionFactory sessionFactory; |
| 21 | + private static Configuration configuration; |
| 22 | + private static StandardServiceRegistryBuilder builder; |
| 23 | + |
| 24 | + public FooSortingPersistenceServiceData() { |
| 25 | + super(); |
| 26 | + } |
| 27 | + |
| 28 | + public void createBars() { |
| 29 | + |
| 30 | + configWork(); |
| 31 | + Session session = null; |
| 32 | + Transaction tx = null; |
| 33 | + session = sessionFactory.openSession(); |
| 34 | + tx = session.getTransaction(); |
| 35 | + try { |
| 36 | + tx.begin(); |
| 37 | + for (int i = 156; i < 160; i++) { |
| 38 | + final Bar bar = new Bar(); |
| 39 | + bar.setName("Bar_" + i); |
| 40 | + final Foo foo = new Foo("Foo_" + (i + 120)); |
| 41 | + foo.setBar(bar); |
| 42 | + session.save(foo); |
| 43 | + final Foo foo2 = new Foo(null); |
| 44 | + if (i % 2 == 0) |
| 45 | + foo2.setName("LuckyFoo" + (i + 120)); |
| 46 | + foo2.setBar(bar); |
| 47 | + session.save(foo2); |
| 48 | + bar.getFooSet().add(foo); |
| 49 | + bar.getFooSet().add(foo2); |
| 50 | + session.merge(bar); |
| 51 | + } |
| 52 | + tx.commit(); |
| 53 | + session.flush(); |
| 54 | + } catch (final HibernateException he) { |
| 55 | + if (tx != null) |
| 56 | + tx.rollback(); |
| 57 | + System.out.println("Not able to open session"); |
| 58 | + he.printStackTrace(); |
| 59 | + } catch (final Exception e) { |
| 60 | + e.printStackTrace(); |
| 61 | + } finally { |
| 62 | + if (session != null) |
| 63 | + session.close(); |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + public void createFoos() { |
| 69 | + |
| 70 | + configWork(); |
| 71 | + Session session = null; |
| 72 | + Transaction tx = null; |
| 73 | + session = sessionFactory.openSession(); |
| 74 | + tx = session.getTransaction(); |
| 75 | + final List<Foo> fooList = Lists.newArrayList(); |
| 76 | + for (int i = 35; i < 46; i++) { |
| 77 | + |
| 78 | + final Foo foo = new Foo(); |
| 79 | + foo.setName("Foo_" + (i + 120)); |
| 80 | + final Bar bar = new Bar("bar_" + i); |
| 81 | + bar.getFooSet().add(foo); |
| 82 | + foo.setBar(bar); |
| 83 | + fooList.add(foo); |
| 84 | + |
| 85 | + } |
| 86 | + try { |
| 87 | + tx.begin(); |
| 88 | + for (final Foo foo : fooList) { |
| 89 | + |
| 90 | + session.save(foo.getBar()); |
| 91 | + session.save(foo); |
| 92 | + } |
| 93 | + tx.commit(); |
| 94 | + session.flush(); |
| 95 | + } catch (final HibernateException he) { |
| 96 | + if (tx != null) |
| 97 | + tx.rollback(); |
| 98 | + System.out.println("Not able to open session"); |
| 99 | + he.printStackTrace(); |
| 100 | + } catch (final Exception e) { |
| 101 | + e.printStackTrace(); |
| 102 | + } finally { |
| 103 | + if (session != null) |
| 104 | + session.close(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public void configWork() { |
| 109 | + configuration = new Configuration(); |
| 110 | + configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); |
| 111 | + configuration.setProperty("dialect", "org.hibernate.dialect.MySQLDialect"); |
| 112 | + configuration.setProperty(AvailableSettings.DRIVER, "com.mysql.jdbc.Driver"); |
| 113 | + configuration.setProperty(AvailableSettings.URL, "jdbc:mysql://localhost:3306/HIBERTEST2_TEST"); |
| 114 | + configuration.setProperty(AvailableSettings.USER, "root"); |
| 115 | + configuration.setProperty(AvailableSettings.PASS, ""); |
| 116 | + builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); |
| 117 | + sessionFactory = configuration.addPackage("com.cc.example.hibernate").addAnnotatedClass(Foo.class).addAnnotatedClass(Bar.class).configure().buildSessionFactory(builder.build()); |
| 118 | + } |
| 119 | +} |
0 commit comments