Skip to content

Commit

Permalink
InheritanceType.TABLE_PER_CLASS
Browse files Browse the repository at this point in the history
  • Loading branch information
myluckagain committed Dec 16, 2021
1 parent ecb7a29 commit 71b78c3
Show file tree
Hide file tree
Showing 8 changed files with 1,537 additions and 0 deletions.
1,377 changes: 1,377 additions & 0 deletions hibernate-inheritance3-tableperclass/DEMO_INFORMATION_SCHEMA_CATALOGS.sql

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions hibernate-inheritance3-tableperclass/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.sysout</groupId>
<artifactId>inherit1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>inherit1</name>

<properties>
<java.version>11</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.sysout;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import ru.sysout.dao.CustomerRepository;
import ru.sysout.model.Customer;
import ru.sysout.model.EmployeeCustomer;
import ru.sysout.model.ExternalCustomer;

import javax.annotation.PostConstruct;

@SpringBootApplication
public class SpringDataJpaApplication {
@Autowired
private CustomerRepository customerRepository;

public static void main(String[] args) {
SpringApplication.run(SpringDataJpaApplication.class, args);
}

@PostConstruct
void init() {
EmployeeCustomer employeeCustomer = new EmployeeCustomer();
employeeCustomer.setMonthsInCompany(10);
employeeCustomer.setName("Petr");
customerRepository.save(employeeCustomer);

ExternalCustomer externalCustomer = new ExternalCustomer();
externalCustomer.setSum(110);
externalCustomer.setName("Vasya");
customerRepository.save(externalCustomer);

Customer customer=new Customer();
customer.setName("ldld");
customerRepository.save(customer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.sysout.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import ru.sysout.model.Customer;

public interface CustomerRepository extends JpaRepository<Customer, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.sysout.model;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Data
@NoArgsConstructor
@Entity
@Inheritance(
strategy = InheritanceType.TABLE_PER_CLASS
)
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String name;

public Customer(String name){
this.name =name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.sysout.model;

import lombok.Data;

import javax.persistence.Entity;

@Entity
@Data
public class EmployeeCustomer extends Customer {
private int monthsInCompany;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.sysout.model;

import lombok.Data;

import javax.persistence.Entity;

@Data
@Entity
public class ExternalCustomer extends Customer {
private long sum;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
datasource:
url: jdbc:postgresql://localhost/inherit
username: postgres
password: qqq
jpa:
show-sql: true
hibernate:
ddl-auto: create

0 comments on commit 71b78c3

Please sign in to comment.