Dyskryminator to typ który grupuje wiersze w tabeli.
- Stworzenie tabeli w bazie danych
- Stworzenie klas Entity
- Klasa testująca wyświetlanie danych
- Klasa testująca dodawanie danych
✅ Stworzenie tabeli w bazie danych
CREATE DATABASE IF NOT EXISTS `bazatestowa2`;
USE `bazatestowa2`;
CREATE TABLE IF NOT EXISTS animal (
animal_id INT AUTO_INCREMENT,
animal_name VARCHAR(40) DEFAULT NULL,
animal_type VARCHAR(40) DEFAULT NULL,
PRIMARY KEY (owner_id)
) ENGINE=InnoDB;
✅ Stworzenie klas Entity
👉 Entity Animal
package myhibernate.discriminator.entity;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
@Entity
@Table(name="animal")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="animal_type",
discriminatorType = DiscriminatorType.STRING)
public class Animal {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="animal_id")
private long animal_id;
@Column(name="animal_name")
private String animalName;
public String getAnimalName() {
return animalName;
}
public void setAnimalName(String animalName) {
this.animalName = animalName;
}
@Override
public String toString() {
return "Animal [animal_id=" + animal_id + ", animalName=" + animalName + "]";
}
}
👉 Entity Ptak
package myhibernate.discriminator.entity;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("ptak")
public class Ptak extends Animal {
}
👉 Entity Ssak
package myhibernate.discriminator.entity;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("ssak")
public class Ssak extends Animal {
}
✅ Klasa testująca wyświetlanie danych
package myhibernate.discriminator;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import myhibernate.discriminator.entity.Animal;
import myhibernate.discriminator.entity.Ptak;
import myhibernate.discriminator.entity.Ssak;
public class DiscriminatorApp {
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.configure("hibernate.cfg.xml");
conf.addAnnotatedClass(Animal.class);
conf.addAnnotatedClass(Ssak.class);
conf.addAnnotatedClass(Ptak.class);
SessionFactory factory = conf.buildSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
String sql = "select o from Animal o ";
Query<Animal> query = session.createQuery(sql);
for(Animal animal : query.list()) {
if (animal instanceof Ssak) {
System.out.println("Animal: " + animal);
}
}
session.getTransaction().commit();
factory.close();
}
}
❗ Wynik działania w konsoli:
✅ Klasa testująca dodawanie danych
package myhibernate.discriminator;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import myhibernate.discriminator.entity.Animal;
import myhibernate.discriminator.entity.Ptak;
import myhibernate.discriminator.entity.Ssak;
public class AddDiscriminatorApp {
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.configure("hibernate.cfg.xml");
conf.addAnnotatedClass(Animal.class);
conf.addAnnotatedClass(Ssak.class);
conf.addAnnotatedClass(Ptak.class);
SessionFactory factory = conf.buildSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
Ssak ssak = new Ssak();
ssak.setAnimalName("delfin");
session.persist(ssak);
session.getTransaction().commit();
factory.close();
}
}
❗ Wynik działania w konsoli:
❗Wynik w bazie danych:
1. tabela animal
Brak komentarzy:
Prześlij komentarz