Micronaut Data的级联持久化不会保存子对象

时间:2019-12-06 16:34:21

标签: postgresql micronaut micronaut-data

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

团队

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

TeamPlayer

@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

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 (?,?) 输出是...

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" = ?)
...

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

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

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

FormattedNumber

1 个答案:

答案 0 :(得分:0)

此问题的原因是ID被定义为原语,而不是对象Integer。更改为private Integer id;后,它就可以使用。