-->
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.  [ 10 posts ] 
Author Message
 Post subject: Parent delete child: really stuck here
PostPosted: Sat Nov 19, 2005 1:38 pm 
Newbie

Joined: Sat Nov 19, 2005 1:18 pm
Posts: 5
I have a pool which contains poolclients. I want to do the same thing as in the hibernate manual 22.3 (last case).

I use this code to delete a poolclient:

Code:
pool.removePoolClient(pc);      
session.flush();


I always get a constraint error for the poolclient however (which should be deleted)

org.hibernate.PropertyValueException: not-null property references a null or transient value: epaper.server.model.PoolClient.pool

These are my settings:

pool.hbm

Code:
<set name="poolClients" inverse="true" cascade="all-delete-orphan">
   <key column="POOL_ID_REF"/>
   <one-to-many class="PoolClient"/>
</set>


poolclient.hbm

Code:
<many-to-one name="pool" column="POOL_ID_REF" not-null="true"/>


Pool.java

Code:
public void removePoolClient(PoolClient pc) {
   getPoolClients().remove(pc);
   pc.setPool(null);
}


I suspect that getPoolClients().remove(pc) doesn't work. If I put getPoolClients.contains(pc) in the removePoolClient method, it always answers false, even if the pool contains a poolclient with the same id...

Here is my equals method in PoolClient:

Code:
   public boolean equals(Object obj) {
      if (null == obj)
         return false;
      if (!(obj instanceof epaper.server.model.PoolClient))
         return false;
      else {
         epaper.server.model.PoolClient client = (epaper.server.model.PoolClient) obj;
         if (null == this.getId() || null == client.getId())
            return false;
         else
            return (this.getId().equals(client.getId()));
      }
   }


Can someone help me cause I'm really stuck here...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 2:17 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
pc.setPool(null);


What's that supposed to do?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 2:48 pm 
Newbie

Joined: Sat Nov 19, 2005 1:18 pm
Posts: 5
There is a bidirectional link between pool en poolclient.

pc.setPool(null) sets the reference from the poolClient to the pool to null, because the poolClient doens't belongs to the pool anymore.

I expected to remove the poolClients with null reference with "all-delete-orphan"


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 2:51 pm 
Newbie

Joined: Sat Nov 19, 2005 1:18 pm
Posts: 5
I just wrote this test in the removePoolClient method. The output is weird...

Code:
public void removePoolClient(PoolClient pc) {
      System.out.println("Method removePoolClient, remove poolclient with id :" +pc.getId());
      System.out.println("PoolClients in this pool: ");
      Iterator it = getPoolClients().iterator();
      while(it.hasNext()) {
         PoolClient poolCl = (PoolClient) it.next();      
         System.out.println(poolCl);
      }
      System.out.println("Output of getPoolClients().contains(pc) :");
      if (getPoolClients().contains(pc)) {
         System.out.println("Pool contains pc");
      }
      else {
         System.out.println("Pool does not contain pc");
      }

      getPoolClients().remove(pc);
      pc.setPool(null);
   }


Output:


Method removePoolClient, remove poolclient with id :4
PoolClients in this pool:
PoolClient met id : 2
PoolClient met id : 3
PoolClient met id : 4
Output of getPoolClients().contains(pc) :
Pool does not contain pc


This would mean getPoolClients.remove(pc) does not remove the poolclient from the set no?
I don't see any errors in the equals function...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 2:57 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Just remove the set(null) thing and it will work.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 3:10 pm 
Newbie

Joined: Sat Nov 19, 2005 1:18 pm
Posts: 5
I tried this and the error is gone now, but it just doesn't remove the poolclient from the database.

The removePoolClient(pc) function just doens't seem to remove the poolclient from the set... (see the test output above)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 4:29 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://www.hibernate.org/ForumMailingli ... AskForHelp

First item in "When posting".


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 4:55 pm 
Newbie

Joined: Sat Nov 19, 2005 1:18 pm
Posts: 5
What do you mean by that? I gave you the information that seamed necessary to me... What else you need?[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 8:52 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
It's not me, I'm done here. But somebody else might take a round at guessing. To make it easier you should not ignore but provide the information you have been asked about before:

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 7:44 am 
Beginner
Beginner

Joined: Fri Oct 28, 2005 10:46 am
Posts: 37
It looks like you're using the database primary key in the equals method, and that's something to be avoided if possible. See this article:
http://www.hibernate.org/109.html

And lots of other resources online. Have you checked to see why exactly equals is failing? That's the problem you need to solve here. Christian's advice has taken care of your other problem. There is one logical problem that I see. Your check for null will return false if either or both objects have a null id. What if both are null? Are they equal? Are they just two transient entities?


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