Latest Changes as of May 13th

Added utility class for data creation (populating tables).
Eliminated hbm.xml files
Fixed the strange “id bug” with Foo
Eliminated the bar_id field from the Foo class
This commit is contained in:
egmp777
2014-05-13 09:42:24 -05:00
parent ce91d77866
commit 27ab504a90
7 changed files with 171 additions and 75 deletions
@@ -7,31 +7,30 @@
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true</property>
<property name="connection.username">tutorialuser</property>
<property name="connection.password">tutorialmy5ql</property>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/HIBERTEST2_TEST</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- Drop and re-create the database schema on startup -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<mapping resource="org//baeldung//persistence//model//Foo.hbm.xml" />
<mapping resource="org//baeldung//persistence//model//Bar.hbm.xml" />
</session-factory>
</session-factory>
</hibernate-configuration>
@@ -0,0 +1,119 @@
package org.baeldung.persistence.hibernate;
import java.util.List;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.model.Foo;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.google.common.collect.Lists;
public class FooSortingPersistenceServiceData {
private static ServiceRegistry serviceRegistry;
private static SessionFactory sessionFactory;
private static Configuration configuration;
private static StandardServiceRegistryBuilder builder;
public FooSortingPersistenceServiceData() {
super();
}
public void createBars() {
configWork();
Session session = null;
Transaction tx = null;
session = sessionFactory.openSession();
tx = session.getTransaction();
try {
tx.begin();
for (int i = 156; i < 160; i++) {
final Bar bar = new Bar();
bar.setName("Bar_" + i);
final Foo foo = new Foo("Foo_" + (i + 120));
foo.setBar(bar);
session.save(foo);
final Foo foo2 = new Foo(null);
if (i % 2 == 0)
foo2.setName("LuckyFoo" + (i + 120));
foo2.setBar(bar);
session.save(foo2);
bar.getFooSet().add(foo);
bar.getFooSet().add(foo2);
session.merge(bar);
}
tx.commit();
session.flush();
} catch (final HibernateException he) {
if (tx != null)
tx.rollback();
System.out.println("Not able to open session");
he.printStackTrace();
} catch (final Exception e) {
e.printStackTrace();
} finally {
if (session != null)
session.close();
}
}
public void createFoos() {
configWork();
Session session = null;
Transaction tx = null;
session = sessionFactory.openSession();
tx = session.getTransaction();
final List<Foo> fooList = Lists.newArrayList();
for (int i = 35; i < 46; i++) {
final Foo foo = new Foo();
foo.setName("Foo_" + (i + 120));
final Bar bar = new Bar("bar_" + i);
bar.getFooSet().add(foo);
foo.setBar(bar);
fooList.add(foo);
}
try {
tx.begin();
for (final Foo foo : fooList) {
session.save(foo.getBar());
session.save(foo);
}
tx.commit();
session.flush();
} catch (final HibernateException he) {
if (tx != null)
tx.rollback();
System.out.println("Not able to open session");
he.printStackTrace();
} catch (final Exception e) {
e.printStackTrace();
} finally {
if (session != null)
session.close();
}
}
public void configWork() {
configuration = new Configuration();
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
configuration.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
configuration.setProperty(AvailableSettings.DRIVER, "com.mysql.jdbc.Driver");
configuration.setProperty(AvailableSettings.URL, "jdbc:mysql://localhost:3306/HIBERTEST2_TEST");
configuration.setProperty(AvailableSettings.USER, "root");
configuration.setProperty(AvailableSettings.PASS, "");
builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.addPackage("com.cc.example.hibernate").addAnnotatedClass(Foo.class).addAnnotatedClass(Bar.class).configure().buildSessionFactory(builder.build());
}
}
@@ -5,6 +5,8 @@ import static org.junit.Assert.assertNull;
import java.util.List;
import java.util.Set;
import javax.imageio.spi.ServiceRegistry;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.model.Foo;
import org.baeldung.spring.PersistenceConfig;
@@ -14,6 +16,7 @@ import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order;
import org.junit.After;
@@ -30,12 +33,25 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
public class FooSortingPersistenceServiceTest {
private SessionFactory sf;
private Session sess;
private static ServiceRegistry serviceRegistry;
private static Configuration configuration;
private static StandardServiceRegistryBuilder builder;
@Before
public final void before() {
final Configuration configuration = new Configuration().configure();
final StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sf = configuration.buildSessionFactory(builder.build());
public void before() {
// final FooSortingPersistenceServiceData fooData = new FooSortingPersistenceServiceData();
// fooData.createBars();
configuration = new Configuration();
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
configuration.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
configuration.setProperty(AvailableSettings.DRIVER, "com.mysql.jdbc.Driver");
configuration.setProperty(AvailableSettings.URL, "jdbc:mysql://localhost:3306/HIBERTEST2_TEST");
configuration.setProperty(AvailableSettings.USER, "root");
configuration.setProperty(AvailableSettings.PASS, "");
configuration.setProperty("hibernate.show_sql", "true");
builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sf = configuration.addPackage("org.baeldung.persistence.model").addAnnotatedClass(Foo.class).addAnnotatedClass(Bar.class).configure().buildSessionFactory(builder.build());
sess = sf.openSession();
sess.beginTransaction();
}