Work on DAO example. Table per class strategy. Persist fixes.
This commit is contained in:
parent
82eebeaee0
commit
08c3145c47
@ -13,8 +13,10 @@ public class App {
|
|||||||
public static void persistData(WizardDao dao) {
|
public static void persistData(WizardDao dao) {
|
||||||
Spell spell = new Spell("Fireball");
|
Spell spell = new Spell("Fireball");
|
||||||
Spellbook spellbook = new Spellbook("Book of fire");
|
Spellbook spellbook = new Spellbook("Book of fire");
|
||||||
|
spell.setSpellbook(spellbook);
|
||||||
spellbook.getSpells().add(spell);
|
spellbook.getSpells().add(spell);
|
||||||
Wizard wizard = new Wizard("Jugga");
|
Wizard wizard = new Wizard("Jugga");
|
||||||
|
spellbook.setWizard(wizard);
|
||||||
wizard.getSpellbooks().add(spellbook);
|
wizard.getSpellbooks().add(spellbook);
|
||||||
dao.persist(wizard);
|
dao.persist(wizard);
|
||||||
}
|
}
|
||||||
@ -23,6 +25,12 @@ public class App {
|
|||||||
List<Wizard> wizards = dao.findAll();
|
List<Wizard> wizards = dao.findAll();
|
||||||
for (Wizard w: wizards) {
|
for (Wizard w: wizards) {
|
||||||
System.out.println(w);
|
System.out.println(w);
|
||||||
|
for (Spellbook spellbook: w.getSpellbooks()) {
|
||||||
|
System.out.println(spellbook);
|
||||||
|
for (Spell spell: spellbook.getSpells()) {
|
||||||
|
System.out.println(spell);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,13 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Inheritance;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.InheritanceType;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.MappedSuperclass;
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.persistence.Version;
|
import javax.persistence.Version;
|
||||||
|
|
||||||
@Entity
|
@MappedSuperclass
|
||||||
|
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
|
||||||
public class BaseEntity {
|
public class BaseEntity {
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue
|
|
||||||
@Column(name = "ID")
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Version
|
@Version
|
||||||
private Long version;
|
private Long version;
|
||||||
|
@ -27,7 +27,7 @@ public abstract class DaoBase<E extends BaseEntity> {
|
|||||||
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1")
|
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1")
|
||||||
.setProperty("hibernate.current_session_context_class", "thread")
|
.setProperty("hibernate.current_session_context_class", "thread")
|
||||||
.setProperty("hibernate.show_sql", "true")
|
.setProperty("hibernate.show_sql", "true")
|
||||||
.setProperty("hibernate.hbm2ddl.auto", "create")
|
.setProperty("hibernate.hbm2ddl.auto", "create-drop")
|
||||||
.buildSessionFactory();
|
.buildSessionFactory();
|
||||||
return sessionFactory;
|
return sessionFactory;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Inheritance;
|
||||||
|
import javax.persistence.InheritanceType;
|
||||||
import javax.persistence.JoinColumn;
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
@ -19,8 +24,21 @@ public class Spell extends BaseEntity {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
@Column(name = "SPELL_ID")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name="SPELLBOOK_ID_FK", referencedColumnName="ID")
|
@JoinColumn(name="SPELLBOOK_ID_FK", referencedColumnName="SPELLBOOK_ID")
|
||||||
private Spellbook spellbook;
|
private Spellbook spellbook;
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@ -31,6 +49,14 @@ public class Spell extends BaseEntity {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Spellbook getSpellbook() {
|
||||||
|
return spellbook;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSpellbook(Spellbook spellbook) {
|
||||||
|
this.spellbook = spellbook;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return name;
|
return name;
|
||||||
|
@ -4,7 +4,13 @@ import java.util.HashSet;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.persistence.CascadeType;
|
import javax.persistence.CascadeType;
|
||||||
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.FetchType;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Inheritance;
|
||||||
|
import javax.persistence.InheritanceType;
|
||||||
import javax.persistence.JoinColumn;
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
import javax.persistence.OneToMany;
|
import javax.persistence.OneToMany;
|
||||||
@ -23,13 +29,26 @@ public class Spellbook extends BaseEntity {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
@Column(name = "SPELLBOOK_ID")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name="WIZARD_ID_FK", referencedColumnName="ID")
|
@JoinColumn(name="WIZARD_ID_FK", referencedColumnName="WIZARD_ID")
|
||||||
private Wizard wizard;
|
private Wizard wizard;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "spellbook", orphanRemoval = true, cascade = CascadeType.ALL)
|
@OneToMany(mappedBy = "spellbook", orphanRemoval = true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
|
||||||
private Set<Spell> spells;
|
private Set<Spell> spells;
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -4,7 +4,13 @@ import java.util.HashSet;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.persistence.CascadeType;
|
import javax.persistence.CascadeType;
|
||||||
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.FetchType;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Inheritance;
|
||||||
|
import javax.persistence.InheritanceType;
|
||||||
import javax.persistence.OneToMany;
|
import javax.persistence.OneToMany;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@ -20,10 +26,23 @@ public class Wizard extends BaseEntity {
|
|||||||
this();
|
this();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
@Column(name = "WIZARD_ID")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "wizard", orphanRemoval = true, cascade = CascadeType.ALL)
|
@OneToMany(mappedBy = "wizard", orphanRemoval = true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
|
||||||
private Set<Spellbook> spellbooks;
|
private Set<Spellbook> spellbooks;
|
||||||
|
|
||||||
public String getFirstName() {
|
public String getFirstName() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user