使用Kotlin在JpaRepository上的ClassCastException

时间:2018-06-13 19:42:32

标签: spring-boot jpa kotlin spring-data-jpa

一旦比赛结果已知,我就试图创建一项服务来处理投注游戏中所有未完成的投注。我在ClassCastException尝试使用查询结果时收到JpaRepository

以下是我的服务界面和实施

package com.github.juanmougan.prode.services

import com.github.juanmougan.prode.models.Match

interface MatchesService {
    fun processBetsForMatch(match: Match)
}


package com.github.juanmougan.prode.services

import com.github.juanmougan.prode.models.Bet
import com.github.juanmougan.prode.models.Match
import com.github.juanmougan.prode.repositories.BetsRepository
import org.springframework.stereotype.Service

@Service("messageService")
class MatchesServiceImpl(
                        val betsRepository: BetsRepository
                        ) : MatchesService {
    override fun processBetsForMatch(match: Match) {
        val unfulfilledBets: List<Bet> = betsRepository.findAllByMatchWhereBetHasNotBeenPlayed(match)
        unfulfilledBets.forEach { b ->
            b.played = true
            b.counted = true
            if (match.result?.equals(b.result)!!) {
                b.pointsWon = 1
            }
        }
    }
}

此外,存储库

package com.github.juanmougan.prode.repositories

import com.github.juanmougan.prode.models.Bet
import com.github.juanmougan.prode.models.Match
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param

interface BetsRepository : JpaRepository<Bet, Long> {
    @Query("SELECT b.id, b.match, b.result, b.player, b.played, b.counted, b.pointsWon " +
            "FROM Bet b where b.match = :match and b.played = false")
    fun findAllByMatchWhereBetHasNotBeenPlayed(@Param("match") match: Match): List<Bet>
}

我还尝试使用Java版本,没有运气

package com.github.juanmougan.prode.repositories;

import com.github.juanmougan.prode.models.Bet;
import com.github.juanmougan.prode.models.Match;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface BetsRepository extends JpaRepository<Bet, Long> {
    @Query("SELECT b.id, b.match, b.result, b.player, b.played, b.counted, b.pointsWon " +
            "FROM Bet b where b.match = :match and b.played = false")
    List<Bet> findAllByMatchWhereBetHasNotBeenPlayed(@Param("match")Match match);
}

最后,Bet实体,这是一个Java POJO

package com.github.juanmougan.prode.models;

import javax.persistence.*;

@Entity
public class Bet {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @ManyToOne
    private Match match;
    private Result result;
    @ManyToOne
    private Person player;
    private Boolean played;
    private Boolean counted;
    private Integer pointsWon;

    public Bet() {
    }

    public Bet(Match match, Result result, Person player, Boolean played, Boolean counted, Integer     pointsWon) {
        this.match = match;
        this.result = result;
        this.player = player;
        this.played = played;
        this.counted = counted;
        this.pointsWon = pointsWon;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Bet bet = (Bet) o;

        if (id != null ? !id.equals(bet.id) : bet.id != null) return false;
        if (match != null ? !match.equals(bet.match) : bet.match != null) return false;
        if (result != bet.result) return false;
        if (player != null ? !player.equals(bet.player) : bet.player != null) return false;
        if (played != null ? !played.equals(bet.played) : bet.played != null) return false;
        if (counted != null ? !counted.equals(bet.counted) : bet.counted != null) return false;
        return pointsWon != null ? pointsWon.equals(bet.pointsWon) : bet.pointsWon == null;
    }

    @Override
    public int hashCode() {
        int result1 = id != null ? id.hashCode() : 0;
        result1 = 31 * result1 + (match != null ? match.hashCode() : 0);
        result1 = 31 * result1 + (result != null ? result.hashCode() : 0);
        result1 = 31 * result1 + (player != null ? player.hashCode() : 0);
        result1 = 31 * result1 + (played != null ? played.hashCode() : 0);
        result1 = 31 * result1 + (counted != null ? counted.hashCode() : 0);
        result1 = 31 * result1 + (pointsWon != null ? pointsWon.hashCode() : 0);
        return result1;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Match getMatch() {
        return match;
    }

    public void setMatch(Match match) {
        this.match = match;
    }

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }

    public Person getPlayer() {
        return player;
    }

    public void setPlayer(Person player) {
        this.player = player;
    }

    public Boolean getPlayed() {
        return played;
    }

    public void setPlayed(Boolean played) {
        this.played = played;
    }

    public Boolean getCounted() {
        return counted;
    }

    public void setCounted(Boolean counted) {
        this.counted = counted;
    }

    public Integer getPointsWon() {
        return pointsWon;
    }

    public void setPointsWon(Integer pointsWon) {
        this.pointsWon = pointsWon;
    }
}

我得到的例外是:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.github.juanmougan.prode.models.Bet
at com.github.juanmougan.prode.services.MatchesServiceImpl.processBetsForMatch(MatchesServiceImpl.kt:31) ~[classes/:na]
at com.github.juanmougan.prode.controllers.MatchesController$updateById$1.apply(MatchesController.kt:34) ~[classes/:na]
at com.github.juanmougan.prode.controllers.MatchesController$updateById$1.apply(MatchesController.kt:12) ~[classes/:na]
at java.util.Optional.map(Optional.java:215) ~[na:1.8.0_66]
at com.github.juanmougan.prode.controllers.MatchesController.updateById(MatchesController.kt:31) ~[classes/:na]

存储库返回的列表包含对象,尽管键入了Bet 有什么想法吗?

0 个答案:

没有答案