Skip to content

Commit 08c3145

Browse files
committed
Work on DAO example. Table per class strategy. Persist fixes.
1 parent 82eebea commit 08c3145

File tree

6 files changed

+82
-23
lines changed

6 files changed

+82
-23
lines changed

dao/src/main/java/com/iluwatar/App.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ public static void main( String[] args ) {
1313
public static void persistData(WizardDao dao) {
1414
Spell spell = new Spell("Fireball");
1515
Spellbook spellbook = new Spellbook("Book of fire");
16+
spell.setSpellbook(spellbook);
1617
spellbook.getSpells().add(spell);
1718
Wizard wizard = new Wizard("Jugga");
19+
spellbook.setWizard(wizard);
1820
wizard.getSpellbooks().add(spellbook);
1921
dao.persist(wizard);
2022
}
@@ -23,6 +25,12 @@ public static void queryData(WizardDao dao) {
2325
List<Wizard> wizards = dao.findAll();
2426
for (Wizard w: wizards) {
2527
System.out.println(w);
28+
for (Spellbook spellbook: w.getSpellbooks()) {
29+
System.out.println(spellbook);
30+
for (Spell spell: spellbook.getSpells()) {
31+
System.out.println(spell);
32+
}
33+
}
2634
}
2735
}
2836
}

dao/src/main/java/com/iluwatar/BaseEntity.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
package com.iluwatar;
22

3-
import javax.persistence.Column;
4-
import javax.persistence.Entity;
5-
import javax.persistence.GeneratedValue;
6-
import javax.persistence.Id;
3+
import javax.persistence.Inheritance;
4+
import javax.persistence.InheritanceType;
5+
import javax.persistence.MappedSuperclass;
76
import javax.persistence.Version;
87

9-
@Entity
8+
@MappedSuperclass
9+
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
1010
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-
}
2411

2512
@Version
2613
private Long version;

dao/src/main/java/com/iluwatar/DaoBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private SessionFactory createSessionFactory() {
2727
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1")
2828
.setProperty("hibernate.current_session_context_class", "thread")
2929
.setProperty("hibernate.show_sql", "true")
30-
.setProperty("hibernate.hbm2ddl.auto", "create")
30+
.setProperty("hibernate.hbm2ddl.auto", "create-drop")
3131
.buildSessionFactory();
3232
return sessionFactory;
3333
}

dao/src/main/java/com/iluwatar/Spell.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.iluwatar;
22

3+
import javax.persistence.Column;
34
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.Id;
7+
import javax.persistence.Inheritance;
8+
import javax.persistence.InheritanceType;
49
import javax.persistence.JoinColumn;
510
import javax.persistence.ManyToOne;
611
import javax.persistence.Table;
@@ -19,8 +24,21 @@ public Spell(String name) {
1924
this.name = name;
2025
}
2126

27+
@Id
28+
@GeneratedValue
29+
@Column(name = "SPELL_ID")
30+
private Long id;
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
2240
@ManyToOne
23-
@JoinColumn(name="SPELLBOOK_ID_FK", referencedColumnName="ID")
41+
@JoinColumn(name="SPELLBOOK_ID_FK", referencedColumnName="SPELLBOOK_ID")
2442
private Spellbook spellbook;
2543

2644
public String getName() {
@@ -31,6 +49,14 @@ public void setName(String name) {
3149
this.name = name;
3250
}
3351

52+
public Spellbook getSpellbook() {
53+
return spellbook;
54+
}
55+
56+
public void setSpellbook(Spellbook spellbook) {
57+
this.spellbook = spellbook;
58+
}
59+
3460
@Override
3561
public String toString() {
3662
return name;

dao/src/main/java/com/iluwatar/Spellbook.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
import java.util.Set;
55

66
import javax.persistence.CascadeType;
7+
import javax.persistence.Column;
78
import javax.persistence.Entity;
9+
import javax.persistence.FetchType;
10+
import javax.persistence.GeneratedValue;
11+
import javax.persistence.Id;
12+
import javax.persistence.Inheritance;
13+
import javax.persistence.InheritanceType;
814
import javax.persistence.JoinColumn;
915
import javax.persistence.ManyToOne;
1016
import javax.persistence.OneToMany;
@@ -23,13 +29,26 @@ public Spellbook(String name) {
2329
this.name = name;
2430
}
2531

32+
@Id
33+
@GeneratedValue
34+
@Column(name = "SPELLBOOK_ID")
35+
private Long id;
36+
37+
public Long getId() {
38+
return id;
39+
}
40+
41+
public void setId(Long id) {
42+
this.id = id;
43+
}
44+
2645
private String name;
2746

2847
@ManyToOne
29-
@JoinColumn(name="WIZARD_ID_FK", referencedColumnName="ID")
48+
@JoinColumn(name="WIZARD_ID_FK", referencedColumnName="WIZARD_ID")
3049
private Wizard wizard;
3150

32-
@OneToMany(mappedBy = "spellbook", orphanRemoval = true, cascade = CascadeType.ALL)
51+
@OneToMany(mappedBy = "spellbook", orphanRemoval = true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
3352
private Set<Spell> spells;
3453

3554
public String getName() {

dao/src/main/java/com/iluwatar/Wizard.java

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

66
import javax.persistence.CascadeType;
7+
import javax.persistence.Column;
78
import javax.persistence.Entity;
9+
import javax.persistence.FetchType;
10+
import javax.persistence.GeneratedValue;
11+
import javax.persistence.Id;
12+
import javax.persistence.Inheritance;
13+
import javax.persistence.InheritanceType;
814
import javax.persistence.OneToMany;
915
import javax.persistence.Table;
1016

@@ -20,10 +26,23 @@ public Wizard(String name) {
2026
this();
2127
this.name = name;
2228
}
29+
30+
@Id
31+
@GeneratedValue
32+
@Column(name = "WIZARD_ID")
33+
private Long id;
34+
35+
public Long getId() {
36+
return id;
37+
}
38+
39+
public void setId(Long id) {
40+
this.id = id;
41+
}
2342

2443
private String name;
2544

26-
@OneToMany(mappedBy = "wizard", orphanRemoval = true, cascade = CascadeType.ALL)
45+
@OneToMany(mappedBy = "wizard", orphanRemoval = true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
2746
private Set<Spellbook> spellbooks;
2847

2948
public String getFirstName() {

0 commit comments

Comments
 (0)