0

在保存创建新子对象的拥有实体对象后,发生了一件奇怪的事情。尽管 SQL 日志还显示了子实体的插入,但只保存了拥有的对象。

团队

@Data
@EqualsAndHashCode(of = "id")
@NoArgsConstructor
@Entity
public class Team implements Comparable<Team> {
    @Id
    @GeneratedValue
    private int id;
    private String name;
    @Relation(value = Relation.Kind.ONE_TO_MANY, cascade = Relation.Cascade.PERSIST, mappedBy = "team")
    private List<TeamPlayer> teamPlayers;
    private int givenGoals;
    private int gotGoals;
    private int points;
    @Relation(value = Relation.Kind.MANY_TO_ONE)
    private Tournament tournament;
}

有团队精神的人

@Data
@EqualsAndHashCode(of = "id")
@NoArgsConstructor
@Entity
public class TeamPlayer {
    @Id
    @GeneratedValue
    private int id;
    @Relation(value = Relation.Kind.MANY_TO_ONE)
    private Team team;
    @Relation(Relation.Kind.MANY_TO_ONE)
    private Player player;
}

播放器

@Data
@EqualsAndHashCode(of = "id")
@NoArgsConstructor
@Entity
public class Player {
    @Id
    @GeneratedValue
    private int id;
    private String forename;
    private String surname;
    private int rank;
    @Relation(value = Relation.Kind.ONE_TO_ONE, cascade = Relation.Cascade.ALL)
    private User user;
}

团队存储库

@JdbcRepository
public interface TeamRepository extends CrudRepository<Team, Integer> {}

保存集合后 teamRepository.saveAll(teams) 输出是......

17:16:32.160 [pool-1-thread-11] DEBUG io.micronaut.data.query - Executing Batch SQL Insert: INSERT INTO "team" ("name","given_goals","got_goals","points","tournament_id") VALUES (?,?,?,?,?)
17:16:33.308 [pool-1-thread-11] DEBUG io.micronaut.data.query - Executing Batch SQL Insert: INSERT INTO "team_player" ("team_id","player_id") VALUES (?,?)
17:16:33.308 [pool-1-thread-11] DEBUG io.micronaut.data.query - Executing Batch SQL Insert: INSERT INTO "team_player" ("team_id","player_id") VALUES (?,?)
17:16:33.309 [pool-1-thread-11] DEBUG io.micronaut.data.query - Executing Batch SQL Insert: INSERT INTO "team_player" ("team_id","player_id") VALUES (?,?)
17:16:33.309 [pool-1-thread-11] DEBUG io.micronaut.data.query - Executing Batch SQL Insert: INSERT INTO "team_player" ("team_id","player_id") VALUES (?,?)
17:16:33.309 [pool-1-thread-11] DEBUG io.micronaut.data.query - Executing Batch SQL Insert: INSERT INTO "team_player" ("team_id","player_id") VALUES (?,?)
17:16:33.310 [pool-1-thread-11] DEBUG io.micronaut.data.query - Executing Batch SQL Insert: INSERT INTO "team_player" ("team_id","player_id") VALUES (?,?)

但是,仅在数据库中创建了团队,并且没有显示进一步的错误。

有人可以帮我解决这个问题吗?

编辑:将日志记录更改为跟踪级别后,我可以看到表 team_player 的批处理选择的值未绑定..

14:31:40.928 [pool-1-thread-4] DEBUG io.micronaut.data.query - Executing SQL Insert: INSERT INTO "team" ("name","given_goals","got_goals","points","tournament_id") VALUES (?,?,?,?,?)
14:31:40.928 [pool-1-thread-4] TRACE io.micronaut.data.query - Binding value Team1 to parameter at position: 1
14:31:40.928 [pool-1-thread-4] TRACE io.micronaut.data.query - Binding value 0 to parameter at position: 2
14:31:40.928 [pool-1-thread-4] TRACE io.micronaut.data.query - Binding value 0 to parameter at position: 3
14:31:40.928 [pool-1-thread-4] TRACE io.micronaut.data.query - Binding value 0 to parameter at position: 4
14:31:40.928 [pool-1-thread-4] TRACE io.micronaut.data.query - Binding value 2 to parameter at position: 5
14:31:40.959 [pool-1-thread-4] DEBUG io.micronaut.data.query - Executing Batch SQL Insert: INSERT INTO "team_player" ("team_id","player_id") VALUES (?,?)
14:31:40.991 [pool-1-thread-4] DEBUG io.micronaut.data.query - Executing Query: UPDATE "tournament" SET "status"=? WHERE ("id" = ?)
...
4

1 回答 1

0

此问题的原因是 ID 被定义为原语而不是对象整数。改成之后private Integer id;就可以了。

于 2020-01-18T20:09:49.087 回答