SpringBoot JPA仓库 – 获取选定字段显示错误“列未找到”

huangapple 未分类评论44阅读模式
英文:

SpringBoot JPA Repository - get selected fields shows error "Column not found"

问题

###我想要获取选定的列,最好是动态的。###

目前,我正在指定静态列 - id、title、description。

Category.java

@Entity(name="Category")
@Table(name="categories")
public class Category {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long id;
   private String slug;
   private String title;
   private String description;
   private String preview_image;
   private int isFeatured;

   /*  Getters, setters, etc */
}

CategoryRepository.java

public interface CategoryRepository extends JpaRepository<Category, Long> {

    @Query(
        value = "SELECT id, title, description FROM categories", 
        nativeQuery = true
    )
    List<Category> findAll();
}

这会导致错误:

未找到列 'is_featured'。异常类:class
org.springframework.dao.InvalidDataAccessResourceUsageException
异常 [无法执行查询;SQL [SELECT id, title, description
FROM categories]; 嵌套异常是
org.hibernate.exception.SQLGrammarException: 无法执行查询]

有没有办法解决这个错误?我正在考虑使用另一个模型而不是Category,但稍后需要使字段动态化。如果我不知道要返回哪些字段,就很难创建模型类。

另外,有没有办法使这个 @Query 代码动态化,以便根据参数返回列?

感谢您的任何帮助。提前感谢! SpringBoot JPA仓库 – 获取选定字段显示错误“列未找到”

英文:

###I want to fetch selected columns, preferably dynamically.###

For now, I'm specifying static columns - id, title, description.

Category.java

@Entity(name=&quot;Category&quot;)
@Table(name=&quot;categories&quot;)
public class Category {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long id;
   private String slug;
   private String title;
   private String description;
   private String preview_image;
   private int isFeatured;

   /*  Getters, setters etc */
}

CategoryRepository.java

public interface CategoryRepository extends JpaRepository&lt;Category, Long&gt; {

    @Query(
	    value = &quot;SELECT id, title, description FROM categories&quot;, 
	    nativeQuery = true
    )
    List&lt;Category&gt; findAll();
}

This gives the error:

> Column 'is_featured' not found. Exception class: class
> org.springframework.dao.InvalidDataAccessResourceUsageException
> Exception [could not execute query; SQL [SELECT id, title, description
> FROM categories]; nested exception is
> org.hibernate.exception.SQLGrammarException: could not execute query]

Any idea how this error can be resolved? I was thinking of using another model instead of Category but I need to make the fields dynamic later on. It's hard to make model class if I'm unaware of which fields to return.

Also, is there a way I can make this @Query code dynamic such that it returns columns mentioned in parameter?

Any help would be appreciated. Thanks in advance! SpringBoot JPA仓库 – 获取选定字段显示错误“列未找到”

答案1

得分: 0

因为Spring和Hibernate的命名策略导致的。默认情况下,它会将camelCase转换为SNAKE_CASE。所以在你的情况下,isFeatured会变为is_featured

如果你不想改变命名策略,只需在你的属性上添加@Column("isFeatured")。这将覆盖该属性的默认行为。

你可以在这里找到更多信息:https://stackoverflow.com/q/25283198/3493036

关于动态查询,你应该在Spring-data-jpa的文档中查找有关功能,以及关于querydsl的内容。

英文:

Its because of the naming strategy of Spring and Hibernate. It will convert camelCase to SNAKE_CASE by default. So in your case its isFeatured -> is_featured.

If you dont want to change the naming strategy just add @Column(&quot;isFeatured&quot;) on your property. This will override the default behavior of this property.

Here you can find more https://stackoverflow.com/q/25283198/3493036

Regarding dynamic query. You should look up the features in the documentation of Spring-data-jpa and about querydsl

huangapple
  • 本文由 发表于 2020年4月8日 14:28:00
  • 转载请务必保留本文链接:https://java.coder-hub.com/61094533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定