-->
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: many to many hibernat mapping just save first object
PostPosted: Wed Sep 16, 2009 9:06 am 
Newbie

Joined: Wed Jul 01, 2009 1:13 pm
Posts: 2
I have two table Post and Tag with relationship Nx N . When set several tags to um Post an save Post Object,hibernate saved Post and first Tag,the other Tags donĀ“t saved .Post code an mapping classes.

Post
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <!-- put your value object in the class name, the table name is optional
      but it is a good idea to specify it -->
   <class name="org.sakaiproject.forummultimidia.model.Post"
      table="FORUM_MULTIMIDIA_POST" dynamic-update="true"
   dynamic-insert="true" select-before-update="false">

      <!-- create the primary key id, using native is typically the best way
         to do this -->
      <id name="idPost" type="long" column="idPost">
          <generator class="native">
                <param name="sequence">POST_ID_SEQ</param>
          </generator>
      </id>
      
      <property name="title" type="string" length="200" not-null="true">
            <column name="TITLE"/>
        </property>   
       
        <property name="message" type="string" length="999" not-null="false">
            <column name="MESSAGE"/>
        </property>   
       
       <property name="urlMidia" type="string" length="999" not-null="false">
            <column name="URLMIDIA"/>
        </property>    
       
       <property name="typeMidia" type="character" not-null="false">
            <column name="TYPEMIDIA"/>
        </property>   
       
      <property name="postDate" type="timestamp" not-null="true">
            <column name="POSTDATE"/>
       </property>
       
       <property name="idPostFather" type="long" not-null="false">
            <column name="IDPOSTFATHER"/>
       </property>
       
      <property name="idUser" type="string" length="999" not-null="true">
            <column name="IDUSER"/>
       </property>
     
     <property name="idSite" type="string" length="999" not-null="true">
            <column name="IDSITE"/>
     </property>   
          
       
     <set name="tags" table="FORUM_MULTIMIDIA_POST_TAGS"  cascade="all">
        <key column="idPost" />
        <many-to-many column="idTag"
            class="org.sakaiproject.forummultimidia.model.Tag"/>
    </set>       
                
       
   </class>
   
</hibernate-mapping>

Tag

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <!-- put your value object in the class name, the table name is optional
      but it is a good idea to specify it -->
   <class name="org.sakaiproject.forummultimidia.model.Tag"
      table="FORUM_MULTIMIDIA_TAG" >

      <!-- create the primary key id, using native is typically the best way
         to do this -->
      <id name="idTag" type="long" column="idTag">
          <generator class="native">
                <param name="sequence">TAG_ID_SEQ</param>
          </generator>
      </id>
      
      <property name="name" type="string" length="100" not-null="true">
            <column name="NAME"/>
        </property>   
       
        <property name="url" type="string" length="999" not-null="true">
            <column name="URL"/>
        </property>   
       
       <property name="frequency" type="long" not-null="false">
            <column name="FREQUENCY"/>
        </property>                 
       
     <set name="posts" inverse="true" table="FORUM_MULTIMIDIA_POST_TAGS" >
        <key column="idTag" />
        <many-to-many column="idPost"
            class="org.sakaiproject.forummultimidia.model.Post"/>
    </set>       
                
       
   </class>
   
</hibernate-mapping>

Classes

public class Post implements Serializable{
   
   /**
    *
    */
   private static final long serialVersionUID = 1L;

   private Long idPost;
   
   private String title;
   
   private String message;
   
   private  String urlMidia;
   
   private Character typeMidia;
   
   private Date postDate;
   
   private  Long idPostFather;
   
   private String idSite;
   
   private String idUser;
   
   private Set tags = new HashSet();

   public Set getTags() {
      return tags;
      
   }

   public void setTags(Set tags) {
      this.tags = tags;
   }
   
   public void addTag(Tag tag){
      this.tags.add(tag);
   }

   public Long getIdPost() {
      return idPost;
   }

   public void setIdPost(Long idPost) {
      this.idPost = idPost;
   }

   public String getTitle() {
      return title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   public String getMessage() {
      return message;
   }

   public void setMessage(String message) {
      this.message = message;
   }

   public String getUrlMidia() {
      return urlMidia;
   }

   public void setUrlMidia(String urlMidia) {
      this.urlMidia = urlMidia;
   }

   public Character getTypeMidia() {
      return typeMidia;
   }

   public void setTypeMidia(Character typeMidia) {
      this.typeMidia = typeMidia;
   }

   public Date getPostDate() {
      return postDate;
   }

   public void setPostDate(Date postDate) {
      this.postDate = postDate;
   }

   public Long getIdPostFather() {
      return idPostFather;
   }

   public void setIdPostFather(Long idPostFather) {
      this.idPostFather = idPostFather;
   }

   public String getIdSite() {
      return idSite;
   }

   public void setIdSite(String idSite) {
      this.idSite = idSite;
   }

   public String getIdUser() {
      return idUser;
   }

   public void setIdUser(String idUser) {
      this.idUser = idUser;
   }
   
   public boolean equals(Object obj) {
      if (obj instanceof Post == false) {
         return false;
      }
      if (this == obj) {
         return true;
      }
      Post rhs = (Post) obj;
      return new EqualsBuilder()
         .append(this.getIdPost(), rhs.getIdPost())
         .isEquals();
    }

    public int hashCode() {
       return new HashCodeBuilder().append(getIdPost()).toHashCode();
    }


}
===
Tag

public class Tag implements Serializable{
   
   /**
    *
    */
   private static final long serialVersionUID = 1L;

   private Long idTag;
   
   private String name;
   
   private Long frequency;
   
   private String url;
   
   private Set posts = new HashSet();

   public Set getPosts() {
      return posts;
   }

   public void setPosts(Set posts) {
      this.posts = posts;
   }

   public Long getIdTag() {
      return idTag;
   }

   public void setIdTag(Long idTag) {
      this.idTag = idTag;
   }

   public String getName() {
      return name;
   }

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

   public Long getFrequency() {
      return frequency;
   }

   public void setFrequency(Long frequency) {
      this.frequency = frequency;
   }

   public String getUrl() {
      return url;
   }

   public void setUrl(String url) {
      this.url = url;
   }
   public boolean equals(Object obj) {
      if (obj instanceof Tag == false) {
         return false;
      }
      if (this == obj) {
         return true;
      }
      Tag rhs = (Tag) obj;
      return new EqualsBuilder()
         .append(this.getIdTag(), rhs.getIdTag())
         .isEquals();
    }

    public int hashCode() {
       return new HashCodeBuilder().append(getIdTag()).toHashCode();
    }


}


==
metodo
                       
                         postCreate.addTag(tag);
               postCreate.addTag(tag2);
         
                        Session session = getSessionFactory().openSession();
         Transaction transaction = session.beginTransaction();
         getHibernateTemplate().save(postCreate);
         transaction.commit();
         return true;


Please,somebody could help ,had lost two days trying any solution to this


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.