-->
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: How to check the reference count for an entity
PostPosted: Fri May 19, 2006 9:44 am 
Newbie

Joined: Fri May 19, 2006 9:07 am
Posts: 3
Hi,

I have an entity A that is referenced by several entity types - lets say B, C and D. I want to enable or disable the delete operation for A if it is referenced by other entities. Is there a light-weight way to do it? I mean, is there a way to know the reference count for entity A without executing selects on all the entities that could reference it? I suppose that the database system(s) keep a reference count number for every foreign key relationship - if I can get this number somehow in Hibernate that will do the job...

Another question in the same line is how do I prevent constraint violation exceptions from being logged? Say if I entity A is referenced by entity B when the user tries to delete A, Hibernate will log a constraint violation exception. Is there a way to prevent it, without blocking other kinds of exceptions from being logged?

Thanks,
Yuri


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 10:43 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I dont know if this is what you want

Code:
EntityA aObject = (EntityA) session.get( EntityA.class, entityAPrimKey );

if ( ( aObject.getEntityB() == null ) &&
     ( aObject.getEntityC() == null ) &&
     ( aObject.getEntityD() == null ) ) {
   // delete only when EntityB, EntityC, EntityD are not referencing
   // this EntityA
   // i.e. entityB, entityC, entityD have collection of entityA
   // which doesnt have any reference to this element "aObject"
   session.delete( aObject );
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 11:42 am 
Newbie

Joined: Fri May 19, 2006 9:07 am
Posts: 3
I meant the opposite - entity A is referenced by B, C etc., which means that you don't have A.getB(), you have B.getA().

I can have A.getBCount(), which is something like:

select count(b) from B b where b.a.id = :a_id

that returns the number of B's that reference A, but I was wondering if there is a way to check whether A is referenced by any entity (B, C, etc.) without making a select query for every entity.


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.