-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Duplicate insert oneToMany
PostPosted: Mon Sep 23, 2013 9:03 am 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.