db4o Developer Community

db4o open source object database, native to Java and .NET
Welcome to db4o Developer Community Sign in | Join
in Search
More Search Options

Product News from the Core Team

This blog features product news right from the core developer team, once new features and functions get checked into Subversion, available as Continuous Build every 2 hours.

Unique fields constraints

Unique Constraints allow a user to define a field to be unique across a particular Class. 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. Multiple constraints can be defined on the same class if required. 

How to Use Unique Constraints 

// First, you should always index a field you wish to be unique:
config.objectClass(Item.class).objectField("_str").indexed(true);
// Second, add the constraint:
config.add(new UniqueFieldValueConstraint(Item.class, "_str"));
// open objectContainer
try {
  // do some work and save some objects
  objectContainer.commit();
} catch(UniqueFieldValueConstraintViolationException exc) {
  objectContainer.rollback();
}

Unique Constraints are based on commit-time callbacks, also a new feature in version 6.2, which allow users to listen for commit-time events. The Unique Constraints use the "on committing" event to verify that the fields are unique before the final commit is actually executed.

Unique Constraints are available as of db4o version 6.2.

Published Tuesday, March 20, 2007 10:06 AM by Patrick Roemer


Comments

 

Product News from the Core Team said:

Which db4o feature provides you with the ability to ensure that specific constraints are not violated

March 20, 2007 4:24 AM
 

jiangshachina said:

> config.add(new UniqueFieldValueConstraint(Item.class, "_str"));

I shall set the constraint very time before commit, or just once?

March 21, 2007 4:11 AM
 

db4o Newsletter said:

Welcome to the April newsletter! Version 6.1 Stable and Ready for Shipment 6.2 Dev with Unique Constraints,

March 22, 2007 2:33 AM
 

db4o in Chinese said:

事件 2nd Global db4o User Conference 2007 (dUC) July, 9 and 10 - Fort Mason - San Francisco, CA, United

March 25, 2007 3:01 PM
 

Outsignia said:

Any idea when version 6.2 will ship, as I really need this feature for my University project, though I still have 5 weeks till the deadline.

Would it be advisable to use the development version, I'm going to try it out, hoping that it will be stable enough.

March 26, 2007 7:46 PM
 

db4o in Japanese said:

目次 バージョン6.1がStableになり、出荷可能レベルに バージョン6.2がDev版でリリース(ユニーク制約、コミット時コールバック) Spring、Castle、RSSOwl、db4oコミュニティプロジェクト

April 3, 2007 3:47 AM
 

huangyifu said:

It seam unique constraint function NOT implement in version 6.3?

-------------------------------------------------------------------------------------------------

      ObjectContainer db = Db4o.openFile("stock.yap");

       db.ext().configure().objectClass(Stock.class).objectField("code")

               .indexed(true);

       db.ext().configure().add(

               new UniqueFieldValueConstraint(Stock.class, "code"));

...

       db.set(stock);// I can store 2 stock object with same code property!!!

July 1, 2007 9:42 AM
 

huangyifu said:

package scode;

import java.io.File;

import com.db4o.Db4o;

import com.db4o.ObjectContainer;

import com.db4o.constraints.UniqueFieldValueConstraint;

import com.db4o.constraints.UniqueFieldValueConstraintViolationException;

public class TestMain {

   static class Item {

       public String id;

       @Override

       public String toString() {

           return "Item:" + id;

       }

   }

   public static void main(String[] args) {

       File f = new File("test.yap");

       f.delete();

       ObjectContainer db = Db4o.openFile("test.yap");

       db.ext().configure().objectClass(Item.class).objectField("id").indexed(

               true);

       db.ext().configure().add(

               new UniqueFieldValueConstraint(Item.class, "id"));

       Item item = new Item();

       item.id = "1";

       db.set(item);

       try {

           db.commit();

           System.out.println(item);

       } catch (UniqueFieldValueConstraintViolationException exc) {

           db.rollback();

           System.out.println("ERROR: " + item);

       }

       Item item1 = new Item();

       item1.id = "1";

       db.set(item1);

       try {

           db.commit();

           System.out.println(item1);

       } catch (UniqueFieldValueConstraintViolationException exc) {

           db.rollback();

           System.out.println("ERROR: " + item);

       }

       db.close();

   }

}

July 1, 2007 11:09 AM
Anonymous comments are disabled