Skip to content

Commit b567fba

Browse files
lor6pivovarit
authored andcommitted
Bael 1337 (eugenp#3072)
* inheritance ex * add more ex and tests, formatting
1 parent 41d7ec5 commit b567fba

13 files changed

Lines changed: 445 additions & 0 deletions

File tree

‎hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
import com.baeldung.hibernate.pojo.Student;
1313
import com.baeldung.hibernate.pojo.User;
1414
import com.baeldung.hibernate.pojo.UserProfile;
15+
import com.baeldung.hibernate.pojo.inheritance.Animal;
16+
import com.baeldung.hibernate.pojo.inheritance.Bag;
17+
import com.baeldung.hibernate.pojo.inheritance.Book;
18+
import com.baeldung.hibernate.pojo.inheritance.Car;
19+
import com.baeldung.hibernate.pojo.inheritance.MyEmployee;
20+
import com.baeldung.hibernate.pojo.inheritance.MyProduct;
21+
import com.baeldung.hibernate.pojo.inheritance.Pen;
22+
import com.baeldung.hibernate.pojo.inheritance.Person;
23+
import com.baeldung.hibernate.pojo.inheritance.Pet;
24+
import com.baeldung.hibernate.pojo.inheritance.Vehicle;
1525

1626
import org.hibernate.SessionFactory;
1727
import org.hibernate.boot.Metadata;
@@ -50,6 +60,16 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
5060
metadataSources.addAnnotatedClass(OrderEntry.class);
5161
metadataSources.addAnnotatedClass(OrderEntryIdClass.class);
5262
metadataSources.addAnnotatedClass(UserProfile.class);
63+
metadataSources.addAnnotatedClass(Book.class);
64+
metadataSources.addAnnotatedClass(MyEmployee.class);
65+
metadataSources.addAnnotatedClass(MyProduct.class);
66+
metadataSources.addAnnotatedClass(Pen.class);
67+
metadataSources.addAnnotatedClass(Person.class);
68+
metadataSources.addAnnotatedClass(Animal.class);
69+
metadataSources.addAnnotatedClass(Pet.class);
70+
metadataSources.addAnnotatedClass(Vehicle.class);
71+
metadataSources.addAnnotatedClass(Car.class);
72+
metadataSources.addAnnotatedClass(Bag.class);
5373

5474
Metadata metadata = metadataSources.buildMetadata();
5575
return metadata.getSessionFactoryBuilder()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.hibernate.pojo.inheritance;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
import javax.persistence.Inheritance;
6+
import javax.persistence.InheritanceType;
7+
8+
@Entity
9+
@Inheritance(strategy = InheritanceType.JOINED)
10+
public class Animal {
11+
12+
@Id
13+
private long animalId;
14+
15+
private String species;
16+
17+
public Animal() {}
18+
19+
public Animal(long animalId, String species) {
20+
this.animalId = animalId;
21+
this.species = species;
22+
}
23+
24+
public long getAnimalId() {
25+
return animalId;
26+
}
27+
28+
public void setAnimalId(long animalId) {
29+
this.animalId = animalId;
30+
}
31+
32+
public String getSpecies() {
33+
return species;
34+
}
35+
36+
public void setSpecies(String species) {
37+
this.species = species;
38+
}
39+
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.hibernate.pojo.inheritance;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
import org.hibernate.annotations.Polymorphism;
7+
import org.hibernate.annotations.PolymorphismType;
8+
9+
@Entity
10+
@Polymorphism(type = PolymorphismType.EXPLICIT)
11+
public class Bag implements Item {
12+
13+
@Id
14+
private long bagId;
15+
16+
private String type;
17+
18+
public Bag(long bagId, String type) {
19+
this.bagId = bagId;
20+
this.type = type;
21+
}
22+
23+
public long getBagId() {
24+
return bagId;
25+
}
26+
27+
public void setBagId(long bagId) {
28+
this.bagId = bagId;
29+
}
30+
31+
public String getType() {
32+
return type;
33+
}
34+
35+
public void setType(String type) {
36+
this.type = type;
37+
}
38+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.hibernate.pojo.inheritance;
2+
3+
import javax.persistence.DiscriminatorValue;
4+
import javax.persistence.Entity;
5+
6+
@Entity
7+
@DiscriminatorValue("1")
8+
public class Book extends MyProduct {
9+
private String author;
10+
11+
public Book() {
12+
}
13+
14+
public Book(long productId, String name, String author) {
15+
super(productId, name);
16+
this.author = author;
17+
}
18+
19+
public String getAuthor() {
20+
return author;
21+
}
22+
23+
public void setAuthor(String author) {
24+
this.author = author;
25+
}
26+
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.hibernate.pojo.inheritance;
2+
3+
import javax.persistence.Entity;
4+
5+
@Entity
6+
public class Car extends Vehicle {
7+
private String engine;
8+
9+
public Car() {
10+
}
11+
12+
public Car(long vehicleId, String manufacturer, String engine) {
13+
super(vehicleId, manufacturer);
14+
this.engine = engine;
15+
}
16+
17+
public String getEngine() {
18+
return engine;
19+
}
20+
21+
public void setEngine(String engine) {
22+
this.engine = engine;
23+
}
24+
25+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.hibernate.pojo.inheritance;
2+
3+
public interface Item {
4+
5+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.hibernate.pojo.inheritance;
2+
3+
import javax.persistence.Entity;
4+
5+
@Entity
6+
public class MyEmployee extends Person {
7+
private String company;
8+
9+
public MyEmployee(long personId, String name, String company) {
10+
super(personId, name);
11+
this.company = company;
12+
}
13+
14+
public String getCompany() {
15+
return company;
16+
}
17+
18+
public void setCompany(String company) {
19+
this.company = company;
20+
}
21+
22+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.hibernate.pojo.inheritance;
2+
3+
import javax.persistence.DiscriminatorColumn;
4+
import javax.persistence.DiscriminatorType;
5+
import javax.persistence.Entity;
6+
import javax.persistence.Id;
7+
import javax.persistence.Inheritance;
8+
import javax.persistence.InheritanceType;
9+
10+
import org.hibernate.annotations.DiscriminatorFormula;
11+
12+
@Entity
13+
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
14+
@DiscriminatorColumn(name = "product_type", discriminatorType = DiscriminatorType.INTEGER)
15+
// @DiscriminatorFormula("case when author is not null then 1 else 2 end")
16+
public class MyProduct {
17+
@Id
18+
private long productId;
19+
20+
private String name;
21+
22+
public MyProduct() {
23+
}
24+
25+
public MyProduct(long productId, String name) {
26+
super();
27+
this.productId = productId;
28+
this.name = name;
29+
}
30+
31+
public long getProductId() {
32+
return productId;
33+
}
34+
35+
public void setProductId(long productId) {
36+
this.productId = productId;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.hibernate.pojo.inheritance;
2+
3+
import javax.persistence.DiscriminatorValue;
4+
import javax.persistence.Entity;
5+
6+
@Entity
7+
@DiscriminatorValue("2")
8+
public class Pen extends MyProduct {
9+
private String color;
10+
11+
public Pen() {
12+
}
13+
14+
public Pen(long productId, String name, String color) {
15+
super(productId, name);
16+
this.color = color;
17+
}
18+
19+
public String getColor() {
20+
return color;
21+
}
22+
23+
public void setColor(String color) {
24+
this.color = color;
25+
}
26+
27+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.hibernate.pojo.inheritance;
2+
3+
import javax.persistence.Id;
4+
import javax.persistence.MappedSuperclass;
5+
6+
@MappedSuperclass
7+
public class Person {
8+
9+
@Id
10+
private long personId;
11+
12+
private String name;
13+
14+
public Person() {
15+
}
16+
17+
public Person(long personId, String name) {
18+
this.personId = personId;
19+
this.name = name;
20+
}
21+
22+
public long getPersonId() {
23+
return personId;
24+
}
25+
26+
public void setPersonId(long personId) {
27+
this.personId = personId;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)