【发布时间】:2018-05-25 08:49:19
【问题描述】:
我正在寻找一种从实现 Spring Data JPA Repository 接口的实例中获取实体类型或类名的方法。
我有许多接口扩展了一个基本接口,这些接口扩展了Repository 接口并定义了一些基本查询。
@NoRepositoryBean
public interface EnumerationRepository<T extends IDatabaseEnumeration> extends Repository<T, String> {
// ...
}
public interface SampleEnumerationRepository extends EnumerationRepository<SampleEnumeration> {
}
Spring 允许我将所有这些接口的实现作为一个集合注入到一个 bean 中
@Autowired
private Collection<EnumerationRepository<? extends IDatabaseEnumeration>> repositories;
我想将所有这些实现放入Map 中,以便在泛型方法中轻松访问。我想使用实体类型或名称作为键,但我不知道从哪里得到它。有没有办法获得这些属性之一?最好是类类型。
我能够通过这样的查询实现所需的行为。但如果可能,我想避免这种解决方案,因为它会创建对底层数据库的依赖。
@Query(value = "select '#{#entityName}' from dual", nativeQuery = true)
public String getEntityName();
【问题讨论】:
-
尝试删除“from dual”并删除 nativeQuery 部分。它应该像普通 JPA 查询一样工作。
-
不幸的是,这并没有奏效。现在我收到此异常,因为查询不完整。
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree [select 'SampleEnumeration'] -
我会 100% 发誓我在个人项目中做过同样的事情,但我现在找不到。今晚晚些时候我会搜索更多。与此同时,我暂时将“from dual”和 native 保留。
标签: java spring spring-data