2

After saving the owning entity object which creates new child objects, a strange thing happens. Only the owning object is saved, though the SQL logs also shows inserts for the child entities.

Team

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

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

TeamRepository

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

After saving the collection with teamRepository.saveAll(teams) the output is...

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 (?,?)

However only the Team is created in the database and no further error is shown.

Can someone help me with this issue?

Edit: After changing logging to trace level i can see that the values for the batch select for table team_player are not binded..

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" = ?)
...
Jan Korcak
  • 41
  • 4
  • You haven't shown code that indicates how you are saving the instances. – Jeff Scott Brown Dec 06 '19 at 16:39
  • I have updated the post, however the comments are same even when changing saving item by item with ```save()```. Note: I`m using default methods save and saveAll from CrudRepository – Jan Korcak Dec 08 '19 at 08:40
  • One note on logging. If you turn on TRACE level logging for io.micronaut.data, you will get the values used in the SQL. I noticed that the DEBUG message for Batch SQL Insert does not tell you how many records are inserted. The DEBUG msg is logged, even if there are no records to inserted. TRACE level will show values for each row (if any) inserted. Based on the Release Notes for 1.0.0.M5, it supports cascading on insert (save). Perhaps it does not support it on update() yet? – Mike Houston Dec 10 '19 at 19:59
  • Thank you for the hint, I didn`t know how to show binded values in micronaut. Speaking of cascade update, it is not yet supported, but how does it relates? – Jan Korcak Dec 11 '19 at 07:59

1 Answers1

2

The cause of this issue was that IDs was defined as primitives instead of object Integer. After change to private Integer id; it works.

Jan Korcak
  • 41
  • 4