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