Yes, sorry. I was originally trying to do this with spring-data and when that failed I fell back to trying to do it with standard JPA and I mixed them up in the sample. I'll cut and paste some snippets from my actual code to try to avoid those kinds of mistakes again. To give a little more context I am trying to model sports teams and try to find games containing a specific player prior to a given date.
The (hopefully) relevant portions of the Entity classes
Code:
@Entity
@Table(name="team")
public class Team implements Serializable, Comparable<Team> {
private static final long serialVersionUID = 1L;
@Id
@TableGenerator(
name="team_generator",
table="id_sequence",
pkColumnName="name",
pkColumnValue="team",
valueColumnName="number"
)
@GeneratedValue(strategy=GenerationType.TABLE, generator="team_generator")
private Integer id;
@Column(name="name", length=180, nullable=false)
private String name;
/* Other fields and methods */
}
Code:
@Entity
@Table(name="game")
@NamedQueries({
@NamedQuery(
name="Game.findByDateAndTeam",
query="SELECT g FROM Game g WHERE g.gameDate = :date AND (g.homeTeam = :team OR g.awayTeam = :team)"
),
@NamedQuery(
name="Game.findPreviousGames",
query="SELECT g FROM Game g, Player p, BoxScoreRow b WHERE g.gameDate < :date AND g = b.game AND p = b.player AND p = :player ORDER BY g.gameDate DESC"
)
})
public class Game implements Serializable, Comparable<Game> {
@Id
@Column(name="id")
@TableGenerator(
name="game_generator",
table="id_sequence",
pkColumnName="name",
pkColumnValue="game",
valueColumnName="number"
)
@GeneratedValue(strategy=GenerationType.TABLE, generator="game_generator")
private Integer id;
@Column(name="game_date", nullable=false)
private LocalDate gameDate;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="home_team")
private Team homeTeam;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="away_team")
private Team awayTeam;
@OneToMany(mappedBy="game", cascade=CascadeType.ALL)
private List<BoxScoreRow> boxScore;
/* Other fields and methods */
}
Code:
@Entity
@Table(name="box_score")
public class BoxScoreRow implements Serializable {
private static final long serialVersionUID = 1L;
@Embeddable
public static class BoxScoreRowPK implements Serializable {
private static final long serialVersionUID = 1L;
private int game;
private int player;
/* Other fields and methods */
}
@EmbeddedId
private BoxScoreRowPK id;
@ManyToOne
@JoinColumn(name="game", insertable=false, updatable=false)
private Game game;
@ManyToOne
@JoinColumn(name="player", insertable=false, updatable=false)
private Player player;
/* Other fields and methods */
}
Then to the code that throws the exception:
Code:
@Transactional
@Test
public void testFindPreviousGame() {
Team homeTeam = /* An attached team retrieved from the DB */
Player player = /* An attached player from the DB */
LocalDate date = LocalDate.of(2015, 8, 1);
Game game =
entityManager.createNamedQuery("Game.findByDateAndTeam", Game.class)
.setParameter("date", date)
.setParameter("team", homeTeam)
.getSingleResult();
List<Game> previousGames =
entityManager.createNamedQuery("Game.findPreviousGames", Game.class)
.setParameter("date", date)
.setParameter("player", player)
.setMaxResults(5)
.getResultList();
}
The exception is
Code:
javax.persistence.EntityNotFoundException: Unable to find Team with id 571
And yet there is a Team with id 571 in the database.