-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to avoid redundancy using Hibernate
PostPosted: Mon Apr 08, 2013 4:20 pm 
Newbie

Joined: Mon Apr 08, 2013 3:51 pm
Posts: 1
Hi, I'm new to hibernate, so sorry for silly question. I have many-to-many mapping (Teacher to Languages they know). What I want to do is to avoid redundancy. So how to tell Hibernate when adding to "languages" table, that he should add language only and only if LANGUAGE_NAME is not repeated? Otherwise just add record in "middle" table (It do so, but also redundantly adds same language). I think that using equals would do the job but on the other hand that would be inefficient because he need to download all content from certain table (to run java method).
I have following class

Code:
@Entity
@Table(name = "languages")
public class Language implements Serializable {
   private static final long serialVersionUID = 5L;
   
   @Id
   @GeneratedValue
   @Column(name = "LANGUAGE_ID")
   private int id;

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }
   
   @Column(name = "LANGUAGE_NAME")
   private String name;

   public String getName() {
      return this.name;
   }

   public void setName(String name) {
      this.name = name;
   }

   @ManyToMany(mappedBy = "foreignLanguage")
   private Set<Teacher> teachersSet;
       
        ...


Top
 Profile  
 
 Post subject: Re: How to avoid redundancy using Hibernate
PostPosted: Tue Apr 09, 2013 6:06 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
The most natural approach to solve this, is to declare language name as @NaturalId

Code:
@Column(name = "LANGUAGE_NAME")
@NaturalId (mutable = true)
private String name;


Then in your business code, you can check the existance of a language by:

Language lang = (Language) session.bySimpleNaturalId(Language.class).load(newlanguage);
if (lang == null) { // create the new language only if it does not exist yet
lang = new Language(newlanguage);
session.save(lang);
}

Furthermore the @NaturalId declaration has as conseguence that an unique index will be defined on column LANGUAGE_NAME when
you generate the schema. This avoids duplicate entries a priori.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.