ObjectDB ObjectDB

Problem with @UniqueConstraint

#1

It seems there is a problem with @UniqueConstraint. Same annotations using hibernate works fine - an error will be thrown - duplicate name. ObjectDB - no exception will be thrown.

with best regards
Peter

import javax.persistence.*;
public final class Test2
{
    public static void main(String[] args)
    {
        int r = (int)(Math.random() * 1000000);
        String PATH = "c:\\temp\\test_" + r + ".odb";
        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory(PATH);
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        CI ci = new CI();
        ci.name = "abc";
        em.persist(ci);
        CI ci2 = new CI();
        ci2.name = "abc";   
        em.persist(ci2);     
        em.getTransaction().commit();
        em.close();
        System.out.println("OK: " + ci.id + " "  + ci2.id);
    }

    @Entity
    @javax.persistence.Table(name="CI",uniqueConstraints =
        { @UniqueConstraint(columnNames =  { "NAME" }) })
    public static final class CI {
        @Id @GeneratedValue long id;
        @Column(name="NAME", nullable=false)
        String name;
    }
}

 

edit
delete
#2

@UniqueConstraint is an ORM annotation. It references RDBMS columns rather than Java class fields. Therefore, it is silently ignored by ObjectDB, as specified on:

http://www.objectdb.com/api/java/jpa/UniqueConstraint

Until JPA defines Java object model level @Index / @Unique annotations (hopefully in next versions) - you will have to use JDO @Index / @Unique annotations:

http://www.objectdb.com/api/java/jdo/annotations/indexes

http://www.objectdb.com/java/jpa/entity/index

For example:

@Unique(members={"lastName","firstName"})

And for a single field simply @Unique on the field.

ObjectDB Support
edit
delete
#3

Thanks for the quick reply! Tongue out

edit
delete

Reply

To post on this website please sign in.