Stefan Bratanov
Stefan Bratanov

Reputation: 101

@OneToMany and @ManyToOne Spring Data JPA Child does not update Parent

I am struggling with simple scenario, where I have to specify a Team of Many developers and a Developer can only have one team.

These are my current two POJOs, but it is starting to get complex and i start to hate it. Not sure what is the best approach.

@Entity
@Data
@EqualsAndHashCode(exclude = {"team"})
@ToString(exclude = {"team"})
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Developer {

    @Id
    @GeneratedValue
    private Long id;

    @Column(nullable = false)
    private String name;

    @ManyToOne(optional = false)
    @JsonIgnoreProperties(value = {"developers"})
    private Team team;

}
@Entity
@Data
@EqualsAndHashCode(exclude = {"developers"})
@ToString(exclude = {"developers"})
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Team {

    @Id
    @GeneratedValue
    private Long id;

    @Column(nullable = false)
    private String name;

    private String description;

    @OneToMany(mappedBy = "team", cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = {"team"})
    private List<Developer> developers;

}

The problem I have now is if I create a Team and then assign two developers to it and then retrieve the team again, the developers list is still null. What am i missing? I can manually update the list of the team of course, but that defeats the purpose of JPA helping me. Any advice would be helpful.

    @Autowired
    private TeamRepository underTest;

    @Autowired
    private DeveloperRepository developerRepository;

Team team = Team.builder()
        .name("Team 1")
        .build();

Team team1 = underTest.save(team);

Developer developer1 = Developer.builder()
        .name("Stefan Bratanov")
        .team(team1)
        .build();

Developer developer2 = Developer.builder()
        .name("Darth Vader")
        .team(team1)
        .build();

List<Developer> developers = Arrays.asList(developer1, developer2);

developerRepository.saveAll(developers);

Optional<Team> maybeTeamWithDevelopers = underTest.findById(team1.getId());
assertThat(maybeTeamWithDevelopers).isPresent();

Team teamWithDevelopers = maybeTeamWithDevelopers.get();

assertThat(teamWithDevelopers.getDevelopers()).containsExactlyInAnyOrder(developer1, developer2); //DEVELOPERS IS NULL HERE

Upvotes: 1

Views: 2029

Answers (1)

stephane brun
stephane brun

Reputation: 264

    @Entity
    @Data
    @EqualsAndHashCode(exclude = {"developers"})
    @ToString(exclude = {"developers"})
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public class Team {
    
        @Id
        @GeneratedValue
        private Long id;
    
        @Column(nullable = false)
        private String name;
    
        private String description;
    
        @OneToMany(mappedBy = "team", cascade = CascadeType.ALL)
        @JsonIgnoreProperties(value = {"team"})
        private List<Developer> developers = new ArrayList<>;
        
        Team addDeveloper(Developer dev) {
            dev.setTeam(this);
            this.getDevelopers().add(dev);
            return this;

        }
    }

Upvotes: 1

Related Questions