I have these classes:
@Entity
@Table(name = "garage")
class Garage {
@Id
private String id;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "garage_id", referencedColumnName = "id")
private List<Vehicle> vehiclesList;
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
abstract class Vehicle {
}
@Entity
@Table(name = "cars")
class Car extends Vehicle {
}
@Entity
@Table(name = "trucks")
class Truck extends Vehicle {
}
I am using cascade all.
When I am trying to persist them. I get: SqlError, No table 'vehicle'
I want to clarify that I am always persisting a garage with one type of vehicles
So when I hit garageRepository.save(garage) I expect the child table (either car or truck) to be populated. and the garage_id will be the new garage.
How can this be done?
Regards, Ido
I believe this cannot be achieved with
InheritanceType.TABLE_PER_CLASSstrategy.Reason being, with TABLE_PER_CLASS, spring-data-jpa(Hibernate) will create two tables in Database.
carsandtrucks. And there will be noVehicletable.But since you have
private List<Vehicle> vehiclesList;in your Garage class, spring-data searches for Vehicle table and hence you see this error.Couple of suggestions.
This can be achieved by.
1.
InheritanceType.SINGLE_TABLE(There will be only one table.Vehicle) 2.InheritanceType.JOINED(There will be three tablescars,trucks,vehicle)or
You can change the type in Garage to some concrete type like Car or Truck(which doesn't make sense)