如何检索子类实体

时间:2019-09-27 09:55:22

标签: java spring hibernate

我创建了以下实体结构:

@Data
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class SingleChange {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
}

@Data
@NoArgsConstructor
@Entity
public class SingleChangeByFoot extends SingleChange{

    Double lengthInMinutes;
}

@Data
@NoArgsConstructor
@Entity
public class SingleChangeByVehicle extends SingleChange{

    String lineName;
    String firstStop;
    String lastStop;
}

我还为每个实体创建了JPA存储库。

我想问一下当我只有使用JPA存储库的ID时如何检索子类。我尝试使用SingleChangeRepository然后使用getClass()方法或instanceof运算符来获取对象,但是它在Hibernate代理后面返回了类似SingleChange的信息,但没有说明此对象是SingleChangeByFoot还是SingleChangeByVehicle类。我如何知道应该使用SingleChangeByFoot存储库还是SingleChangeByVehicleRepository

0 个答案:

没有答案