属性名称包含特殊单词的 Spring Data JPA 存储库方法 [英] Spring Data JPA repository method with property name containing a special word

查看:29
本文介绍了属性名称包含特殊单词的 Spring Data JPA 存储库方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个具有以下属性的实体:

@Entity公共类 MyObj {公共字符串名称OrId;}

然后我继续创建存储库

public interface MyObjRepository extends JpaRepository{MyObj findByNameOrId(String nameOrId);}

这失败了,因为似乎 Spring 将方法名称标记为 findBy Name Or Id,而不是findBy nameOrId.

由此产生的错误是name"属性未找到(或类似).

有没有办法转义方法语法中的特殊词(Or"、And"等)?重命名列以不包含这些词不是一种选择.

解决方案

一种解决方法是重命名 Java 字段以避免使用特殊词,同时使用 @Column 注释保留列名.

>

@Column(name = "nameOrId")公共字符串名称Id;

相应地重命名 JPA 方法.

MyObj findByNameId(String nameOrId);

For example, I have an entity with the following attribute:

@Entity
public class MyObj {
    public String nameOrId;
}

I then proceed to create the repository

public interface MyObjRepository extends JpaRepository<MyObj, String> {
    MyObj findByNameOrId(String nameOrId);
}

This fails, because it seems that Spring tokenizes the method name as findBy Name Or Id, instead of findBy nameOrId.

The resulting error is "name" property not found (or similar).

Is there a way to escape the special words ("Or", "And", etc) in the method syntax? Renaming the column to not include those words is not an option.

解决方案

A workaround is to rename the Java field to avoid special words, while preserving the column name using the @Column annotation.

@Column(name = "nameOrId")
public String nameId;

The JPA method is renamed accordingly.

MyObj findByNameId(String nameOrId);

这篇关于属性名称包含特殊单词的 Spring Data JPA 存储库方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆