-->
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.  [ 3 posts ] 
Author Message
 Post subject: Spring, Hibernate, CSV @ManyToMany data populate
PostPosted: Fri Jul 21, 2017 1:50 pm 
Newbie

Joined: Fri Jul 21, 2017 1:46 pm
Posts: 2
So I have 2 entities - Post and Tag and there is a many to many relation between them. I also have a CSV file and need to pre-populate the database (it contains post details including the ids of the matching Tag entities). Moreover following DDD, the Post entity is the Aggregate root with its PostRepository. Given below are the java classes: -

Code:
@Entity
public class Post{
@Id
@GeneratedValue
private long id;

  @ManyToMany(cascadeType = {MERGE, PERSIST})
  @JoinTable(name = "post_tag",
        joinColumns = @JoinColumn(name = "post_id"),
        inverseJoinColumns = @JoinColumn(name = "tag_id"))
  private Set<Tag> tags = new HashSet<>();

  // getters ommited.

  public void addTag(Tag tag){
    tags.add(tag);
    tag.getPosts().add(this);
  }
}

@Entity
public class Tag{
@Id
  private long id;

  private Set<Post> posts = new HashSet<>();

  // getters ommited.
  }
}
Repository interface below : -

public interface PostRepo extends Repository<Post, Long>{
  void save(Post p);
}

Database pre-loading code below : -

Code:
@Service
public class Service {

  @Inject
  private PostRepo repo;

  @PostConstruct
  void init(){
   // parse CSV file and get a Stream<String[]> stream

      stream.foreach( splits -> {
        long tagId = Long.parseLong(splits[0]);
         Post post = new Post();
         post.add(new Tag(tagId);
         repo.save(post);
     }
  }
}


While prepopulating the DB using the
Code:
PostRepo::save(post)
method I am running into a Primary key constraint validation error, and I think the reason behind that being while adding a Post entity I am also trying to add a new Tag with the same id as a pre-existing Tag entity.

So here is my question how can I do it correctly without adding a TagRepository and using the saveOrUpdate method of the TagRepository please?


Last edited by aprofromindia on Sat Jul 22, 2017 6:31 am, edited 3 times in total.

Top
 Profile  
 
 Post subject: Re: Spring, Hibernate, CSV @ManyToMany data populate
PostPosted: Fri Jul 21, 2017 11:58 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
There are many things you are doing it wrong here:

1. You have a bidirectional @ManyToMany association but you don't synchronize both end of the association. Check out this article for more details.
2. You're using Lists with @ManyToMany association which are not as efficient as Set.
3. You're not using any batching at all. Check out this article for more details.


Top
 Profile  
 
 Post subject: Re: Spring, Hibernate, CSV @ManyToMany data populate
PostPosted: Sat Jul 22, 2017 6:20 am 
Newbie

Joined: Fri Jul 21, 2017 1:46 pm
Posts: 2
Hey vlad many thanks for trying to answer the question, I followed some of your advice but it still doesnt solve the problem.

vlad wrote:
There are many things you are doing it wrong here:

1. You have a bidirectional @ManyToMany association but you don't synchronize both end of the association. Check out this article for more details.


Fixed in the above post, but it doesnt solve the error.

vlad wrote:


Migrated to Set with same failing error.

vlad wrote:
3. You're not using any batching at all. Check out this article for more details.


Batch update might make it more performant but doesnt solve the problem 'how do I pre-populate a DB from a CSV having many to many associations' ? TBH I am wondering if JPA is the right tool for the task or should I use JDBCTemplates for it ?

Moreover since we are talking, 1 quick question for a ManyToMany association do you still maintain (based on your book) that 2 separate one to many is the most performant mapping please?


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

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.