Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 2e9c43658e374d290a2de15d25134ac8 > files > 467

db4o-doc-8.0-1.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Advanced Features" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem">
    <head><title>Unique Constraints</title>
        <script type="text/javascript">/* <![CDATA[ */
window.onload = function(){
	var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf';
	ZeroClipboard.setMoviePath(pathToFlash);
			
	function bindToClipBord(element,content){
		var clip = new ZeroClipboard.Client();
		clip.setText(content);
		clip.glue(element);
	};
		
	if(location.protocol==='file:'){
		$('.copylink-marker').remove();
	} else{
			$('.copylink-marker').each(function(){
				var text = $(this).parent().parent().children('.prettyprint').html();
				$(this).hover(function(){
					bindToClipBord(this,text);
				},
				function(){});
			});	
	}		
	prettyPrint();	
};
                /* ]]> */</script>
        <link href="../SkinSupport/MadCap.css" rel="stylesheet" />
        <link href="../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" />
        <script src="../Resources/Code/prettify.js">
        </script>
        <script src="../Resources/Code/lang-vb.js">
        </script>
        <script src="../Resources/Code/jquery.min.js">
        </script>
        <script src="../Resources/Code/ZeroClipboard.js">
        </script>
        <script src="../SkinSupport/MadCapAll.js" type="text/javascript">
        </script>
    </head>
    <body>
        <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../index_CSH.html#advanced_topics/unique_constraints.htm" style="">Open topic with navigation</a>
        </p>
        <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../advanced_topics.htm">Advanced Features</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Unique Constraints</span>
        </div>
        <p>
            <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">');
				document.write("Direct Link");
			document.write('</a>');/*]]>*/</script>
        </p>
        <p>
        </p>
        <h1><a name="kanchor69"></a><a name="kanchor70"></a>Unique Constraints</h1>
        <p>Unique constraints allow a user to define a field
to be unique across all the objects of a particular class stored to db4o. This
means that you cannot save an object where a previously committed object
has the same field value for fields marked as unique. 
   
A Unique Constraint is checked at commit-time and a constraint violation will
cause a UniqueFieldValueConstraintViolationException to be thrown. This
functionality is based on <a href="callbacks/commit-time_callbacks.htm">Commit-Time Callbacks</a> feature. </p>
        <h2>How To Use Unique Constraints</h2>
        <p>First you need to add an index on the field which should be unique. After that you add the UniqueFieldValueConstraint to the configuration for the unique field.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">configuration.Common.ObjectClass(typeof (UniqueId)).ObjectField("id").Indexed(true);
configuration.Common.Add(new UniqueFieldValueConstraint(typeof (UniqueId), "id"));</pre>
            <div class="codesnippet-meta">UniqueConstrainExample.cs: Add the index the field and then the unique constrain
			<div class="codedownload"><a href="../CodeExamples/features/uniqueconstrain/Example-features-uniqueconstrain-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">configuration.Common.ObjectClass(GetType(UniqueId)).ObjectField("id").Indexed(True)
configuration.Common.Add(New UniqueFieldValueConstraint(GetType(UniqueId), "id"))</pre>
            <div class="codesnippet-meta">UniqueConstrainExample.vb: Add the index the field and then the unique constrain
			<div class="codedownload"><a href="../CodeExamples/features/uniqueconstrain/Example-features-uniqueconstrain-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <p>After that, the unique constrain is applied. When you commit a transaction the uniqueness of the field is checked. If there's any violation, the UniqueFieldValueConstraintViolationException will be thrown.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">container.Store(new UniqueId(42));
container.Store(new UniqueId(42));
try
{
    container.Commit();
}
catch (UniqueFieldValueConstraintViolationException e)
{
    Console.Out.WriteLine(e.StackTrace);
}</pre>
            <div class="codesnippet-meta">UniqueConstrainExample.cs: Violating the constrain throws an exception
			<div class="codedownload"><a href="../CodeExamples/features/uniqueconstrain/Example-features-uniqueconstrain-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">container.Store(New UniqueId(42))
container.Store(New UniqueId(42))
Try
    container.Commit()
Catch e As UniqueFieldValueConstraintViolationException
    Console.Out.WriteLine(e.StackTrace)
End Try</pre>
            <div class="codesnippet-meta">UniqueConstrainExample.vb: Violating the constrain throws an exception
			<div class="codedownload"><a href="../CodeExamples/features/uniqueconstrain/Example-features-uniqueconstrain-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <script type="text/javascript" src="../SkinSupport/MadCapBodyEnd.js">
        </script>
    </body>
</html>