0

This is the first entity:

    @Entity
@Table(name = "photographers")
public class Photographer {

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

    @NotNull
    @Column(name = "first_name")
    private String firstName;

    @NotNull
    @Size(min = 2, max = 50)
    @Column(name = "last_name")
    private String lastName;

    @Pattern(regexp = "^\\+[0-9]{1,3}/[0-9]{8,10}$")
    private String phone;

    @NotNull
    @OneToOne(optional = false)
    @JoinColumn(name = "primary_camera_id", referencedColumnName = "id")
    private BasicCamera primaryCamera;

    @NotNull
    @OneToOne(optional = false)
    @JoinColumn(name = "secondary_camera_id", referencedColumnName = "id")
    private BasicCamera secondaryCamera;

    @OneToMany(mappedBy = "owner")
    private Set<Lens> lenses;

    @OneToMany(mappedBy = "owner")
    private Set<Accessory> accessories;

and this is the other(lenses) :

@Entity
@Table(name = "lenses")
public class Lens {

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

    private String make;

    @Column(name = "focal_length")
    private Integer focalLength;

    @Column(name = "max_aperture", precision = 1)
    private Double maxAperture;

    @Column(name = "compatible_with")
    private String compatibleWith;

    @ManyToOne
    @JoinColumn(name = "owner_id", referencedColumnName = "id")
    private Photographer owner;

Firstly i have lenses without owners. Then i want to create a photographer with everything and set its lenses to exiting ones i do this with this code:

 for (Lens lens : lensSet) {
            lens.setOwner(photographer);
        }

        photographer.setPrimaryCamera(primaryCamera);
        photographer.setSecondaryCamera(secondaryCamera);
        photographer.setLenses(lensSet);

and when i save the photographer i want the lenses owner to be set to the created one. I tried cascad.MERGE on the OneToMany realation in the photographer entity but did't work and in the database all lenses owners are still nulls

Ivan Kirchev
  • 133
  • 1
  • 9
  • Can you post error log here. – Akash Aug 12 '17 at 17:07
  • there are no errors the entity is persisted in the database but the lenses are not updated When i tried cascade.All on OneToMany side this error occurred: detached entity passed to persist: exmaprep.model.Entity.Lens – Ivan Kirchev Aug 12 '17 at 17:09
  • You have two independent entities (so called [aggregate root](https://stackoverflow.com/q/1958621)) - Photographer and Len. So you need to have repositories for both. First you save your lens, then add them to the photographer, then save the photographer. – Cepr0 Aug 12 '17 at 17:13

1 Answers1

0

I figured it out : i need to save the lenses AFTER i save the photographer:

for (Lens lens : lensSet) {
            lens.setOwner(photographer);
        }

        photographer.setPrimaryCamera(primaryCamera);
        photographer.setSecondaryCamera(secondaryCamera);
        photographer.setLenses(lensSet);


        this.photographerRepository.save(photographer);
        this.lensRepository.save(lensSet);
Ivan Kirchev
  • 133
  • 1
  • 9