Manuel Waltschek
Manuel Waltschek

Reputation: 555

Hibernate @OneToMany reference child not updated / lost?

Say I have three entities.

@Entity
public class Process {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(unique = true)
    private String name;

    @ManyToAny(
            metaColumn = @Column(name = "node_type"),
            fetch = FetchType.LAZY
    )
    @AnyMetaDef(
            idType = "long", metaType = "string",
            metaValues = {
                    @MetaValue(targetEntity = Milestone.class, value = MILESTONE_DISC),
                    @MetaValue(targetEntity = Phase.class, value = PHASE_DISC)
            }
    )
    @Cascade({org.hibernate.annotations.CascadeType.ALL})
    @JoinTable(
            name = "process_nodes",
            joinColumns = @JoinColumn(name = "process_id", nullable = false),
            inverseJoinColumns = @JoinColumn(name = "node_id", nullable = false)
    )
    private Collection<ProcessNode> nodes = new ArrayList<>();

    ...
}

@Entity
@ToString
@DiscriminatorValue(MILESTONE_DISC)
public class Milestone implements ProcessNode {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Collection<ResultDefinition> results;

    @ManyToOne()
    private Process process;

...
}

@Entity
@ToString
public class ResultDefinition {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String externalId;
    private String name;
    private ResultType resultType;

}

From my client I want to add an Object of type ResultDefinition to a Milestone in a Process like this:

@Transactional
@PostMapping("/{milestone_id}/results")
public ResultDefinitionDto createResult(@PathVariable("milestone_id") Long milestoneId, @RequestBody ResultDefinitionDto dto) {
    Process foundProcess = getProcess(milestoneId);
    checkFoundProcess(milestoneId, foundProcess);
    Milestone milestone = getMilestone(foundProcess, milestoneId);
    ResultDefinition resultDefinition = resultDefinitionMapper.fromDTO(dto);
    milestone.addResult(resultDefinition);
    processService.save(foundProcess);
    //TODO: Find out why this is necessary (???)
    ResultDefinition savedResult = milestone.getResult(resultDefinition.getName());
    return resultDefinitionMapper.fromEntity(savedResult);
}

In my method createResult I add resultDefinition to the milestone results collection. When I save the parent foundProcess, I see that foundprocess->milestone->resultDefinition get's persisted and gets an ID. When I call resultDefinition.getId() it returns null. Also the ResultDefinition Object in the foundProcess is another reference and not the same that I added to milestone.results.

Why do I get the correct instance when calling milestone.getResult()?


Edit: my implementation of processService / repository

@Override
public Process save(Process entity) {
    return processRepository.saveAndFlush(entity);
}

public interface ProcessRepository extends JpaRepository<Process, Long>, JpaSpecificationExecutor<Process> {
        ...
    }

Upvotes: 0

Views: 198

Answers (2)

C. Weber
C. Weber

Reputation: 937

The ResultDefinition gets replaced during the save process. The transient entity gets inserted into the database and will be replaced through a managed entity with an id. Your reference to the ResultDefinition in the createResult method still points to the transient one. That´s why you have to work with the returned entities from a save call.

In your case you are saving the parent process. So you have to access the saved ResultDefinition through the process or milestone entity.

Upvotes: 1

CimaW
CimaW

Reputation: 29

You can try add flush method after save.

Upvotes: 0

Related Questions