parent
41d7ec50a1
commit
b567fba474
|
@ -12,6 +12,16 @@ import com.baeldung.hibernate.pojo.Course;
|
|||
import com.baeldung.hibernate.pojo.Student;
|
||||
import com.baeldung.hibernate.pojo.User;
|
||||
import com.baeldung.hibernate.pojo.UserProfile;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Animal;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Bag;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Book;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Car;
|
||||
import com.baeldung.hibernate.pojo.inheritance.MyEmployee;
|
||||
import com.baeldung.hibernate.pojo.inheritance.MyProduct;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Pen;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Person;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Pet;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Vehicle;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.Metadata;
|
||||
|
@ -50,6 +60,16 @@ public class HibernateUtil {
|
|||
metadataSources.addAnnotatedClass(OrderEntry.class);
|
||||
metadataSources.addAnnotatedClass(OrderEntryIdClass.class);
|
||||
metadataSources.addAnnotatedClass(UserProfile.class);
|
||||
metadataSources.addAnnotatedClass(Book.class);
|
||||
metadataSources.addAnnotatedClass(MyEmployee.class);
|
||||
metadataSources.addAnnotatedClass(MyProduct.class);
|
||||
metadataSources.addAnnotatedClass(Pen.class);
|
||||
metadataSources.addAnnotatedClass(Person.class);
|
||||
metadataSources.addAnnotatedClass(Animal.class);
|
||||
metadataSources.addAnnotatedClass(Pet.class);
|
||||
metadataSources.addAnnotatedClass(Vehicle.class);
|
||||
metadataSources.addAnnotatedClass(Car.class);
|
||||
metadataSources.addAnnotatedClass(Bag.class);
|
||||
|
||||
Metadata metadata = metadataSources.buildMetadata();
|
||||
return metadata.getSessionFactoryBuilder()
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.hibernate.pojo.inheritance;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.JOINED)
|
||||
public class Animal {
|
||||
|
||||
@Id
|
||||
private long animalId;
|
||||
|
||||
private String species;
|
||||
|
||||
public Animal() {}
|
||||
|
||||
public Animal(long animalId, String species) {
|
||||
this.animalId = animalId;
|
||||
this.species = species;
|
||||
}
|
||||
|
||||
public long getAnimalId() {
|
||||
return animalId;
|
||||
}
|
||||
|
||||
public void setAnimalId(long animalId) {
|
||||
this.animalId = animalId;
|
||||
}
|
||||
|
||||
public String getSpecies() {
|
||||
return species;
|
||||
}
|
||||
|
||||
public void setSpecies(String species) {
|
||||
this.species = species;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.hibernate.pojo.inheritance;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.annotations.Polymorphism;
|
||||
import org.hibernate.annotations.PolymorphismType;
|
||||
|
||||
@Entity
|
||||
@Polymorphism(type = PolymorphismType.EXPLICIT)
|
||||
public class Bag implements Item {
|
||||
|
||||
@Id
|
||||
private long bagId;
|
||||
|
||||
private String type;
|
||||
|
||||
public Bag(long bagId, String type) {
|
||||
this.bagId = bagId;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public long getBagId() {
|
||||
return bagId;
|
||||
}
|
||||
|
||||
public void setBagId(long bagId) {
|
||||
this.bagId = bagId;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.hibernate.pojo.inheritance;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue("1")
|
||||
public class Book extends MyProduct {
|
||||
private String author;
|
||||
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Book(long productId, String name, String author) {
|
||||
super(productId, name);
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.hibernate.pojo.inheritance;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class Car extends Vehicle {
|
||||
private String engine;
|
||||
|
||||
public Car() {
|
||||
}
|
||||
|
||||
public Car(long vehicleId, String manufacturer, String engine) {
|
||||
super(vehicleId, manufacturer);
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
public String getEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
public void setEngine(String engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.hibernate.pojo.inheritance;
|
||||
|
||||
public interface Item {
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.hibernate.pojo.inheritance;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class MyEmployee extends Person {
|
||||
private String company;
|
||||
|
||||
public MyEmployee(long personId, String name, String company) {
|
||||
super(personId, name);
|
||||
this.company = company;
|
||||
}
|
||||
|
||||
public String getCompany() {
|
||||
return company;
|
||||
}
|
||||
|
||||
public void setCompany(String company) {
|
||||
this.company = company;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.hibernate.pojo.inheritance;
|
||||
|
||||
import javax.persistence.DiscriminatorColumn;
|
||||
import javax.persistence.DiscriminatorType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
|
||||
import org.hibernate.annotations.DiscriminatorFormula;
|
||||
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(name = "product_type", discriminatorType = DiscriminatorType.INTEGER)
|
||||
// @DiscriminatorFormula("case when author is not null then 1 else 2 end")
|
||||
public class MyProduct {
|
||||
@Id
|
||||
private long productId;
|
||||
|
||||
private String name;
|
||||
|
||||
public MyProduct() {
|
||||
}
|
||||
|
||||
public MyProduct(long productId, String name) {
|
||||
super();
|
||||
this.productId = productId;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getProductId() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
public void setProductId(long productId) {
|
||||
this.productId = productId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.hibernate.pojo.inheritance;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue("2")
|
||||
public class Pen extends MyProduct {
|
||||
private String color;
|
||||
|
||||
public Pen() {
|
||||
}
|
||||
|
||||
public Pen(long productId, String name, String color) {
|
||||
super(productId, name);
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.hibernate.pojo.inheritance;
|
||||
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
|
||||
@MappedSuperclass
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private long personId;
|
||||
|
||||
private String name;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(long personId, String name) {
|
||||
this.personId = personId;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getPersonId() {
|
||||
return personId;
|
||||
}
|
||||
|
||||
public void setPersonId(long personId) {
|
||||
this.personId = personId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.hibernate.pojo.inheritance;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.PrimaryKeyJoinColumn;
|
||||
|
||||
@Entity
|
||||
@PrimaryKeyJoinColumn(name = "petId")
|
||||
public class Pet extends Animal {
|
||||
private String name;
|
||||
|
||||
public Pet() {
|
||||
}
|
||||
|
||||
public Pet(long animalId, String species, String name) {
|
||||
super(animalId, species);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.hibernate.pojo.inheritance;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
|
||||
public class Vehicle {
|
||||
@Id
|
||||
private long vehicleId;
|
||||
|
||||
private String manufacturer;
|
||||
|
||||
public Vehicle() {
|
||||
}
|
||||
|
||||
public Vehicle(long vehicleId, String manufacturer) {
|
||||
this.vehicleId = vehicleId;
|
||||
this.manufacturer = manufacturer;
|
||||
}
|
||||
|
||||
public long getVehicleId() {
|
||||
return vehicleId;
|
||||
}
|
||||
|
||||
public void setVehicleId(long vehicleId) {
|
||||
this.vehicleId = vehicleId;
|
||||
}
|
||||
|
||||
public String getManufacturer() {
|
||||
return manufacturer;
|
||||
}
|
||||
|
||||
public void setManufacturer(String manufacturer) {
|
||||
this.manufacturer = manufacturer;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
package com.baeldung.hibernate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.hibernate.pojo.inheritance.Bag;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Book;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Car;
|
||||
import com.baeldung.hibernate.pojo.inheritance.MyEmployee;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Pen;
|
||||
import com.baeldung.hibernate.pojo.inheritance.Pet;
|
||||
|
||||
public class InheritanceMappingIntegrationTest {
|
||||
private Session session;
|
||||
|
||||
private Transaction transaction;
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
session = HibernateUtil.getSessionFactory()
|
||||
.openSession();
|
||||
transaction = session.beginTransaction();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
transaction.rollback();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSubclasses_whenQuerySingleTableSuperclass_thenOk() {
|
||||
Book book = new Book(1, "1984", "George Orwell");
|
||||
session.save(book);
|
||||
Pen pen = new Pen(2, "my pen", "blue");
|
||||
session.save(pen);
|
||||
|
||||
assertThat(session.createQuery("from MyProduct")
|
||||
.getResultList()
|
||||
.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSubclasses_whenQueryMappedSuperclass_thenOk() {
|
||||
MyEmployee emp = new MyEmployee(1, "john", "baeldung");
|
||||
session.save(emp);
|
||||
|
||||
assertThat(session.createQuery("from com.baeldung.hibernate.pojo.inheritance.Person")
|
||||
.getResultList()
|
||||
.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSubclasses_whenQueryJoinedTableSuperclass_thenOk() {
|
||||
Pet pet = new Pet(1, "dog", "lassie");
|
||||
session.save(pet);
|
||||
|
||||
assertThat(session.createQuery("from Animal")
|
||||
.getResultList()
|
||||
.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSubclasses_whenQueryTablePerClassSuperclass_thenOk() {
|
||||
Car car = new Car(1, "audi", "xyz");
|
||||
session.save(car);
|
||||
|
||||
assertThat(session.createQuery("from Vehicle")
|
||||
.getResultList()
|
||||
.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSubclasses_whenQueryNonMappedInterface_thenOk() {
|
||||
Bag bag = new Bag(1, "large");
|
||||
session.save(bag);
|
||||
|
||||
assertThat(session.createQuery("from com.baeldung.hibernate.pojo.inheritance.Item")
|
||||
.getResultList()
|
||||
.size()).isEqualTo(0);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue