Hi,
I get duplicate inserts when I think I shouldn't.
Code:
public class Post{
@OneToMany(cascade = CascadeType.ALL, orphanRemoval=true,fetch = FetchType.LAZY, mappedBy="post")
@BatchSize(size=5)
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region=CacheRegion.PHOTO)
@IndexedEmbedded(prefix="photo.")
public List<Photo> getPhotos() {
return photos;
}
}
public class Photo{
@ManyToOne(optional=true, fetch=FetchType.LAZY)
@JoinColumn(name="FK_PostID",nullable=true)
@ContainedIn
@JsonIgnore
public WallPost getPost() {
return post;
}
}
The reason the Post reference is nullable is because I use Photo in other entities as well with the same kind of setup. So Photos are not exclusive to Posts.
This code leads to duplicate inserts
Code:
photo.setPost(post);
post.getPhotos().add(photo);
postMgr.save(post);
This code works correctly
Code:
photo.setPost(post);
photo = photoMgr.save(photo);
post.getPhotos().add(photo);
postMgr.save(post);
Another way to go would be
Code:
photo.setPost(post);
photo = photoMgr.save(photo);
because the relation is owned by Post. But for some reason I HSearch didn't pick write the change to the index or didn't do it fast enough. Anyway, it was late and I went with number 2. Still think number 1 should work also though.