Spring JPA + CRUD

编程入门 行业动态 更新时间:2024-10-28 13:26:19
Spring JPA + CRUD - 自定义查询不允许_字符?(Spring JPA + CRUD - custom query doesn't allow _ characters?)

我在spring中创建自定义查询时遇到麻烦,因为我的Entity在其参数的名称中包含一个“_”字符:“game_date”。

我的表也有一个名为“game_date”的列。

我创建了以下方法:

List<Games> findByGame_dateAndOpponent(@Param("game_date") Date game_date, @Param("opponent") String opponent);

但是当我启动我的应用程序时,除了类型之外它崩溃了:“org.springframework.data.mapping.PropertyReferenceException:找不到类型游戏的属性gamedate!”。 在Entity和Query方法中将参数名称更改为“gameDate”之后,它停止了抱怨,并且实际上正在返回预期的条目。 但与此同时,它不会在搜索查询中返回“game_date”列中的值,这是Date类型的简单常规列。 我不知道这件事发生了什么。

我正在使用的是MySql。

这是代码本身:

实体:

import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity @Table(name = "games") public class Games { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id_game") private int id; @Column(name = "game_date", columnDefinition = "DATE") @Temporal(TemporalType.DATE) private Date gameDate; public Date getGame_date() { return gameDate; } public void setGame_date(Date _game_date) { this.gameDate = _game_date; } }

还有一个存储库:

import java.sql.Date; import java.util.List; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RepositoryRestResource; @RepositoryRestResource public interface GamesRepository extends CrudRepository< Games , Integer > { List< Games > findById( @Param( "id" ) int id ); List< Games > findAll( ); List<Games> findByGameDateAndOpponent(@Param("game_date") Date game_date, @Param("opponent") String opponent); }

I'm having troubles in creating a custom query within spring, because my Entity contains an "_" character in it's parameter's name: "game_date".

My table has a column named "game_date" as well.

I have created following method:

List<Games> findByGame_dateAndOpponent(@Param("game_date") Date game_date, @Param("opponent") String opponent);

but when I start my app, it's crashing with exception of kind: "org.springframework.data.mapping.PropertyReferenceException: No property gamedate found for type Games!". After changing a parameter name to the "gameDate" both in Entity and Query method, it stopped complaining, and is actually returning expected entries. But at the same time, it doesn't return values from the column "game_date", in the search queries, which is a simple regular column of a Date type. I have no idea what's going on with all this thing.

DB I'm using is MySql.

Here comes the code itself:

Entity:

import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity @Table(name = "games") public class Games { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id_game") private int id; @Column(name = "game_date", columnDefinition = "DATE") @Temporal(TemporalType.DATE) private Date gameDate; public Date getGame_date() { return gameDate; } public void setGame_date(Date _game_date) { this.gameDate = _game_date; } }

And a repository:

import java.sql.Date; import java.util.List; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RepositoryRestResource; @RepositoryRestResource public interface GamesRepository extends CrudRepository< Games , Integer > { List< Games > findById( @Param( "id" ) int id ); List< Games > findAll( ); List<Games> findByGameDateAndOpponent(@Param("game_date") Date game_date, @Param("opponent") String opponent); }

最满意答案

下划线是Spring Data JPA中的保留关键字 。 它应该足以从你的属性和它的getter和setter中删除它,Hibernate将完成剩下的工作:

@Entity @Table(name = "games") public class Games { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id_game") private int id; //Getter and setters for id @Column(name = "game_date") private Date gameDate; public Date getGameDate() { return gameDate; } public void setGameDate(Date gameDate) { this.gameDate = gameDate; } }

此外,通常,尝试对变量和字段名称使用java命名约定 ,这是首先使用小写的大小写混合。

也可以看看:

Spring Data JPA存储库方法无法识别带下划线的属性名称

The underscore is a reserved keyword in Spring Data JPA. It should be enough to remove it from your property and from its getters and setters and Hibernate will do the rest:

@Entity @Table(name = "games") public class Games { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id_game") private int id; //Getter and setters for id @Column(name = "game_date") private Date gameDate; public Date getGameDate() { return gameDate; } public void setGameDate(Date gameDate) { this.gameDate = gameDate; } }

Also, in general, try to use java naming convention for variable and field names, which is mixed case with lowercase first.

See also:

Spring Data JPA repository methods don't recognize property names with underscores

更多推荐

本文发布于:2023-08-02 11:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1375489.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Spring   JPA   CRUD

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!