Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-07-2008, 09:32 AM
Member
 
Join Date: Nov 2008
Posts: 5
dennisome is on a distinguished road
check record dependency before deleting
Hi all!

Please help me on this...

I would just like to create a method that checks if a record or records are being used in another table(s). We're using EJB3, JBoss Framework and MySQL in the development.

Currently, our implementation is using a try catch. When trying to delete the record returns an exception, which is commonly about the record is being used in another table, it immediately stops the current thread and prompts the user that the record is being used in another table.

Thanks,
Dennis
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-07-2008, 09:40 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
So do you looking to use a SQL query to find this?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-07-2008, 10:09 AM
Member
 
Join Date: Nov 2008
Posts: 5
dennisome is on a distinguished road
if you can give me, please, why not.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-07-2008, 10:21 AM
Senior Member
 
Join Date: Jun 2008
Posts: 551
masijade is on a distinguished road
Setup your foreign keys properly.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-07-2008, 10:39 AM
Member
 
Join Date: Nov 2008
Posts: 5
dennisome is on a distinguished road
the foreign keys was set to 'restrict' on delete. what should it be?...
'no action'...?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-07-2008, 10:55 AM
Senior Member
 
Join Date: Jun 2008
Posts: 551
masijade is on a distinguished road
No, that is correct. That means you should get an exception if you attempt to delete a record that others are dependent upon. simply catch the Exception and check that it is that exception that was raised, rather than informing the user immediately. If you want to inform the user, simply keep a list of all the "records" that caused the exception and show it to the user at the end.

Edit: That will take less time than a possibly very large and time consuming join query with it's round trip to check the results.

Last edited by masijade : 11-07-2008 at 10:58 AM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-07-2008, 11:00 AM
Senior Member
 
Join Date: Jun 2008
Posts: 551
masijade is on a distinguished road
And I know the try catch is what you already had, I was stating to you to kepp doing it that way, as that is the proper way. A query beforehand is a (possibly very) large amount of unneccesary overhead.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-07-2008, 11:59 AM
Member
 
Join Date: Nov 2008
Posts: 5
dennisome is on a distinguished road
That is good to know, masijade. But you see, the exception error also results an error message, "Transaction has failed," that goes with the message that is normally the only message I want to show up when deletion fails. I'm using richfaces message (<rich:message>) you know.
I believe this will be a JBoss richfaces question, but if you have any idea, please let me know... Is there a way to remove that exception message?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-07-2008, 12:09 PM
Senior Member
 
Join Date: Jun 2008
Posts: 551
masijade is on a distinguished road
Well, you are already catching the exceptions, right, then adding them to this message (however you may be adding them). Well, use getErrorCode() from the SQLException and determine whether it was that exception that was thrown before adding the exception to the message.

If you are not using try catch inside the method doing the delete (but rather just throwing the exception and letting a generic routine handle it) then change that method from:
Code:
public Whatever whateverYouCallIt(Whatever whatever) throws SQLException { // whatever }
to
Code:
public Whatever whateverYouCallIt(Whatever whatever) throws SQLException { try { // whatever } catch (SQLException sqle) { if (sqle.getErrorCode() != whateverTheErrorCodeForTheForeignKeyConstraintIs) { throw sqle; } } }
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-07-2008, 01:57 PM
Member
 
Join Date: Nov 2008
Posts: 5
dennisome is on a distinguished road
Thanks, masijade! That really helps a lot!...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Maven Dependency Overview 1.1 Java Tip Java Announcements 0 07-02-2008 09:13 PM
Maven Dependency Overview 1.0 Java Tip Java Announcements 0 04-15-2008 08:52 PM
What are dependency injection and its advantages Java Tip Java Tips 0 03-29-2008 02:36 PM
What are dependency injection and its advantages JavaBean Java Tips 0 09-26-2007 10:27 PM
Dependency Analyzer 1.0.3-rc0 levent Java Announcements 0 07-30-2007 06:34 PM


All times are GMT +3. The time now is 02:27 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org