有没有办法在基础代理接口上声明@Query,以便您不必在所有存储库中声明它?该查询在" FROM"中具有不同的实体名称。查询的一部分。
@MappedSuperclass
public abstract class BaseAction {
@Id
Long id;
...
}
@Entity
@Table(name="AKTION_EMAIL")
public class EmailAction extends BaseAction {
private String email;
}
public interface ActionRepository<T extends BaseAction> extends JpaRepository<T, ActionPK> {
@Query("SELECT max(seqNumber) + 1 FROM ????????????? e WHERE e.order = ?1 AND e.action = ?2")
Long findNextSeqNumberByOrderAndAction(Order order, ActionConfiguration action);
}
public interface EmailActionRepository extends ActionRepository<EmailAction> {
// This works, but I don't want to repeat that in all entity repositories...
@Query("SELECT max(seqNumber) + 1 FROM EmailAction e WHERE e.order = ?1 AND e.action = ?2")
Long findNextSeqNumberByOrderAndAction(Order order, ActionConfiguration action);
}
答案 0 :(得分:2)
您可以在通用查询定义中使用SpEL表达式来引用未知实体类型:
interface ActionRepository<T extends BaseAction> extends JpaRepository<T, ActionPK> {
@Query("SELECT max(seqNumber) + 1 FROM #{#entityName} e WHERE …")
Long findNextSeqNumberByOrderAndAction(Order ActionConfiguration action);
}
请注意我们如何使用#{#entityName}
动态插入将为其创建存储库的实体的名称。