postgresql - Micronaut Data 的级联持久不保存子对象

标签 postgresql micronaut micronaut-data

保存创建新子对象的所属实体对象后,发生了奇怪的事情。尽管 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;
}

TeamPlayer

@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 的批量选择的值未绑定(bind)..

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

最佳答案

此问题的原因是 ID 被定义为基元而不是对象 Integer。更改为 private Integer id; 后,它可以工作。

关于postgresql - Micronaut Data 的级联持久不保存子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59216990/

相关文章:

java - Micronaut 循环依赖

java - Micronaut,声明式 Http-Client,JSON 序列化包装 HTTP Post 对象

micronaut - 我如何限制我的 micronaut 服务器应用程序的内存

java - Micronaut 无法实现存储库方法 : Repository. 更新(对象实体)。找不到可能的实现

实体中的 Micronaut 数据和枚举字段

database - 自动分片postgresql?

postgresql - PostgreSQL 中的 "use database_name"命令

sql - PostgreSQL 更新触发器

python - 为 Python Web 应用程序设置环境变量

postgresql - 如何将 JsonNode 参数(micronaut + kotlin)保存为 postgres 数据库中的 jsonb