简体   繁体   English

我可以在 JpaRepository nativeQuery 中使用枚举参数吗?

[英]Can I use enum parameter into JpaRepository nativeQuery?

Entity looks like this:实体看起来像这样:

@Getter
@Setter
@Entity
public class Application {
@Id
private Long id;
@Enumerated(EnumType.STRING)
private ApplicationStatus status;
}

Code works this way:代码是这样工作的:

public interface ApplicationRepository extends JpaRepository<Application, Long> {
@Query("SELECT app FROM #{#entityName} AS app WHERE app.status LIKE :status")
List<Application> find(@Param("status") ApplicationStatus status);

But the same snippet with nativeQuery - doesn't:但是与 nativeQuery 相同的片段 - 没有:

@Query(value = "SELECT app.* FROM application AS app WHERE app.status LIKE :status", nativeQuery = true)
List<Application> findNative(@Param("status") ApplicationStatus status);
}

And I don`t have any exception, just empty list.而且我没有任何例外,只是空列表。

How can I fix this?我该如何解决这个问题? Is it possible to use enum with nativeQuery ?是否可以将enumnativeQuery一起使用?

PS I can pass String into method instead of ApplicationStatus but maybe there are another option? PS 我可以将String传递给方法而不是ApplicationStatus但也许还有另一种选择?

Following similar question with similar requirement and one of the answers pointing to Spring Expression Language (SpEL) you could use:遵循具有类似要求的类似问题以及指向Spring Expression Language (SpEL)的答案之一,您可以使用:

public interface ApplicationRepository extends JpaRepository<Application, Long> {
    @Query(nativeQuery = true, value = "SELECT app FROM #{#entityName} AS app WHERE app.status=:#{#status.name()}")
    List<Application> find(@Param("status") ApplicationStatus status);
}

Above important part is app.status=:#{#status.name()}上面的重要部分是app.status=:#{#status.name()}

To extend @Aivaras answer: If you want to use list of statuses, SpEL ecpression is slightly different - you need to do projection:扩展@Aivaras 答案:如果您想使用状态列表,SpEL ecpression 略有不同 - 您需要进行投影:

public interface ApplicationRepository extends JpaRepository<Application, Long> {
    @Query(nativeQuery = true, value = "SELECT app FROM #{#entityName} AS app WHERE app.status in :#{#statuses.![name()]}")
    List<Application> find(@Param("statuses") List<ApplicationStatus> statuses);
}

Note the change of expression to请注意将表达式更改为

#{#statuses.![name()]}

您可以在传递参数之前转换为字符串。

how about this?这个怎么样?

public interface ApplicationRepository extends JpaRepository<Application, Long> {
List<Application> findByStatus(ApplicationStatus status);

After a few days with this error, I was able to find the solution.在出现此错误几天后,我找到了解决方案。

I did a lot of research and tested in many ways receiving as a parameter @Param("environment") environment: Environment :我做了很多研究,并以多种方式进行了测试,作为参数接收@Param("environment") environment: Environment :

 :#{#environment.TESTING}
 :#{#environment}
 :#{#environment.name()}
 CAST(:#{#environment.name()} as environment)

Solution in Kotlin Kotlin 中的解决方案

The key is in the query.关键在于查询。 You must to transform the value of the parameter to a String using .name() (or receive a String as a parameter) and cast that value of type String to the specific Enum that is needed .您必须使用.name()将参数的值转换为字符串(或接收字符串作为参数)并将该字符串类型的值转换为所需的特定 Enum Because passing an object of type Enum directly in the query does not work.因为直接在查询中传递 Enum 类型的对象不起作用。

Assuming the Enum in your database is defined as environment .假设您的数据库中的 Enum 被定义为environment

@Query(
        value = "SELECT some_routine(CAST(:#{#environmentNamedParam.name()} as environment))",
        nativeQuery = true
)
fun yourFunction(
        @Param("environmentNamedParam") environmentParam: Environment
) : Boolean

Differentiate between:区分:

  • environmentNamedParam环境命名参数
  • environmentParam环境参数
  • environment环境

#spring #jpa #postgresql #kotlin #spring #jpa #postgresql #kotlin

I solved this issue using #{#paramName?.name()}我使用#{#paramName?.name()}解决了这个问题

public interface ItemRepository extends JpaRepository<Item, Long> {
    @Query(value = "select * from items where type = :#{#type?.name()}", nativeQuery = true)
    List<Item> findByType(@Param("type") EnumType type);
}
 
public enum EnumType { NORMAL, LARGE };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM