使用@query的Spring规范

如何解决使用@query的Spring规范

我有一个名为“ content”的表,该表具有一个名为“ created_at”的字段。 我正在尝试在此表中使用分页和规范。

规格完美运行,但我对分页有问题。如果我使用从存储库继承的方法搜索可分页页面,则无法识别带下划线的字段,并尝试将其拆分。给出此错误:

"No property created found for type Content!"

如果我在存储库中创建了可分页的方法,但规范却没有。 这是我的资料库:

@Repository
public interface ContentRepository extends JpaRepository<Content,String>,JpaSpecificationExecutor<Content> {

   @Query(value = "SELECT * FROM content",nativeQuery = true)
   public Page<Content> findAll(Specification<Content> specification,Pageable pageable);    

}

我该怎么办?

内容分类:

 @Entity
 @Table(name = "content")
 @Setter
 @Getter
 public class Content {

  @Id
  @Column(name = "id")
  private String id;

  @Column
  private String name;

  @Column
  private String description;

  @Column(columnDefinition = "TEXT")
  private String content;

  @Column(columnDefinition = "TEXT")
  private String reference;

  @ManyToOne
  @JoinColumn(nullable = false)
  private User author;

  @ManyToOne
  @JoinColumn(nullable = true)
  private Agenda agenda;

  @ManyToOne
  @JoinColumn(nullable = false)
  private ContentType contenttype;

  @Column(columnDefinition = "boolean default true")
  private boolean enabled;

  @Column(columnDefinition = "boolean default false")
  private boolean approved;

  @Column
  private Date sent_at;
  @Column
  private Date created_at;
  @Column
  private Date updated_at;
  @Column
  private Date deleted_at;

}

解决方法

如果您可以控制属性命名,请避免在实体属性名称中使用下划线。这将解决您的存储库问题,并导致更简洁的代码库。在您处理完代码后,开发人员将感激您。

请注意,不仅仅是我的意见:Spring specifically discourages using underscores

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

this JIRA issue显示了为何使用此建议更新文档,并删除了描述双下划线选项的部分。

我怀疑您的根本问题是Spring / Hibernate并未将骆驼案例属性名称映射到数据库中列所具有的蛇案例名称。您真正需要的是在冬眠生成的SQL中将属性名解释为created_at。

这就是为什么您的财产名称中的下划线是“必需” 的原因吗?如果是这样,有几种解决方法:

选项1:@Column注释

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

@Entity
@Table(name = "content")
@Setter
@Getter
public class Content {
     @Id
     @Column(name="created_at")
     private String createdAt;
}

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

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

此命名策略会将camelCase转换为SNAKE_CASE。然后您的班级可能看起来像这样简单:

@Entity
public class Content{
     @Id
     private String createdAt;

}

使用这些选项之一,在创建SQL时会将列名解析为:

 created_at

注意:如果您正在使用或可以使用Spring Boot,则自动配置默认设置将使用SpringNamingStrategy,它是休眠改进策略的略微修改版本。您无需采取任何措施即可获得改进的命名策略。

终点线:

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

@Repository
@Transactional
public interface ContentRepository extends CrudRepository<Content,String> {  
      @Query(value = "SELECT * FROM content",nativeQuery = true)
       List<Student> findAll(Specification<Content> specification,Pageable pageable);   
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-