Spring Data JPA 存储库方法无法识别带下划线的属性名称 [英] Spring Data JPA repository methods don't recognize property names with underscores

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

问题描述

我在实体属性名称中有下划线,当 Spring 尝试创建 JPA 存储库实现时,会导致尝试解析属性名称的异常.

I have underscores in the entity property names, and when Spring tries to create the JPA repository implementation, it results in an exception trying to resolve the name of the property.

实体:

@Entity
public class Student {
      @Id
      private String s_id;
      private String s_name;
      ...
}

存储库:

 @Repository
 @Transactional
 public interface StudentRepository extends CrudRepository<Student, String> {

       List<Student> findByS__name(String name);

}

异常:

org.springframework.data.mapping.PropertyReferenceException: 
No property s found for type Student

这里说的是http://docs.spring.io/spring-data/jpa/docs/current/reference/html/

如果您的属性名称包含下划线(例如 first_name),您可以使用第二个下划线对方法名称中的下划线进行转义.对于必须命名查询方法的 first_name 属性findByFirst__name(…).

If your property names contain underscores (e.g. first_name) you can escape the underscore in the method name with a second underscore. For a first_name property the query method would have to be named findByFirst__name(…).

我只是按照文档说的做了,但还是出现了异常.我不想自己写@Query,我的属性名称需要下划线,如何解决这个问题?

I just did as document said, but I still got the exception. I dont want write @Query by myself, and I need underscore in my property name, how to fix this problem?

我使用 Spring 数据 jpa 1.8.0.RELEASE + hibernate 4.3.9.Final

I use Spring data jpa 1.8.0.RELEASE + hibernate 4.3.9.Final

推荐答案

如果您可以控制属性命名,请避免在实体属性名称中使用下划线.这将解决您的存储库问题,并使代码库更清晰.开发人员处理代码后会感谢你.

Avoid using underscores in the entity property names if you have control over the property naming. This will resolve your repository woes, and will result in a cleaner code-base. Developers dealing with the code after you will thank you.

注意,这不仅仅是我的意见:Spring 特别不鼓励使用下划线.

Note, it's not just my opinion: Spring specifically discourages using underscores.

由于我们将下划线视为保留字符,因此我们强烈建议遵循标准的 Java 命名约定(即不使用下划线属性名称,但不是驼峰式大小写).

As we treat underscore as a reserved character we strongly advise to follow standard Java naming conventions (i.e. not using underscores in property names but camel case instead).

这个 JIRA 问题 显示了为什么用这个推荐更新文档,以及部分描述双下划线选项被删除.

this JIRA issue shows why the documentation was updated with this reccomendation, and the part describing the double underscore option were removed.

我怀疑您的根本问题是 Spring/Hibernate 没有将驼峰式大小写属性名称映射到数据库中列的蛇式大小写名称.您真正需要的是在 hiberate 生成为 S_NAME 的 SQL 中解释您的属性名称.

I suspect your root problem is that Spring/Hibernate is not mapping camel case property names to the snake case names you have for your columns in the database. What you really need is for your property name to be interpreted in the SQL that hiberate generates as S_NAME.

这就是为什么您的属性名称中的下划线是必需的"?如果是这样,有几个解决方案:

Is that why underscores in your property name are "required"? If so, there are a few solutions:

选项 1:@Column 注释

要让 JPA/Hibernate 映射到正确的列名,您可以明确地告诉它名称.使用注释 @Column(name="...") 告诉它在 SQL 中使用哪些列名.那么字段名就不受列名的约束.

To get JPA/Hibernate to map to the correct column names you can tell it the names explicitly. Use the annotation @Column(name="...") to tell it what column names to use in SQL. Then the field names are not constrained by the column names.

@Entity
public class Student {
     @Id
     @Column(name="s_id")
     private String sId;
     @Column(name="s_name")
     private String sName;

     //...getters and setters...
}

选项 2:改进命名策略
或者,如果您的应用程序有大量实体,而不是将 @Column 添加到每个属性,而是将配置文件中的默认命名策略更改为 hibernate 改进命名策略.

<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>

这个命名策略会将驼峰命名为 SNAKE_CASE.那么你的类可以看起来像这样简单:

This naming strategy will convert camelCase to SNAKE_CASE. Then your class could look as simple as this:

@Entity
public class Student {
     @Id
     private String sId;
     private String sName;

     //...getters and setters...
}

使用这些选项中的任何一个,当它创建 SQL 时,它会将列名解析为:

Using either of those options, when it creates the SQL it will resolve the column names to:

 S_ID
 S_NAME

注意:如果你正在使用,或者可以使用 Spring Boot,自动配置默认将使用 SpringNamingStrategy,它是hibernate改进策略的略微修改版本.您无需执行任何操作即可获得这种改进的命名策略.

Note: If you are using, or can use Spring Boot, the auto-configuration default will use SpringNamingStrategy, which is a slightly modified version of the hibernate improved strategy. You won't have to do anything to get this improved naming strategy.

终点线:

在属性名称中使用驼峰命名法,您可以使用驼峰命名法编写存储库方法名称,并且您可以停止尝试纠缠双下划线:

Using camel case in your property names you can write your repository method name using camel case, and you can stop trying to wrangle the double underscore:

@Repository
@Transactional
public interface StudentRepository extends CrudRepository<Student, String> {  
       List<Student> findBySName(String name);   
}

这篇关于Spring Data JPA 存储库方法无法识别带下划线的属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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