Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
23.0k views
in Technique[技术] by (71.8m points)

postgresql - JPA Query Postgressql Issue

I have the following query defined in a CrudRepository:

interface RoundRepository : CrudRepository<Round, Long> {
     fun findByGameId(id: Long): Round?
     @Query("select r from Round r where r.game.id like ?1 order by r.createdAt desc")
     fun findLastByGameId(id: Long) : List<Round>
}

Game and Round Entities are defined as follows:

@Entity
class Game(
    @ManyToOne(optional = false)
    @JoinColumn(name = "firstplayer_id", referencedColumnName = "id")
    var firstPlayer: User,
    @ManyToOne(optional = false)
    @JoinColumn(name = "secondeplayer_id", referencedColumnName = "id")
    var secondPlayer: User,
    @Size(min = 6, max = 60)
    var gameName: String,
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    var id: Long = 0,
    @DateTimeFormat
    val createdAt: Date = Date.from(Instant.now())
) {
@OneToMany(mappedBy = "game")
lateinit var rounds: Collection<Round>
}


@Entity
@Table(name = "round")
class Round(
    @ManyToOne
    @JoinColumn(name="game_id", referencedColumnName = "id")
    var game: Game,
    var cancelled: Boolean = false,
    var ended: Boolean = false,
    var winner: Winner = Winner.FIRST_PLAYER,
    var stateFirstPlayer: Int = -1,
    var stateSecondPlayer: Int = -1,
    var moveIndexFirstPlayer: Int = -1,
    var moveIndexSecondPlayer: Int = -1,
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    var id: Long = 0,
    @DateTimeFormat
    val createdAt: Date = Date.from(Instant.now())
) {

@Transient
lateinit var firstPlayerMoves: Collection<Move>

@Transient
lateinit var secondPlayerMoves: Collection<Move>

@Transient
lateinit var firstPlayerPositions: PlanesPositions

@Transient
lateinit var secondPlayerPositions: PlanesPositions
}

When calling the method as follows:

var game: Game? = gamesRepository.findByGameName(gameName)
            ?: throw GameDoesNotExistException("A game with this name does not exist");
var rounds = roundRepository.findLastByGameId(game?.id!!)

I receive an exception: org.hibernate.exception.SQLGrammarException: could not extract ResultSet The inner exception from Postgres says: org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint ~~ bigint

Initially had the field game_id in round table not the not null specification. So I thought that was the problem. Now I fixed that but I still receive the same error. Can anyone help ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...