Reputation: 5122
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
Upvotes: 0
Views: 918
Reputation: 27018
I believe this cannot be achieved with InheritanceType.TABLE_PER_CLASS
strategy.
Reason being, with TABLE_PER_CLASS, spring-data-jpa(Hibernate) will create two tables in Database. cars
and trucks
. And there will be no Vehicle
table.
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 tables cars
, trucks
, vehicle
)
or
You can change the type in Garage to some concrete type like Car or Truck(which doesn't make sense)
Upvotes: 0