Skip to content

Commit 0bded40

Browse files
committed
Added DAO pattern.
1 parent 3356680 commit 0bded40

File tree

9 files changed

+253
-1
lines changed

9 files changed

+253
-1
lines changed

dao/pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>dao</artifactId>
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.hibernate</groupId>
14+
<artifactId>hibernate-core</artifactId>
15+
</dependency>
16+
<dependency>
17+
<groupId>com.h2database</groupId>
18+
<artifactId>h2</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<scope>test</scope>
24+
</dependency>
25+
</dependencies>
26+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.iluwatar;
2+
3+
import java.util.List;
4+
5+
import org.hibernate.Query;
6+
import org.hibernate.Session;
7+
import org.hibernate.SessionFactory;
8+
import org.hibernate.cfg.Configuration;
9+
10+
public class App
11+
{
12+
public static void main( String[] args ) {
13+
14+
SessionFactory sessionFactory = new Configuration()
15+
.addAnnotatedClass(Wizard.class)
16+
.addAnnotatedClass(Spellbook.class)
17+
.addAnnotatedClass(Spell.class)
18+
.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
19+
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1")
20+
.setProperty("hibernate.current_session_context_class", "thread")
21+
.setProperty("hibernate.show_sql", "true")
22+
.setProperty("hibernate.hbm2ddl.auto", "create")
23+
.buildSessionFactory();
24+
25+
Session session = sessionFactory.getCurrentSession();
26+
session.beginTransaction();
27+
Wizard wizard = new Wizard();
28+
wizard.setFirstName("Jugga");
29+
Spellbook spellbook = new Spellbook();
30+
Spell spell = new Spell();
31+
spellbook.getSpells().add(spell);
32+
wizard.getSpellbooks().add(spellbook);
33+
session.persist(wizard);
34+
Query query = session.createQuery("from Wizard");
35+
List<?> list = query.list();
36+
Wizard p1 = (Wizard) list.get(0);
37+
System.out.println(String.format("id=%d firstName=%s", p1.getId(), p1.getFirstName()));
38+
session.getTransaction().commit();
39+
}
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.iluwatar;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.Id;
7+
import javax.persistence.Version;
8+
9+
@Entity
10+
public class BaseEntity {
11+
12+
@Id
13+
@GeneratedValue
14+
@Column(name = "ID")
15+
private Long id;
16+
17+
public Long getId() {
18+
return id;
19+
}
20+
21+
public void setId(Long id) {
22+
this.id = id;
23+
}
24+
25+
@Version
26+
private Long version;
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.iluwatar;
2+
3+
public abstract class DaoBase<E extends BaseEntity> {
4+
5+
E find(Long id) {
6+
7+
}
8+
9+
void persist(E e) {
10+
11+
}
12+
13+
E merge(E e) {
14+
15+
}
16+
17+
void remove(E e) {
18+
19+
}
20+
21+
List<E> findAll() {
22+
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.iluwatar;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.JoinColumn;
5+
import javax.persistence.ManyToOne;
6+
import javax.persistence.Table;
7+
8+
@Entity
9+
@Table(name="SPELL")
10+
public class Spell extends BaseEntity {
11+
12+
private String name;
13+
14+
@ManyToOne
15+
@JoinColumn(name="SPELLBOOK_ID_FK", referencedColumnName="ID")
16+
private Spellbook spellbook;
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.iluwatar;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import javax.persistence.CascadeType;
7+
import javax.persistence.Entity;
8+
import javax.persistence.JoinColumn;
9+
import javax.persistence.ManyToOne;
10+
import javax.persistence.OneToMany;
11+
import javax.persistence.Table;
12+
13+
@Entity
14+
@Table(name="SPELLBOOK")
15+
public class Spellbook extends BaseEntity {
16+
17+
public Spellbook() {
18+
spells = new HashSet<Spell>();
19+
}
20+
21+
private String name;
22+
23+
@ManyToOne
24+
@JoinColumn(name="WIZARD_ID_FK", referencedColumnName="ID")
25+
private Wizard wizard;
26+
27+
@OneToMany(mappedBy = "spellbook", orphanRemoval = true, cascade = CascadeType.ALL)
28+
private Set<Spell> spells;
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public Wizard getWizard() {
39+
return wizard;
40+
}
41+
42+
public void setWizard(Wizard wizard) {
43+
this.wizard = wizard;
44+
}
45+
46+
public Set<Spell> getSpells() {
47+
return spells;
48+
}
49+
50+
public void setSpells(Set<Spell> spells) {
51+
this.spells = spells;
52+
}
53+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.iluwatar;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import javax.persistence.CascadeType;
7+
import javax.persistence.Entity;
8+
import javax.persistence.OneToMany;
9+
import javax.persistence.Table;
10+
11+
@Entity
12+
@Table(name="WIZARD")
13+
public class Wizard extends BaseEntity {
14+
15+
public Wizard() {
16+
spellbooks = new HashSet<Spellbook>();
17+
}
18+
19+
private String firstName;
20+
21+
@OneToMany(mappedBy = "wizard", orphanRemoval = true, cascade = CascadeType.ALL)
22+
private Set<Spellbook> spellbooks;
23+
24+
public String getFirstName() {
25+
return firstName;
26+
}
27+
28+
public void setFirstName(String firstName) {
29+
this.firstName = firstName;
30+
}
31+
32+
public Set<Spellbook> getSpellbooks() {
33+
return spellbooks;
34+
}
35+
36+
public void setSpellbooks(Set<Spellbook> spellbooks) {
37+
this.spellbooks = spellbooks;
38+
}
39+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.iluwatar;
2+
3+
public class AppTest {
4+
}

pom.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
<properties>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<hibernate.version>5.0.0.Beta1</hibernate.version>
13+
<h2.version>1.4.187</h2.version>
14+
<junit.version>4.12</junit.version>
1215
</properties>
1316
<modules>
1417
<module>abstract-factory</module>
@@ -46,14 +49,25 @@
4649
<module>intercepting-filter</module>
4750
<module>poison-pill</module>
4851
<module>lazy-loading</module>
52+
<module>dao</module>
4953
</modules>
5054

5155
<dependencyManagement>
5256
<dependencies>
57+
<dependency>
58+
<groupId>org.hibernate</groupId>
59+
<artifactId>hibernate-core</artifactId>
60+
<version>${hibernate.version}</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>com.h2database</groupId>
64+
<artifactId>h2</artifactId>
65+
<version>${h2.version}</version>
66+
</dependency>
5367
<dependency>
5468
<groupId>junit</groupId>
5569
<artifactId>junit</artifactId>
56-
<version>4.11</version>
70+
<version>${junit.version}</version>
5771
<scope>test</scope>
5872
</dependency>
5973
</dependencies>

0 commit comments

Comments
 (0)