• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: ERXValidationException out of a Unique Constraint Violation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ERXValidationException out of a Unique Constraint Violation


  • Subject: Re: ERXValidationException out of a Unique Constraint Violation
  • From: Chuck Hill <email@hidden>
  • Date: Tue, 27 May 2008 11:05:46 -0700

The thing is, as I expect Paul has found out, is that this won't work reliably. Something else can add the value to the database between when you make this check and when the SQL transaction is committed.

Chuck

On May 27, 2008, at 11:01 AM, Zak Burke wrote:

On Tue, May 27, 2008 at 5:45 AM, Paul Stringer <email@hidden> wrote:
I'm following the PracticalWebObjects advice to handle unique values
validation at the database and then handle it in thrown
EOGeneralAdaptorExceptions. Wish I'd checked this before I wasted time
pre-checking for all this in validateKey() which was my first approach,
follow the advice don't do it! :)

[...]

Any better ways to approach this or am I basically on the right track. The
only thing I see as a potential problem is that the errors that come back
maybe database specific and so therefore the code is not db independant. I'm
using FrontBase right now if it makes any difference.

I wrote some simple "validateUniquenessOf..." methods that you can stash in a custom EO superclass. The content is pasted below; you can also just search for "uniquness" on www.wocode.com.

Cheers,

zak.


public class EOChildClass extends EOParentClass {

public void validateForSave()
{
// make sure entityKey value is unique
// corresponds to a DB constraint of "unique(column_name)"
validateUniquenessOf("entityKey");



// make sure dictionary pairs are unique
// corresponds to a DB constraint of "unique(column_name_1, column_name_2)"
NSMutableDictionary d = new NSMutableDictionary();
d.setObjectForKey(entityValue1, "entityKey1");
d.setObjectForKey(entityValue2, "entityKey2");


validateUniquenessOf(d);
}
}



public abstract class EOParentClass extends EOGenericRecord
{


/**
* Return true if this object has not yet been persisted, false otherwise.
* @return true if this object is new (unpersisted), false otherwise.
*/
public boolean isNew()
{
return editingContext().globalIDForObject(this).isTemporary();
}




/**
* Throw if a matching EO already exists.
*
* @param key name of the property on the EO-class to validate
*
* @throws NSValidation.ValidationException if an EO with the same
property value already exists
*/
public void validateUniquenessOf(String key)
{
try
{
EOEnterpriseObject item =
EOUtilities.objectMatchingKeyAndValue(editingContext(), entityName(),
key, this.valueForKey(key));
if (isNew())
throw new NSValidation.ValidationException("An item with this name
already exists. Pick a different name.");
else if (! item.equals(this))
throw new NSValidation.ValidationException("You can't rename this item
because an item with the new name already exists. Pick a different
name.");
}
// nothing to do if there are no matching objects
catch (EOObjectNotAvailableException e)
{
}

// bummmer. the item's properties are already non-unique.
catch (EOUtilities.MoreThanOneException e)
{
throw new NSValidation.ValidationException("An item with this name
already exists. Pick a different name.");
}
}



/**
* Throw if a matching EO already exists.
*
* @param values hash of property-value pairs on the EO-class to validate
*
* @throws NSValidation.ValidationException if an EO with the same
property-value pairs already exists
*/
public void validateUniquenessOf(NSDictionary values)
{
try
{
EOEnterpriseObject item =
EOUtilities.objectMatchingValues(editingContext(), entityName(),
values);
if (isNew())
throw new NSValidation.ValidationException("An item with this name
already exists. Pick a different name.");
else if (! item.equals(this))
throw new NSValidation.ValidationException("You can't rename this item
because an item with the new name already exists. Pick a different
name.");
}
// nothing to do if there are no matching objects; this is exactly what we want!
catch (EOObjectNotAvailableException e)
{
}


// bummmer. the item's properties are already non-unique.
catch (EOUtilities.MoreThanOneException e)
{
throw new NSValidation.ValidationException("An item with this name
already exists. Pick a different name.");
}
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


--

Practical WebObjects - for developers who want to increase their overall knowledge of WebObjects or who are trying to solve specific problems.
http://www.global-village.net/products/practical_webobjects






_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: ERXValidationException out of a Unique Constraint Violation
      • From: Timo Hoepfner <email@hidden>
References: 
 >ERXValidationException out of a Unique Constraint Violation (From: Paul Stringer <email@hidden>)
 >Re: ERXValidationException out of a Unique Constraint Violation (From: "Zak Burke" <email@hidden>)

  • Prev by Date: Re: ERXValidationException out of a Unique Constraint Violation
  • Next by Date: Install WO 5.3/WOLips on Ubuntu
  • Previous by thread: Re: ERXValidationException out of a Unique Constraint Violation
  • Next by thread: Re: ERXValidationException out of a Unique Constraint Violation
  • Index(es):
    • Date
    • Thread