Skip to content

Commit

Permalink
single table inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
myluckagain committed Sep 8, 2020
1 parent 41f9765 commit 5664e82
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 0 deletions.
61 changes: 61 additions & 0 deletions hibernate-inheritance2/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.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.sysout</groupId>
<artifactId>inherit2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>inherit2</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>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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,12 @@
package ru.sysout;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

}
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> {

}
16 changes: 16 additions & 0 deletions hibernate-inheritance2/src/main/java/ru/sysout/model/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.sysout.model;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Data
@NoArgsConstructor
@Entity
@Inheritance
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String 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;

@Data
@Entity
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,6 @@
package ru.sysout.service;

public interface DiscountCalculator<T> {
double calculate(T customer);
Class<T> getClazz();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.sysout.service;

import org.springframework.stereotype.Service;
import ru.sysout.model.EmployeeCustomer;

@Service
public class EmployeeCustomerDiscountCalculator implements DiscountCalculator<EmployeeCustomer> {
@Override
public double calculate(EmployeeCustomer customer) {
if (customer.getMonthsInCompany() > 12)
return 0.1;
return 0.05;
}

public Class getClazz(){
return EmployeeCustomer.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.sysout.service;

import org.springframework.stereotype.Service;
import ru.sysout.model.ExternalCustomer;

@Service
public class ExternalCustomerDiscountCalculator implements DiscountCalculator<ExternalCustomer> {
@Override
public double calculate(ExternalCustomer customer) {
if (customer.getSum() > 100)
return 0.01;
return 0.05;
}
public Class getClazz(){
return ExternalCustomer.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.sysout.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.sysout.dao.CustomerRepository;
import ru.sysout.model.Customer;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MakeDiscountsService {
@Autowired
private CustomerRepository customerRepository;
@Autowired
private List<DiscountCalculator> calculators;

private Map<Class, DiscountCalculator> map = new HashMap<>();

@PostConstruct
private void init() {
for (DiscountCalculator discountCalculator : calculators) {
map.put(discountCalculator.getClazz(), discountCalculator);
}
}

public void makeDiscounts() {
List<Customer> customers = customerRepository.findAll();
for (Customer customer : customers) {
DiscountCalculator discountCalculator = map.get(customer.getClass());
double discount = discountCalculator.calculate(customer);
System.out.println(discount);
}
}
}

14 changes: 14 additions & 0 deletions hibernate-inheritance2/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
spring:
h2:
console:
enabled: true
path: /h2-console
datasource:
url: jdbc:h2:mem:testdb
username: sa
password:
initialization-mode: never
jpa:
show-sql: true
hibernate:
ddl-auto: create
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ru.sysout;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
import ru.sysout.dao.CustomerRepository;
import ru.sysout.model.EmployeeCustomer;
import ru.sysout.model.ExternalCustomer;
import ru.sysout.service.EmployeeCustomerDiscountCalculator;
import ru.sysout.service.ExternalCustomerDiscountCalculator;
import ru.sysout.service.MakeDiscountsService;

@DataJpaTest
@Import({MakeDiscountsService.class, EmployeeCustomerDiscountCalculator.class, ExternalCustomerDiscountCalculator.class})
public class CustomerCalculatorTest {
@Autowired
private CustomerRepository customerRepository;

@Autowired
private MakeDiscountsService makeDiscountsService;

@BeforeEach
private 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);
}

@Test
public void test() {
makeDiscountsService.makeDiscounts();
}
}

0 comments on commit 5664e82

Please sign in to comment.