Skip to content

Commit c2a6799

Browse files
author
Eugen
committed
Merge pull request eugenp#10 from egmp777/master
Latest Changes as of May 13th
2 parents ce91d77 + 27ab504 commit c2a6799

File tree

7 files changed

+174
-78
lines changed

7 files changed

+174
-78
lines changed

spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.hbm.xml

Lines changed: 0 additions & 21 deletions
This file was deleted.

spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Set;
55

66
import javax.persistence.CascadeType;
7+
import javax.persistence.Column;
78
import javax.persistence.Entity;
89
import javax.persistence.FetchType;
910
import javax.persistence.GeneratedValue;
@@ -22,11 +23,13 @@ public class Bar implements Serializable {
2223

2324
@Id
2425
@GeneratedValue(strategy = GenerationType.AUTO)
26+
@Column(name = "id")
2527
private int id;
2628

29+
@Column(name = "name")
2730
private String name;
2831

29-
@OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
32+
@OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
3033
@OrderBy(clause = "NAME DESC")
3134
private Set<Foo> fooSet = Sets.newHashSet();
3235

spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.

spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22

33
import java.io.Serializable;
44

5+
import javax.persistence.CascadeType;
56
import javax.persistence.Column;
67
import javax.persistence.Entity;
8+
import javax.persistence.FetchType;
79
import javax.persistence.GeneratedValue;
810
import javax.persistence.GenerationType;
911
import javax.persistence.Id;
1012
import javax.persistence.JoinColumn;
1113
import javax.persistence.ManyToOne;
1214

13-
import org.hibernate.annotations.Fetch;
14-
import org.hibernate.annotations.FetchMode;
15-
1615
@Entity
1716
public class Foo implements Serializable {
1817

1918
@Id
2019
@GeneratedValue(strategy = GenerationType.AUTO)
20+
@Column(name = "id")
2121
private long id;
2222

23-
@Column(nullable = false)
23+
@Column(name = "name")
2424
private String name;
2525

26-
@ManyToOne(targetEntity = Bar.class)
26+
@ManyToOne(targetEntity = Bar.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
2727
@JoinColumn(name = "BAR_ID")
28-
@Fetch(FetchMode.JOIN)
2928
private Bar bar = new Bar();
3029

3130
public Foo() {

spring-hibernate4/src/test/java/hibernate.cfg.xml

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,30 @@
77

88
<hibernate-configuration>
99
<session-factory>
10-
<!-- Database connection settings -->
11-
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12-
<property name="connection.url">jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true</property>
13-
<property name="connection.username">tutorialuser</property>
14-
<property name="connection.password">tutorialmy5ql</property>
15-
16-
<!-- JDBC connection pool (use the built-in) -->
17-
<property name="connection.pool_size">1</property>
18-
19-
<!-- SQL dialect -->
20-
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
21-
22-
<!-- Enable Hibernate's automatic session context management -->
23-
<property name="current_session_context_class">thread</property>
24-
25-
<!-- Disable the second-level cache -->
26-
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
27-
28-
<!-- Echo all executed SQL to stdout -->
29-
<property name="show_sql">true</property>
30-
31-
<!-- Drop and re-create the database schema on startup -->
32-
33-
<mapping resource="org//baeldung//persistence//model//Foo.hbm.xml" />
34-
<mapping resource="org//baeldung//persistence//model//Bar.hbm.xml" />
35-
</session-factory>
36-
10+
<!-- Database connection settings -->
11+
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12+
<property name="connection.url">jdbc:mysql://localhost:3306/HIBERTEST2_TEST</property>
13+
<property name="connection.username">root</property>
14+
<property name="connection.password"></property>
15+
16+
<!-- JDBC connection pool (use the built-in) -->
17+
<property name="connection.pool_size">1</property>
18+
19+
<!-- SQL dialect -->
20+
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
21+
22+
<!-- Enable Hibernate's automatic session context management -->
23+
<property name="current_session_context_class">thread</property>
24+
25+
<!-- Disable the second-level cache -->
26+
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
27+
28+
<!-- Echo all executed SQL to stdout -->
29+
<property name="show_sql">true</property>
30+
31+
<!-- Drop and re-create the database schema on startup -->
32+
33+
34+
</session-factory>
35+
3736
</hibernate-configuration>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}

spring-hibernate4/src/test/java/org/baeldung/persistence/hibernate/FooSortingPersistenceServiceTest.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.List;
66
import java.util.Set;
77

8+
import javax.imageio.spi.ServiceRegistry;
9+
810
import org.baeldung.persistence.model.Bar;
911
import org.baeldung.persistence.model.Foo;
1012
import org.baeldung.spring.PersistenceConfig;
@@ -14,6 +16,7 @@
1416
import org.hibernate.Session;
1517
import org.hibernate.SessionFactory;
1618
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
19+
import org.hibernate.cfg.AvailableSettings;
1720
import org.hibernate.cfg.Configuration;
1821
import org.hibernate.criterion.Order;
1922
import org.junit.After;
@@ -30,12 +33,25 @@
3033
public class FooSortingPersistenceServiceTest {
3134
private SessionFactory sf;
3235
private Session sess;
36+
private static ServiceRegistry serviceRegistry;
37+
private static Configuration configuration;
38+
private static StandardServiceRegistryBuilder builder;
3339

3440
@Before
35-
public final void before() {
36-
final Configuration configuration = new Configuration().configure();
37-
final StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
38-
sf = configuration.buildSessionFactory(builder.build());
41+
public void before() {
42+
43+
// final FooSortingPersistenceServiceData fooData = new FooSortingPersistenceServiceData();
44+
// fooData.createBars();
45+
configuration = new Configuration();
46+
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
47+
configuration.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
48+
configuration.setProperty(AvailableSettings.DRIVER, "com.mysql.jdbc.Driver");
49+
configuration.setProperty(AvailableSettings.URL, "jdbc:mysql://localhost:3306/HIBERTEST2_TEST");
50+
configuration.setProperty(AvailableSettings.USER, "root");
51+
configuration.setProperty(AvailableSettings.PASS, "");
52+
configuration.setProperty("hibernate.show_sql", "true");
53+
builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
54+
sf = configuration.addPackage("org.baeldung.persistence.model").addAnnotatedClass(Foo.class).addAnnotatedClass(Bar.class).configure().buildSessionFactory(builder.build());
3955
sess = sf.openSession();
4056
sess.beginTransaction();
4157
}

0 commit comments

Comments
 (0)