mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-16 06:58:54 +00:00
* implemented single table inheritance * added the Java documentation for the pattern * updated code files as per the standards * added the puml and png diagram for the file * added README.md * #1316 Single Table Inheritance pattern implemented * resolved the Checkstyle violations * removed the tests due to build failure * updated the code as per review * resolved Checkstyle violations
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import com.iluwatar.entity.Car;
|
||||
import com.iluwatar.entity.Truck;
|
||||
import com.iluwatar.entity.Vehicle;
|
||||
import com.iluwatar.service.VehicleService;
|
||||
import java.util.List;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Single Table Inheritance pattern :
|
||||
* <br>
|
||||
* It maps each instance of class in an inheritance tree into a single table.
|
||||
* <br>
|
||||
* <p>
|
||||
* In case of current project, in order to specify the Single Table Inheritance to Hibernate
|
||||
* we annotate the main Vehicle root class with @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
* due to which a single root <b>Vehicle</b> class table will be created
|
||||
* in the database and it will have columns for all the fields of
|
||||
* it's subclasses(Car, Freighter, Train, Truck). <br>
|
||||
* Additional to that, a new separate <b>"vehicle_id"</b> column would be added
|
||||
* to the Vehicle table to save the type of the subclass object that
|
||||
* is being stored in the database. This value is specified by the @DiscriminatorValue annotation
|
||||
* value for each subclass in case of Hibernate. <br>
|
||||
* </p><br>
|
||||
* Below is the main Spring Boot Application class from where the Program Runs.
|
||||
* <p>
|
||||
* It implements the CommandLineRunner to run the statements at the
|
||||
* start of the application program.
|
||||
* </p>
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@AllArgsConstructor
|
||||
public class SingleTableInheritance implements CommandLineRunner {
|
||||
|
||||
//Autowiring the VehicleService class to execute the business logic methods
|
||||
private final VehicleService vehicleService;
|
||||
|
||||
/**
|
||||
* The entry point of the Spring Boot Application.
|
||||
*
|
||||
* @param args program runtime arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SingleTableInheritance.class, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* The starting point of the CommandLineRunner
|
||||
* where the main program is run.
|
||||
*
|
||||
* @param args program runtime arguments
|
||||
*/
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
|
||||
Logger log = LoggerFactory.getLogger(SingleTableInheritance.class);
|
||||
|
||||
log.info("Saving Vehicles :- ");
|
||||
|
||||
// Saving Car to DB as a Vehicle
|
||||
Vehicle vehicle1 = new Car("Tesla", "Model S", 4, 825);
|
||||
Vehicle car1 = vehicleService.saveVehicle(vehicle1);
|
||||
log.info("Vehicle 1 saved : {}", car1);
|
||||
|
||||
// Saving Truck to DB as a Vehicle
|
||||
Vehicle vehicle2 = new Truck("Ford", "F-150", 3325, 14000);
|
||||
Vehicle truck1 = vehicleService.saveVehicle(vehicle2);
|
||||
log.info("Vehicle 2 saved : {}\n", truck1);
|
||||
|
||||
|
||||
log.info("Fetching Vehicles :- ");
|
||||
|
||||
// Fetching the Car from DB
|
||||
Car savedCar1 = (Car) vehicleService.getVehicle(vehicle1.getVehicleId());
|
||||
log.info("Fetching Car1 from DB : {}", savedCar1);
|
||||
|
||||
// Fetching the Truck from DB
|
||||
Truck savedTruck1 = (Truck) vehicleService.getVehicle(vehicle2.getVehicleId());
|
||||
log.info("Fetching Truck1 from DB : {}\n", savedTruck1);
|
||||
|
||||
log.info("Fetching All Vehicles :- ");
|
||||
|
||||
// Fetching the Vehicles present in the DB
|
||||
List<Vehicle> allVehiclesFromDb = vehicleService.getAllVehicles();
|
||||
allVehiclesFromDb.forEach(s -> log.info(s.toString()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.iluwatar.entity;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* A class that extends the PassengerVehicle class
|
||||
* and provides the concrete inheritance implementation of the Car.
|
||||
*
|
||||
* @see PassengerVehicle PassengerVehicle
|
||||
* @see Vehicle Vehicle
|
||||
*/
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Entity
|
||||
@DiscriminatorValue(value = "CAR")
|
||||
public class Car extends PassengerVehicle {
|
||||
|
||||
private int engineCapacity;
|
||||
|
||||
public Car(String manufacturer, String model, int noOfPassengers, int engineCapacity) {
|
||||
super(manufacturer, model, noOfPassengers);
|
||||
this.engineCapacity = engineCapacity;
|
||||
}
|
||||
|
||||
// Overridden the toString method to specify the Vehicle object
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Car{"
|
||||
+ super.toString()
|
||||
+ '}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.iluwatar.entity;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* A class that extends the TransportVehicle class
|
||||
* and provides the concrete inheritance implementation of the Car.
|
||||
*
|
||||
* @see TransportVehicle TransportVehicle
|
||||
* @see Vehicle Vehicle
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Entity
|
||||
@DiscriminatorValue(value = "FREIGHTER")
|
||||
public class Freighter extends TransportVehicle {
|
||||
|
||||
private double flightLength;
|
||||
|
||||
public Freighter(String manufacturer, String model, int loadCapacity, double flightLength) {
|
||||
super(manufacturer, model, loadCapacity);
|
||||
this.flightLength = flightLength;
|
||||
}
|
||||
|
||||
// Overridden the toString method to specify the Vehicle object
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Freighter{ "
|
||||
+ super.toString()
|
||||
+ " ,"
|
||||
+ "flightLength="
|
||||
+ flightLength
|
||||
+ '}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.iluwatar.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* An abstract class that extends the Vehicle class
|
||||
* and provides properties for the Passenger type of Vehicles.
|
||||
*
|
||||
* @see Vehicle
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public abstract class PassengerVehicle extends Vehicle {
|
||||
|
||||
private int noOfPassengers;
|
||||
|
||||
protected PassengerVehicle(String manufacturer, String model, int noOfPassengers) {
|
||||
super(manufacturer, model);
|
||||
this.noOfPassengers = noOfPassengers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.iluwatar.entity;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* A class that extends the PassengerVehicle class
|
||||
* and provides the concrete inheritance implementation of the Car.
|
||||
*
|
||||
* @see PassengerVehicle PassengerVehicle
|
||||
* @see Vehicle Vehicle
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Entity
|
||||
@DiscriminatorValue(value = "TRAIN")
|
||||
public class Train extends PassengerVehicle {
|
||||
|
||||
private int noOfCarriages;
|
||||
|
||||
public Train(String manufacturer, String model, int noOfPassengers, int noOfCarriages) {
|
||||
super(manufacturer, model, noOfPassengers);
|
||||
this.noOfCarriages = noOfCarriages;
|
||||
}
|
||||
|
||||
// Overridden the toString method to specify the Vehicle object
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Train{"
|
||||
+ super.toString()
|
||||
+ '}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.iluwatar.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* An abstract class that extends the Vehicle class
|
||||
* and provides properties for the Transport type of Vehicles.
|
||||
*
|
||||
* @see Vehicle
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public abstract class TransportVehicle extends Vehicle {
|
||||
|
||||
private int loadCapacity;
|
||||
|
||||
protected TransportVehicle(String manufacturer, String model, int loadCapacity) {
|
||||
super(manufacturer, model);
|
||||
this.loadCapacity = loadCapacity;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.iluwatar.entity;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* A class that extends the PassengerVehicle class
|
||||
* and provides the concrete inheritance implementation of the Car.
|
||||
*
|
||||
* @see TransportVehicle TransportVehicle
|
||||
* @see Vehicle Vehicle
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Entity
|
||||
@DiscriminatorValue(value = "TRUCK")
|
||||
public class Truck extends TransportVehicle {
|
||||
|
||||
private int towingCapacity;
|
||||
|
||||
public Truck(String manufacturer, String model, int loadCapacity, int towingCapacity) {
|
||||
super(manufacturer, model, loadCapacity);
|
||||
this.towingCapacity = towingCapacity;
|
||||
}
|
||||
|
||||
// Overridden the toString method to specify the Vehicle object
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Truck{ "
|
||||
+ super.toString()
|
||||
+ ", "
|
||||
+ "towingCapacity="
|
||||
+ towingCapacity
|
||||
+ '}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.iluwatar.entity;
|
||||
|
||||
import javax.persistence.DiscriminatorColumn;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* An abstract class that is the root of the Vehicle Inheritance hierarchy
|
||||
* and basic provides properties for all the vehicles.
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(name = "VEHICLE_TYPE")
|
||||
public abstract class Vehicle {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int vehicleId;
|
||||
|
||||
private String manufacturer;
|
||||
|
||||
private String model;
|
||||
|
||||
protected Vehicle(String manufacturer, String model) {
|
||||
this.manufacturer = manufacturer;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Vehicle{"
|
||||
+ "vehicleId="
|
||||
+ vehicleId
|
||||
+ ", manufacturer='"
|
||||
+ manufacturer
|
||||
+ '\''
|
||||
+ ", model='"
|
||||
+ model
|
||||
+ '}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.iluwatar.repository;
|
||||
|
||||
import com.iluwatar.entity.Vehicle;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* A repository that is extending the JPA Repository
|
||||
* to provide the default Spring DATA JPA methods for the Vehicle class.
|
||||
*/
|
||||
@Repository
|
||||
public interface VehicleRepository extends JpaRepository<Vehicle, Integer> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.iluwatar.service;
|
||||
|
||||
import com.iluwatar.entity.Vehicle;
|
||||
import com.iluwatar.repository.VehicleRepository;
|
||||
import java.util.List;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* A service class that is used to provide the business logic
|
||||
* for the Vehicle class and connect to the database to
|
||||
* perform the CRUD operations on the root Vehicle class.
|
||||
*
|
||||
* @see Vehicle
|
||||
*/
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class VehicleService {
|
||||
|
||||
private final VehicleRepository vehicleRepository;
|
||||
|
||||
/**
|
||||
* A method to save all the vehicles to the database.
|
||||
*
|
||||
* @param vehicle Vehicle bbject
|
||||
* @see Vehicle
|
||||
*/
|
||||
public Vehicle saveVehicle(Vehicle vehicle) {
|
||||
return vehicleRepository.save(vehicle);
|
||||
}
|
||||
|
||||
/**
|
||||
* A method to get a specific vehicle from vehicle id.
|
||||
*
|
||||
* @param vehicleId Vehicle Id
|
||||
* @see Vehicle
|
||||
*/
|
||||
public Vehicle getVehicle(int vehicleId) {
|
||||
return vehicleRepository.findById(vehicleId).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* A method to get all the vehicles saved in the database.
|
||||
*
|
||||
* @see Vehicle
|
||||
*/
|
||||
public List<Vehicle> getAllVehicles() {
|
||||
return vehicleRepository.findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* A method to update a vehicle in the database.
|
||||
*
|
||||
* @param vehicle Vehicle object
|
||||
* @see Vehicle
|
||||
*/
|
||||
public Vehicle updateVehicle(Vehicle vehicle) {
|
||||
return vehicleRepository.save(vehicle);
|
||||
}
|
||||
|
||||
/**
|
||||
* A method to save all the vehicles to the database.
|
||||
*
|
||||
* @param vehicle Vehicle object
|
||||
* @see Vehicle
|
||||
*/
|
||||
public void deleteVehicle(Vehicle vehicle) {
|
||||
vehicleRepository.delete(vehicle);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user