0

I'm trying to create a service to process all unfulfilled bets in a betting game, once a Match result is known. I'm getting a ClassCastException when I'm trying to use the results of a query in a JpaRepository.

Here are my service's interface and implementation

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
            }
        }
    }
}

Also, the repository

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>
}

I tried also with a Java version, with no luck

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

And finally, the Bet entity, which is a 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;
    }
}

The exception I'm getting is:

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]

The list that is being returned by the repository contains Objects, despite being typed with Bet.
Any ideas?

2
  • 1
    This seems to be related to this question, stackoverflow.com/questions/23122846/… Commented Jun 13, 2018 at 22:55
  • Thanks a lot @FabriPautasso! Changing the query to SELECT b FROM Bet b where b.match = :match and b.played = false was the solution Commented Jun 13, 2018 at 23:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.