-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the single-table, joined and table-per-entity inheritance
strategy examples. Added a mapped superclass example.
- Loading branch information
Showing
21 changed files
with
538 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>example</groupId> | ||
<artifactId>jpa10</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.hibernate</groupId> | ||
<artifactId>hibernate-core-jakarta</artifactId> | ||
<version>5.5.7.Final</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish.jaxb</groupId> | ||
<artifactId>jaxb-runtime</artifactId> | ||
<version>3.0.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.postgresql</groupId> | ||
<artifactId>postgresql</artifactId> | ||
<version>42.2.23.jre7</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>example</groupId> | ||
<artifactId>jpaunit</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
29 changes: 29 additions & 0 deletions
29
jpa10/src/main/java/example/jpa/inheritance/TestJoined.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package example.jpa.inheritance; | ||
|
||
import example.jpa.inheritance.entities.Chocolate; | ||
import example.jpa.inheritance.entities.Product; | ||
import example.jpa.inheritance.entities.Wine; | ||
import example.jpaunit.JPAUnit; | ||
|
||
public class TestJoined { | ||
public static void main(String[] args) { | ||
var product = new Product(); | ||
product.setName("Sugar"); | ||
|
||
var choco = new Chocolate(); | ||
choco.setName("Ritter Sport"); | ||
choco.setCalories(575.0); | ||
|
||
var wine = new Wine(); | ||
wine.setName("Tsinandali"); | ||
wine.setYear(2018); | ||
|
||
try (var unit = new JPAUnit("postgres-unit")) { | ||
unit.runTransaction(entityManager -> { | ||
entityManager.persist(product); | ||
entityManager.persist(choco); | ||
entityManager.persist(wine); | ||
}); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
jpa10/src/main/java/example/jpa/inheritance/TestMappedSuperclass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package example.jpa.inheritance; | ||
|
||
import example.jpa.inheritance.entities.Bicycle; | ||
import example.jpa.inheritance.entities.Car; | ||
import example.jpaunit.JPAUnit; | ||
|
||
public class TestMappedSuperclass { | ||
public static void main(String[] args) { | ||
var car = new Car(); | ||
car.setColor("Black"); | ||
car.setHorsePower(140); | ||
|
||
var bike = new Bicycle(); | ||
bike.setColor("Silver"); | ||
bike.setModel("Juggernaut"); | ||
|
||
try (var unit = new JPAUnit("postgres-unit")) { | ||
unit.runTransaction(entityManager -> { | ||
entityManager.persist(car); | ||
entityManager.persist(bike); | ||
}); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
jpa10/src/main/java/example/jpa/inheritance/TestSingleTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package example.jpa.inheritance; | ||
|
||
import example.jpa.inheritance.entities.Bug; | ||
import example.jpa.inheritance.entities.Feature; | ||
import example.jpa.inheritance.entities.Issue; | ||
import example.jpaunit.JPAUnit; | ||
|
||
public class TestSingleTable { | ||
public static void main(String[] args) { | ||
var issue = new Issue(); | ||
issue.setTitle("Make our software great again"); | ||
issue.setDescription("Our software is not great anymore. " + | ||
"This has to be sorted out!!!"); | ||
|
||
var bug = new Bug(); | ||
bug.setTitle("It doesn't work"); | ||
bug.setDescription("Whatever I do, nothing happens."); | ||
bug.setSeverity(1); | ||
|
||
var feature = new Feature(); | ||
feature.setTitle("Integrate the spreadsheet into our software"); | ||
feature.setDescription("I feel lazy switching between windows " + | ||
"and it greatly reduces my productivity as a user."); | ||
feature.setTargetVersion("15RC"); | ||
|
||
try (var unit = new JPAUnit("postgres-unit")) { | ||
unit.runTransaction(entityManager -> { | ||
entityManager.persist(issue); | ||
entityManager.persist(bug); | ||
entityManager.persist(feature); | ||
}); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
jpa10/src/main/java/example/jpa/inheritance/TestTablePerEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package example.jpa.inheritance; | ||
|
||
import example.jpa.inheritance.entities.Employee; | ||
import example.jpa.inheritance.entities.Manager; | ||
import example.jpaunit.JPAUnit; | ||
|
||
public class TestTablePerEntity { | ||
public static void main(String[] args) { | ||
var employee = new Employee(); | ||
employee.setName("Вася Пупкин"); | ||
var manager = new Manager(); | ||
manager.setName("Кощей Бессмертный"); | ||
manager.setResponsibility("Сеять плохое, злое и вечное."); | ||
|
||
try (var unit = new JPAUnit("postgres-unit")) { | ||
unit.runTransaction(entityManager -> { | ||
entityManager.persist(employee); | ||
entityManager.persist(manager); | ||
}); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
jpa10/src/main/java/example/jpa/inheritance/entities/Bicycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package example.jpa.inheritance.entities; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "bicycles") | ||
public class Bicycle extends Vehicle { | ||
private String model; | ||
|
||
public String getModel() { | ||
return model; | ||
} | ||
|
||
public void setModel(String model) { | ||
this.model = model; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
jpa10/src/main/java/example/jpa/inheritance/entities/Bug.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package example.jpa.inheritance.entities; | ||
|
||
import jakarta.persistence.DiscriminatorValue; | ||
import jakarta.persistence.Entity; | ||
|
||
@Entity | ||
@DiscriminatorValue("B") | ||
public class Bug extends Issue { | ||
private int severity; | ||
|
||
public int getSeverity() { | ||
return severity; | ||
} | ||
|
||
public void setSeverity(int severity) { | ||
this.severity = severity; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
jpa10/src/main/java/example/jpa/inheritance/entities/Car.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package example.jpa.inheritance.entities; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "cars") | ||
public class Car extends Vehicle{ | ||
@Column(name = "horse_power") | ||
private int horsePower; | ||
|
||
public int getHorsePower() { | ||
return horsePower; | ||
} | ||
|
||
public void setHorsePower(int horsePower) { | ||
this.horsePower = horsePower; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
jpa10/src/main/java/example/jpa/inheritance/entities/Chocolate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package example.jpa.inheritance.entities; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "chocolates") | ||
public class Chocolate extends Product { | ||
double calories; | ||
|
||
public double getCalories() { | ||
return calories; | ||
} | ||
|
||
public void setCalories(double calories) { | ||
this.calories = calories; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
jpa10/src/main/java/example/jpa/inheritance/entities/Employee.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package example.jpa.inheritance.entities; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Inheritance; | ||
import jakarta.persistence.InheritanceType; | ||
import jakarta.persistence.SequenceGenerator; | ||
import jakarta.persistence.Table; | ||
|
||
/** | ||
* Employee entity. | ||
* | ||
* This entity demonstrates table-per-entity inheritance strategy. | ||
* It can't be used with the IDENTITY ID generation, so a SEQUENCE generation | ||
* is used instead. | ||
*/ | ||
@Entity | ||
@Table(name = "employees") | ||
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) | ||
@SequenceGenerator(name = "employee_id_generator", | ||
sequenceName = "employee_id_sequence", | ||
allocationSize = 5) | ||
public class Employee { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE, | ||
generator = "employee_id_generator") | ||
private int id; | ||
|
||
private String name; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
jpa10/src/main/java/example/jpa/inheritance/entities/Feature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package example.jpa.inheritance.entities; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.DiscriminatorValue; | ||
import jakarta.persistence.Entity; | ||
|
||
@Entity | ||
@DiscriminatorValue("F") | ||
public class Feature extends Issue { | ||
@Column(name = "target_version") | ||
private String targetVersion; | ||
|
||
public String getTargetVersion() { | ||
return targetVersion; | ||
} | ||
|
||
public void setTargetVersion(String targetVersion) { | ||
this.targetVersion = targetVersion; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
jpa10/src/main/java/example/jpa/inheritance/entities/Issue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package example.jpa.inheritance.entities; | ||
|
||
import jakarta.persistence.DiscriminatorColumn; | ||
import jakarta.persistence.DiscriminatorValue; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Inheritance; | ||
import jakarta.persistence.InheritanceType; | ||
import jakarta.persistence.Table; | ||
|
||
/** | ||
* Issue entity. | ||
* | ||
* This entity demonstrates the single table inheritance strategy. | ||
* The discriminator column is defined via {@code @DiscriminatorColumn}, | ||
* its value is defined via {@code @DiscriminatorValue}. | ||
*/ | ||
@Entity | ||
@Table(name = "issues") | ||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) | ||
@DiscriminatorColumn(name = "type") | ||
@DiscriminatorValue("I") | ||
public class Issue { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private int id; | ||
|
||
private String title; | ||
|
||
private String description; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
jpa10/src/main/java/example/jpa/inheritance/entities/Manager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package example.jpa.inheritance.entities; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "managers") | ||
public class Manager extends Employee { | ||
private String responsibility; | ||
|
||
public String getResponsibility() { | ||
return responsibility; | ||
} | ||
|
||
public void setResponsibility(String responsibility) { | ||
this.responsibility = responsibility; | ||
} | ||
|
||
} |
Oops, something went wrong.