-->
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: Populating ManyToMany relations objects/tables
PostPosted: Tue Feb 14, 2017 10:26 am 
Newbie

Joined: Tue Feb 14, 2017 9:53 am
Posts: 2
New to this forum + hibernate!

I have two model classes -- Article and Tag -- annotated as follows:

Code:
@Entity
@Table(name="articles")
public class Article {
   @Id
   @GeneratedValue
   private int id;
   @Column(length=255, nullable=false)
   private String title;

    @ManyToMany(cascade = {CascadeType.ALL})
       @JoinTable(name = "articles_tags",
               joinColumns = @JoinColumn(name = "article_id", referencedColumnName = "id"),
               inverseJoinColumns = @JoinColumn(name = "tag_id", referencedColumnName = "id"))
       @OrderBy("name ASC")
       private List<Tag> tags = new ArrayList<>();
      
       ...
      
       }
   
@Entity
@Table(name = "tags")
public class Tag {

    @javax.persistence.Id
    @GeneratedValue
    private int Id;

    @Column(length = 30, nullable = false, unique = true)
    private String name;

    @ManyToMany(mappedBy = "tags")
    private Collection<Article> articles = new ArrayList<>();
    ..
   
    }



I have a form to prepare article with relevant tags for the topic. On submission of the article, it can populate all necessary tables,
viz. articles, tags and articles_tags, with no problem as long as the input tags are new. If the included tag is already in the database, however, it does not
work.

So, basically, I have to ensure is that if tag is new insert into List<Tag> list, and insert into the table, otherwise simply use the tag object
from the database in the List. I am pretty new to hibernate, could somebody in the forum provide some pointers on this?
Thanks,


Top
 Profile  
 
 Post subject: Re: Populating ManyToMany relations objects/tables
PostPosted: Tue Feb 14, 2017 11:36 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
You need to match entries based on their identifiers. If you already find a matching pair of entities, then you can just merge them.


Top
 Profile  
 
 Post subject: Re: Populating ManyToMany relations objects/tables
PostPosted: Tue Feb 14, 2017 4:37 pm 
Newbie

Joined: Tue Feb 14, 2017 9:53 am
Posts: 2
Hi,
I will try that. Thank for your reply - truly appreciated.


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.