-->
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.  [ 6 posts ] 
Author Message
 Post subject: Please help me, @ManyToMany won't work
PostPosted: Mon Nov 27, 2006 7:05 am 
Newbie

Joined: Fri Nov 24, 2006 10:41 am
Posts: 6
Hibernate Annotation version: 3.2.0 CR2

Name and version of the database you are using: HSQLDB 1.8.0.1

Hello to everyone,

i am triyng to manage transitive persistence using Hibernate Annotations. In particular i have a Tag class and a Document class with @ManyToMany annotation:

Tag.java class
Code:
@Entity
public class Tag implements Serializable {
...
    private Set<Document> taggedDocuments;
...
    @ManyToMany(mappedBy="tags")
    public Set<Document> getTaggedDocuments() {
       return taggedDocuments;
    }
...
}



Document.java class
Code:
@Proxy(lazy=true)
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"hashkey","source_id"})})
@Indexed(index=Indexable.DOCUMENT_INDEX_DIR)
@NamedQueries(value={@NamedQuery(name="document.byIds", query="select d from Document d where d.id in (:ids)")})
public class Document implements Serializable {
...
    protected Set<Tag> tags;
...
    @ManyToMany(fetch=FetchType.LAZY, cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    public Set<Tag> getTags() {
       return tags;
    }
...
}


I have no problems with saving or finding, my problem is that when i delete a tagged document (that is a document associated to a tag, that is a document present within the "taggedDocuments" set of the tag) the document itself is still present in "taggedDocuments" set of the tag.

Why the reference to the deleted document isn't removed from the set? Where is my error?

Please help me :)

PS: sorry for my bad english


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 9:33 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
add CascadeType.REMOVE

This will remove the reference and delete it . if you were notusing annotations you would use cascade-all delete-orphan.

if delete is not there it will only set the reference_id to null and not physically remove the row.

hope this helps and if so please do rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 10:01 am 
Newbie

Joined: Fri Nov 24, 2006 10:41 am
Posts: 6
Quote:
add CascadeType.REMOVE

This will remove the reference and delete it . if you were notusing annotations you would use cascade-all delete-orphan.

if delete is not there it will only set the reference_id to null and not physically remove the row.



Hello gopalsaha,

many thanks for your reply, but unfortunately i have yet tried to add "CascadeType.REMOVE": with this addition the entire Tag entity associated to the Document will be removed, and this isn't the behaior that i want.
My goal is to update the reference to "taggedDocuments" in Tag entity (that is, remove from the set "taggedDocuments" in Tag entity the just removed document), and not remove the Tag entity.

Hope this help to clarify the problem, probably i was a bit obscure in first post.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 10:49 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
Could you please all the relevant java files and the code where you are trying to update reference and then i can try to emulate in my machine.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 11:16 am 
Newbie

Joined: Fri Nov 24, 2006 10:41 am
Posts: 6
Quote:
Could you please all the relevant java files and the code where you are trying to update reference and then i can try to emulate in my machine.


Ok, sorry for my omission.

The code where i try to update the reference is the following test:

TagPersistenceTest.java
Code:
public class TagPersistenceTest extends TestCase {
...
    public void testSaveTagAndTaggedDocument() {

      this.tag = new Tag();
      //save Tag
      this.persistenceManager.saveOrUpdate(this.tag);
            
                //create Document
      this.document = new Document();

                //set Document's tags
      Set<Tag> documentTags = new HashSet<Tag>();
      documentTags.add(this.tag);
      this.document.setTags(documentTags);

      //save document reference into Tag
      Set<Document> taggedDocuments = new HashSet<Document>();
      taggedDocuments.add(this.document);
      this.tag.setTaggedDocuments(taggedDocuments);
      //update tag
      this.persistenceManager.saveOrUpdate(this.tag);
      
      //save Document
      this.persistenceManager.saveOrUpdate(this.document);

                //some tests: this are OK
      List<Document> list = persistenceManager.findAll(Document.class);
      Document docFromDB=list.get(0);
      Set<Tag> tagsOfDocFromDB = docFromDB.getTags();
      Iterator iter = tagsOfDocFromDB.iterator();
      Tag documentTag=(Tag) iter.next();
      assertEquals("Tag returned should be equal to the Tag passed",this.tag,documentTag);
      assertEquals("Tag returned should have the same id of the Tag passed",this.tag.getId(),documentTag.getId());
      assertEquals("Tag returned should store only one document",1,documentTag.getTaggedDocuments().size());

                //trying to remove Document and some tests: also this tests are OK
      this.persistenceManager.remove(docFromDB);
      assertEquals("No one Document should be present in database", 0, this.persistenceManager.findAll(Document.class).size());
      assertEquals("After removing the document, the tag should still be present in database", 1, this.persistenceManager.findAll(Tag.class).size());

                //PROBLEMATIC TEST: this is NOT OK
      assertTrue("Tag should have no tagged documents",taggedDocumentsOfReturnedTag.isEmpty());

    }
...
}


The problem is the last test: as you can see Tag shouldn't store any tagged document, but at the end of the test the Tag's set of "taggedDocuments" still contains reference to Document.
This is my problem.

Thank you very much for your help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 4:41 am 
Newbie

Joined: Fri Nov 24, 2006 10:41 am
Posts: 6
Ok, problem solved: there was an error with the test class, an error with the transaction management

Many thanks to gopalsaha for the help


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