failure to enforce NOT NULL for java.lang.String

#1

my test cases are indicating that @Basic(optional=false) and @Persistent(nullValue=NullValue.EXCEPTION) are not enforced in 2.3.7_08 or that i have failed to understand how to annotate. example:

@Entity
public class Request
{
  public Request() { }

  @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Long id;

  @Unique
  @Persistent(nullValue=NullValue.EXCEPTION)
  private String username;

  public Long getId() { return id; }
  public void setId(Long id) { this.id = id; }

  public String getUsername() { return username; }
  public void setUsername(String username) { this.username = username; }
}

public class RegressionTest
{
  /**
   */
  @Test
  public void regressionTest()
  {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("target/test.odb;drop");
    EntityManager em = emf.createEntityManager();
    EntityTransaction tr = em.getTransaction();

    Request r = new Request();
    r.setUsername(null);

    //try {
      tr.begin();
      em.persist(r);
      tr.commit();
    //}
    //catch (...) { }

    em.close();
    emf.close();
  }
}

i am able to run `mvn clean && mvn test` with success. the DB explorer shows the Request object in the database with username=null. the target/ directory is wiped out by `mvn clean` so i am certain a schema change is taking effect. running `mvn test` twice in a row without a clean between them does successfully violate the @Unique constraint:

[ObjectDB 2.3.7] javax.persistence.RollbackException

Failed to commit transaction: Unique constraint (org.wroth.ws.signup.Request[username]) failed: Attempt to reuse an existing value (null) (error 613)
        at com.objectdb.jpa.EMImpl.commit(EMImpl.java:279)
#2

Thank you for this report and for the test. Here is the same test in one top level class format and after omission of irrelevant elements (please use this format in your posts when possible):

import javax.jdo.annotations.*;
import javax.persistence.*;


public class T744 {

    public static void main(String [] args){
        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory(
                "objectdb:test.tmp;drop");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        em.persist(new Request());
        em.getTransaction().commit();

        em.close();
        emf.close();
    }

    @Entity
    public static class Request {

        public Request() {
        }

        @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Long id;

        @Unique
        //@Persistent(nullValue=NullValue.EXCEPTION)
        @Basic(optional=false)
        private String username;
       
        //@Basic(optional=false)
        //@Persistent(nullValue=NullValue.EXCEPTION)
        //private Request otherRequest;

        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }

        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
    }
}

NOT NULL constraints worked well for relationships but not for basic reference types such as String.

Build 2.3.7_14 fixes this issue.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

great, i will upgrade ASAP. sorry about the formatting; i just haven't figured that out yet.


Post Reply

To post a reply and/or subscribe to update notifications - please login