JpaRepository 中的动态实体名称?

Dynamic entity name in JpaRepository?

我正在使用 spring JpaRepository,并希望使用 el 表达式提供具有通用派生 SQL 查询的通用接口,如下所示:

public interface BaseRepo <B> extends CrudRepository<B, Long> {
    @Query("SELECT b FROM #{#entityName} b)
    List<B> findAllB();
}

@Entity
class Booking {} //results in "booking"

这很好用!但是,如果所需的实体包含下划线怎么办?我如何定义 el 表达式如何翻译实体名称的策略?

@Entity
@Table(name = "booking_entity")
class BookingEntity {} //results in "bookingentity", missing underscore!

您可以使用注释 @Entity(name="booking_entity")