ObjectDB ObjectDB

How to apply Unique constraints on embedded fields?

#1

How to apply Unique constraints on embedded fields?


@Entity
public class A
{
    private B b;   
}


@Embeddable
public class B 
{
    private C c;
}


@Embeddable
public class C 
{
    private HashSet<String> values;
}

 

how can i apply unique in values?

 

Thanks

 

edit
delete
#2

Uniqueness is enforced by maintaining an index.

You can try defining a multi part path index in class A. Something like:


@Entity

@Unique(members={"b.c.values"})

@Entity
public class A
{
    private B b;   
}

Please update whether this solution works.

ObjectDB Support
edit
delete
#3

ok thank you its working. but my requirement is something like this

@Entity
@Unique(members =
{
    "b1.c.values",
    "b2.c.values"
})
public class A
{

    private B1 b1;

    private B2 b2;

    public A()
    {
    }
}

But am getting below exception

Caused by: com.objectdb.o.UserException: Invalid index path 'b1.c.values / b2.c.values' in type org.test.entities.A
    at com.objectdb.o.MSG.a(MSG.java:22)
    at com.objectdb.o.IXN.i(IXN.java:289)
    at com.objectdb.o.IXN.<init>(IXN.java:89)
    at com.objectdb.o.UTY.A(UTY.java:496)
    at com.objectdb.o.TYM.Z(TYM.java:349)
    at com.objectdb.o.TYM.<init>(TYM.java:128)
    at com.objectdb.o.MST.aA(MST.java:170)
    at com.objectdb.o.SHN.X(SHN.java:221)
    at com.objectdb.o.SHN.Q(SHN.java:194)
    at com.objectdb.o.SHN.w(SHN.java:77)
    at com.objectdb.o.HND.run(HND.java:72)
    at java.lang.Thread.run(Unknown Source)
edit
delete
#4

These are my sample classes.


@Entity

@Unique(members =
{
    "b1.c.values",
    "b2.c.values"
})
public class A extends code.entities.AEntity
{

    private B1 b1;

    private B2 b2;

    public A()
    {
    }

    public B1 getB1()
    {
        return b1;
    }

    public void setB1(B1 b1)
    {
        this.b1 = b1;
    }

    public B2 getB2()
    {
        return b2;
    }

    public void setB2(B2 b2)
    {
        this.b2 = b2;
    }

}



@Embeddable
public class B1
{

    private C c;

    public B1()
    {
    }

    public C getC()
    {
        return c;
    }

    public void setC(C c)
    {
        this.c = c;
    }

    @Embeddable
    public static class C
    {

        private HashSet<String> values;

        public C()
        {
            this.values = new HashSet<>();
        }

        public HashSet<String> getValues()
        {
            return values;
        }

        public void setValues(HashSet<String> values)
        {
            this.values = values;
        }

    }

}


@Embeddable
public class B2
{

    private C c;

    public B2()
    {
    }

    public C getC()
    {
        return c;
    }

    public void setC(C c)
    {
        this.c = c;
    }

    @Embeddable
    public static class C
    {

        private HashSet<String> values;

        public C()
        {
            this.values = new HashSet<>();
        }

        public HashSet<String> getValues()
        {
            return values;
        }

        public void setValues(HashSet<String> values)
        {
            this.values = values;
        }

    }
}

 

edit
delete
#5

In that case uniqueness will have to be enforced by your application, as you cannot define such indexes. Alternatively, consider changing you model, including possibly duplicating data, in order to have the value that you need to be unique available in one indexable field.

ObjectDB Support
edit
delete

Reply

To post on this website please sign in.