我正在创建一个简单的锦标赛数据库和其中的比赛。我创建了 4 个表:玩家、比赛、锦标赛和锦标赛回合。每个锦标赛有多个回合,每个回合有多个比赛,每场比赛只有两名选手。每个锦标赛还应该跟踪所有注册的玩家。我为这些表中的每一个创建了一个实体,有两个期望:
第一个期望有效,第二个期望无效。
这是我对表格的(简化的)定义:
@MappedEntity(value = "player")
data class PlayerEntity(
@field:Id @GeneratedValue val id: Int? = null,
)
@MappedEntity(value = "match")
data class MatchEntity(
@field:Id @GeneratedValue val id: Int? = null,
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "player_a_id", nullable = false, columnDefinition = "int")
val playerA: PlayerEntity,
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "player_b_id", nullable = false, columnDefinition = "int")
val playerB: PlayerEntity,
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "tournament_round_id", nullable = false, columnDefinition = "int")
val tournamentRound: TournamentRoundEntity,
)
@MappedEntity(value = "tournament_round")
data class TournamentRoundEntity(
@field:Id @GeneratedValue val id: Int? = null,
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "tournament_id")
val tournament: TournamentEntity,
@OneToMany(mappedBy = "tournamentRound", orphanRemoval = true, cascade = [CascadeType.ALL], fetch = FetchType.LAZY)
val matches: List<MatchEntity>,
)
@MappedEntity(value = "tournament")
data class TournamentEntity(
@field:Id
@GeneratedValue
val id: Int? = null,
@JoinTable(name = "tournament_player",
joinColumns = [JoinColumn(name = "tournament_id", referencedColumnName = "id")],
inverseJoinColumns = [JoinColumn(name = "player_id", referencedColumnName = "id")]
)
@ManyToMany(cascade = [CascadeType.ALL])
val players: List<PlayerEntity>,
@OneToMany(mappedBy = "tournament", orphanRemoval = true, cascade = [CascadeType.ALL])
val tournamentRounds: List<TournamentRoundEntity>,
)
我所有的存储库看起来都像这样:
@JdbcRepository(dialect = Dialect.MYSQL)
@Transactional(Transactional.TxType.MANDATORY)
interface TournamentRepository : CrudRepository<TournamentEntity, Int>, JpaSpecificationExecutor<TournamentEntity>
保存锦标赛也会在关联表中正确创建适当的记录。然而,获取锦标赛(通过
tournamentRepository.findAll()
或tournamentRepository.findById(id)
)不会从关联的表中获取任何内容。使用查询日志记录,我可以看到它甚至没有以任何方式尝试这样做:
// This is what happens when I call tournamentRepository.findAll()
DEBUG io.micronaut.data.query - Executing Query: SELECT tournament_entity_.`id` FROM `tournament` tournament_entity_
我试过弄乱注释但没有结果。我很乐意感谢任何帮助!