How to save a detached entity and replace the content of a one to many relationship?
<p>I want to replace the content of a one to many relationship when I call save() on a detached entity but it only add the new occurrences and does not remove the old ones (I don't want to load the entity from the database).</p>

<p>Is there a setting to clear relationships without loading/attach the entity?</p>

<p>I'm using spring-data-jpa.</p>

<p>Here is my service:</p>

<pre><code>@Component
@Transactional
public class StuffService {
    @Autowired
    StuffDao stuffDao;

    public void save(StuffDto stuffDto) {
        // The converter maps the properties into the entity, id included
        Stuff stuff= stuffConverter.dtoToBo(stuffDto);
        stuffDao.save(stuff);
    }
}
</code></pre>

<p>Dtos</p>

<pre><code>public class StuffDto {
    private Long id;

    @NotNull(message = "products-empty")
    @Valid
    private List&lt;ProductStuffDto&gt; products;
}

public class ProductStuffDto {
    Integer order;

    String name;
}
</code></pre>

<p>Entities</p>

<pre><code>@Entity
public class Stuff {

    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(mappedBy = "stuff", cascade = CascadeType.ALL)
    private List&lt;ProductStuff&gt; products;
}

@Entity
public class ProductStuff {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "stuff_id", nullable = false)
    private Stuff stuff;

    Integer order;

    String name;
}
</code></pre>

<p>Dao</p>

<pre><code>public interface StuffDao extends JpaRepository&lt;Stuff, Long&gt; {}
</code></pre>

<p>Thank you for your help.</p>