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) {
|
||||
Spell spell = new Spell("Fireball");
|
||||
Spellbook spellbook = new Spellbook("Book of fire");
|
||||
spell.setSpellbook(spellbook);
|
||||
spellbook.getSpells().add(spell);
|
||||
Wizard wizard = new Wizard("Jugga");
|
||||
spellbook.setWizard(wizard);
|
||||
wizard.getSpellbooks().add(spellbook);
|
||||
dao.persist(wizard);
|
||||
}
|
||||
@ -23,6 +25,12 @@ public class App {
|
||||
List<Wizard> wizards = dao.findAll();
|
||||
for (Wizard w: wizards) {
|
||||
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;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
@MappedSuperclass
|
||||
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
|
||||
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
|
||||
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.current_session_context_class", "thread")
|
||||
.setProperty("hibernate.show_sql", "true")
|
||||
.setProperty("hibernate.hbm2ddl.auto", "create")
|
||||
.setProperty("hibernate.hbm2ddl.auto", "create-drop")
|
||||
.buildSessionFactory();
|
||||
return sessionFactory;
|
||||
}
|
||||
|
@ -1,6 +1,11 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import javax.persistence.Column;
|
||||
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.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
@ -19,8 +24,21 @@ public class Spell extends BaseEntity {
|
||||
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
|
||||
@JoinColumn(name="SPELLBOOK_ID_FK", referencedColumnName="ID")
|
||||
@JoinColumn(name="SPELLBOOK_ID_FK", referencedColumnName="SPELLBOOK_ID")
|
||||
private Spellbook spellbook;
|
||||
|
||||
public String getName() {
|
||||
@ -31,6 +49,14 @@ public class Spell extends BaseEntity {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Spellbook getSpellbook() {
|
||||
return spellbook;
|
||||
}
|
||||
|
||||
public void setSpellbook(Spellbook spellbook) {
|
||||
this.spellbook = spellbook;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
|
@ -4,7 +4,13 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
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.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
@ -23,13 +29,26 @@ public class Spellbook extends BaseEntity {
|
||||
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;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name="WIZARD_ID_FK", referencedColumnName="ID")
|
||||
@JoinColumn(name="WIZARD_ID_FK", referencedColumnName="WIZARD_ID")
|
||||
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;
|
||||
|
||||
public String getName() {
|
||||
|
@ -4,7 +4,13 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
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.Table;
|
||||
|
||||
@ -20,10 +26,23 @@ public class Wizard extends BaseEntity {
|
||||
this();
|
||||
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;
|
||||
|
||||
@OneToMany(mappedBy = "wizard", orphanRemoval = true, cascade = CascadeType.ALL)
|
||||
@OneToMany(mappedBy = "wizard", orphanRemoval = true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
|
||||
private Set<Spellbook> spellbooks;
|
||||
|
||||
public String getFirstName() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user