tags:

views:

20

answers:

2

Hi all,

I would like to have unique constraints operate on more than 1 field for several entities. How can I achieve this with db4o?

Thanks,

Walter

A: 

Sounds like a composite key. Can you create a new class that contains the key-constituting fields, and use a member variable with that type in place of the key-constituting fields in your original class?

i.e. where you had

class Foo {
   String given_name;
   String family_name;
}

instead use

class Name {
   String given_name;
   String family_name;
}

class Foo {
   Name name;
}

and make Foo.name the unique field?

Ladlestein
Ah, I was thinking that, but completing that thought escaped me. That'll work, I was hoping to use an annotation spanning multiple fields.
A: 

Hi

Currently db4o doesn't support the UniqueConstrain on multiple fields. You can set unique-constrains only field by field, but not combine them.

@Ladlestein Well the intention is good, but it doesn't work. db4o manageds objects by it referencial identity. When you apply the unique-constrain on the Foo.name, you ensure that the reference is unique. So no other object can have the same reference to a name object. But you're not interested that the reference is unique, but you want to have a unique content of the names.

Gamlor
I didn't get to test this yet, but thanks for your expertise.