我们拥有实体类的层次结构,其中所有实体类都继承自BaseEntity @MappedSuperclass。有没有办法拥有一个spring-data" base repository"允许您查询任何BaseEntity而无需知道它的类型并使用特定的存储库?
@MappedSuperclass
public class BaseEntity {
@Id
private Long id;
private String owner;
// setter/getter etc...
}
@Entity
public class FooEntity extends BaseEntity {
private String foo;
}
@Entity
public class BarEntity extends BaseEntity {
private String bar;
}
/**
* How to declare this base repository....
*/
public interface BaseRepository extends JpaRepository<BaseEntity, ActionPK> {
List<BaseEntity> findByOwner(String owner);
}
@Component
public class MyService {
@Autowired
private BaseRepository baseRepository;
public void doSomething() {
// ... so that is can be used like this:
List<BaseEntity> entities = baseRepository.findByOwner("john");
doSomethingWith(entities);
}
}
答案 0 :(得分:1)
我认为实现这一目标的唯一方法是创建一个Service类,它将编排对具有owner属性的Repository类的所有调用。该服务调用将返回您的BaseEntity
对象列表。