422 words

failure to enforce NOT NULL for java.lang.String

#1
2012-04-06 12:53

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)
jon
jon's picture
Joined on 2012-04-05
User Post #2
#2
2012-04-06 15:35

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 F389 {
 
    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)
support
support's picture
Joined on 2010-05-03
User Post #1,002
#3
2012-04-08 21:02

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

jon
jon's picture
Joined on 2012-04-05
User Post #4

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel