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:
parent
ce91d77866
commit
27ab504a90
|
@ -1,21 +0,0 @@
|
|||
<?xml version="1.0"?>
|
||||
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
||||
<!-- Generated 29-abr-2014 15:39:59 by Hibernate Tools 3.4.0.CR1 -->
|
||||
<hibernate-mapping>
|
||||
<class name="org.baeldung.persistence.model.Bar" table="BAR">
|
||||
<id name="id" type="int">
|
||||
<column name="ID" />
|
||||
<generator class="assigned" />
|
||||
</id>
|
||||
<set name="fooSet" table="FOO" inverse="false" lazy="true" order-by="NAME DESC">
|
||||
<key>
|
||||
<column name="BAR_ID" />
|
||||
</key>
|
||||
<one-to-many class="org.baeldung.persistence.model.Foo" />
|
||||
</set>
|
||||
<property name="name" type="java.lang.String">
|
||||
<column name="NAME" />
|
||||
</property>
|
||||
</class>
|
||||
</hibernate-mapping>
|
|
@ -4,6 +4,7 @@ import java.io.Serializable;
|
|||
import java.util.Set;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
|
@ -22,11 +23,13 @@ public class Bar implements Serializable {
|
|||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id")
|
||||
private int id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
@OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@OrderBy(clause = "NAME DESC")
|
||||
private Set<Foo> fooSet = Sets.newHashSet();
|
||||
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
<?xml version="1.0"?>
|
||||
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
||||
<!-- Generated 23-abr-2014 18:00:30 by Hibernate Tools 3.4.0.CR1 -->
|
||||
<hibernate-mapping>
|
||||
<class name="org.baeldung.persistence.model.Foo" table="FOO">
|
||||
<id name="id" type="int">
|
||||
<column name="ID" />
|
||||
<generator class="native" />
|
||||
</id>
|
||||
<property name="name" type="java.lang.String">
|
||||
<column name="NAME" />
|
||||
</property>
|
||||
<many-to-one name="bar" class="org.baeldung.persistence.model.Bar" fetch="join" >
|
||||
<column name="BAR_ID" />
|
||||
</many-to-one>
|
||||
|
||||
</class>
|
||||
</hibernate-mapping>
|
|
@ -2,30 +2,29 @@ package org.baeldung.persistence.model;
|
|||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
import org.hibernate.annotations.Fetch;
|
||||
import org.hibernate.annotations.FetchMode;
|
||||
|
||||
@Entity
|
||||
public class Foo implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id")
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@ManyToOne(targetEntity = Bar.class)
|
||||
@ManyToOne(targetEntity = Bar.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "BAR_ID")
|
||||
@Fetch(FetchMode.JOIN)
|
||||
private Bar bar = new Bar();
|
||||
|
||||
public Foo() {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue