0

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);

}
Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Anton
  • 57
  • 8

1 Answers1

1

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:

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217