Results 1 to 10 of 10
- 11-07-2008, 07:32 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
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
- 11-07-2008, 07:40 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So do you looking to use a SQL query to find this?
- 11-07-2008, 08:09 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
if you can give me, please, why not.
- 11-07-2008, 08:21 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Setup your foreign keys properly.
- 11-07-2008, 08:39 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
the foreign keys was set to 'restrict' on delete. what should it be?...
'no action'...?
- 11-07-2008, 08:55 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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 08:58 AM.
- 11-07-2008, 09:00 AM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 11-07-2008, 09:59 AM #8
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
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?
- 11-07-2008, 10:09 AM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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:
toJava Code:public Whatever whateverYouCallIt(Whatever whatever) throws SQLException { // whatever }
Java Code:public Whatever whateverYouCallIt(Whatever whatever) throws SQLException { try { // whatever } catch (SQLException sqle) { if (sqle.getErrorCode() != whateverTheErrorCodeForTheForeignKeyConstraintIs) { throw sqle; } } }
- 11-07-2008, 11:57 AM #10
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Maven Dependency Overview 1.1
By Java Tip in forum Java SoftwareReplies: 0Last Post: 07-02-2008, 07:13 PM -
Maven Dependency Overview 1.0
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-15-2008, 06:52 PM -
What are dependency injection and its advantages
By Java Tip in forum Java TipReplies: 0Last Post: 03-29-2008, 12:36 PM -
What are dependency injection and its advantages
By JavaBean in forum Java TipReplies: 0Last Post: 09-26-2007, 08:27 PM -
Dependency Analyzer 1.0.3-rc0
By levent in forum Java SoftwareReplies: 0Last Post: 07-30-2007, 04:34 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks