Rudimentary Joe
Rudimentary Joe

Reputation: 159

@NotNull ignored on class

I have an entity:

@Entity
public class MyEntity {

  @Id
  private String Id;

  @NotNull
  @Column(nullable = false)
  private Integer size;

  public void setSize(Integer size) { this.size = size; }
  public Integer getSize() { return this.size; }

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

A repository:

@Repository
public class MyEntityDAO {

  @PersistenceContext
  private EntityManager em;

  public void create(MyEntity myEntity) {
    em.persist(myEntity);
  }
}

A test which should throw an exception:

@RunWith(SpringRunner.class)
@Transactional
@SpringBootTest
public class MyEntityDAOTest {

  @Inject 
  private MyEntityDAO myEntityDAO;

  @Test(expected = ConstraintViolationException.class)
  public void nullSizeNotAllowedTest() {
    MyEntity myEntity = new MyEntity();
    myEntity.setSize(null);
    myEntity.setId("entity_id");
    myEntityDAO.create(myEntity);
  }
}

However the Test fails. The Entity does not throw the required exception. The annotation works OK for String, but not for Integer.

The auto-generated table for MyEntity:

FIELD   TYPE            NULL    KEY     DEFAULT  
ID      VARCHAR(255)    NO      PRI     NULL
SIZE    INTEGER(10)     NO              NULL

UPDATE

Everything works when I specify the @GeneratedValue(strategy = ...) to the Id field. However, I want to be able to assign my own value to Primary Key field.

UPDATE 2

Updated Entity (Repository was updated to reflect changes as well):

@Entity
public class MyEntity {

  @Id
  private Long Id;

  @NaturalId
  private String oldId;

  @NotNull
  @Column(nullable = false)
  private Integer size;

  /* getters and setters */
}

The auto-generated table for MyEntity:

FIELD   TYPE            NULL    KEY     DEFAULT  
ID      BIGINT(19)      NO      PRI     NULL
OLD_ID  VARCHAR(255)    YES     UNI     NULL
SIZE    INTEGER(10)     NO              NULL

Upvotes: 2

Views: 1185

Answers (2)

Patrick
Patrick

Reputation: 2195

You have to call entityManager.flush() after calling entityManager.save().

Otherwise, the exception will not be thrown and will not be picked up in your test.

Here is a full working example:

MyEntity:

@Entity
public class MyEntity {

    @Id
    private String id;

    @Column(nullable = false)
    @NotNull
    private Integer size;

    public MyEntity(){}

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
         this.size = size;
    }
}

MyEntityDAO:

@Repository
public class MyEntityDAO {

     @PersistenceContext
     private EntityManager entityManager;

     public void save(MyEntity myEntity){
          entityManager.persist(myEntity);
          entityManager.flush();
     }
}

MyEntityDAOTest:

@RunWith(SpringRunner.class)
@Transactional
@SpringBootTest
public class MyEntityDAOTest {

    @Autowired
    private MyEntityDAO myEntityDAO;

    @Test(expected = ConstraintViolationException.class)
    public void nullSizeNotAllowedTest() throws Exception {
        MyEntity myEntity = new MyEntity();
        myEntity.setId("testId");
        myEntity.setSize(null);
        myEntityDAO.save(myEntity);
    }
}

Upvotes: 1

Hardik Shah
Hardik Shah

Reputation: 4200

Try this, Working as expected in my case.

@Column(name = "size", nullable = false)
@NotNull(message= "size may not be empty.")
private Integer size;

Upvotes: 0

Related Questions