【发布时间】:2018-06-25 08:14:10
【问题描述】:
我有以下映射:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Vehicle {
@Id
@GeneratedValue
Long id;
}
@Entity
@Table(name = "car")
@AttributeOverride(name = "id", column = @Column(name = "car_id"))
public class Car extends Vehicle {
}
@Entity
@Table(name = "bus")
@AttributeOverride(name = "id", column = @Column(name = "bus_id"))
public class Bus extends Vehicle {
}
我想要实现的是查询不同的表以检索Car 和Bus 实体。为此,我创建了以下 Spring Data 存储库
public interface VehicleRepository extends CrudRepository<Vehicle, Long> {
}
并尝试像这样使用它:vehicleRepository.findAll();
但是,在这种情况下,我得到了java.sql.SQLSyntaxErrorException: ORA-00904: "KEY": invalid identifier。似乎将@Inheritance 与@AttributeOverride 一起用于@Id 字段不起作用。
我想指出的是,如果Car 和Bus 实体对@Id 具有相同的映射,它将完美地工作(但情况并非如此:“car_id”和“bus_id”)
另外,我尝试将@Id 字段从Vehicle 类移动到子类,但结果发现每个@Entity 都应该包含一个@Id。
我还想提一提的是,我尝试使用 @MappedSuperclass 而不是 @Inheritance,但在这种情况下,我无法使用 abstact Vehicle 类型进行查询。
谁能帮我解决这个问题?
谢谢
【问题讨论】:
-
由于您已接受以下答案,能否请您在此处更新您的工作解决方案。我遇到了与您提到的完全相同的问题,但是通过阅读下面接受的答案,我无法前进。我一定错过了一些东西,所以请帮助我。谢谢。
标签: java hibernate jpa spring-data-jpa table-per-class