Мне удалось найти решение, и проблема заключалась в том, что я не мог разобраться с ссылками на классы Car
и Truck
из Engine
, когда в базе данных модель отличалась.
Автомобиль.java
package com.backend.dao.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import java.util.Objects;
import java.util.UUID;
public class Car implements Persistable<UUID> {
@Id
private UUID id;
private String brand;
private String model;
public void setId(UUID id) {
this.id = id;
}
@Override
public UUID getId() {
return id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
@Override
public boolean isNew() {
return id == null;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Car)) {
return false;
}
Car car = (Car) o;
return Objects.equals(id, car.id) &&
Objects.equals(brand, car.brand) &&
Objects.equals(model, car.model);
}
@Override
public int hashCode() {
return Objects.hash(id, brand, model);
}
}
Truck.java
package com.backend.dao.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Table;
import java.util.Objects;
import java.util.UUID;
@Table("truck")
public class Truck implements Persistable<UUID> {
@Id
private UUID id;
private String brand;
private String model;
private Integer cargoMaxWeight;
private String truckType;
public void setId(UUID id) {
this.id = id;
}
@Override
public UUID getId() {
return id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public Integer getCargoMaxWeight() {
return cargoMaxWeight;
}
public void setCargoMaxWeight(Integer cargoMaxWeight) {
this.cargoMaxWeight = cargoMaxWeight;
}
public String getTruckType() {
return truckType;
}
public void setTruckType(String truckType) {
this.truckType = truckType;
}
@Override
public boolean isNew() {
return id == null;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Truck)) {
return false;
}
Truck truck = (Truck) o;
return Objects.equals(id, truck.id) &&
Objects.equals(brand, truck.brand) &&
Objects.equals(model, truck.model) &&
Objects.equals(cargoMaxWeight, truck.cargoMaxWeight) &&
Objects.equals(truckType, truck.truckType);
}
@Override
public int hashCode() {
return Objects.hash(id, brand, model, cargoMaxWeight, truckType);
}
}
Engine.java
package com.backend.dao.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.UUID;
@Table("engine")
public class Engine implements Persistable<UUID> {
@Id
private UUID id;
private String name;
private LocalDateTime dateCreated;
private String type;
@Column("engine_id")
private Car car;
@Column("engine_id")
private Truck truck;
public void setId(UUID id) {
this.id = id;
}
@Override
public UUID getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDateTime getDateCreated() {
return dateCreated;
}
public void setDateCreated(LocalDateTime dateCreated) {
this.dateCreated = dateCreated;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public boolean isNew() {
return id == null;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Engine)) {
return false;
}
Engine engine = (Engine) o;
return Objects.equals(id, engine.id) &&
Objects.equals(name, engine.name) &&
Objects.equals(dateCreated, engine.dateCreated) &&
Objects.equals(type, engine.type);
}
@Override
public int hashCode() {
return Objects.hash(id, name, dateCreated, type);
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public Truck getTruck() {
return truck;
}
public void setTruck(Truck truck) {
this.truck = truck;
}
}
Однако это решение не соответствует требованию - операции CRUD выполняются на Engine.java
отражены Car.java
и Truck.java
.И я бы хотел, чтобы операции CRUD, выполняемые на Car.java
и Truck.java
, отражались на Engine.java
.
Если у кого-то есть лучшее решение, напишите, пожалуйста.