Null Version fields in JPA?

有些话、适合烂在心里 提交于 2019-12-23 18:28:30

问题


Nothing in http://docs.oracle.com/javaee/6/api/javax/persistence/Version.html says it cannot be null.

After running

import javax.validation.Validation;
import javax.validation.Validator;

import org.junit.Test;

import static org.junit.Assert.assertFalse;

public class Test
{
    @Test
    public void test()
    {
        //Using Hibernate Validator 4.1.0.Final...
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        assertFalse(validator.validate(new Foo()).isEmpty()); //why does this fail??
    }
}

where Foo.class is simply

import javax.persistence.Entity;
import javax.persistence.Version;

@Entity 
public class Foo
{
    @Version
    private final Integer version = null;
}

the test fails and no @Version validation errors are reported.

So does this mean @Version-annotated fields can be null?


回答1:


@Version just defines that a particular field is used for optimistic locking. It does not affect the column in DB itself nor the validation. So if you want to enforce not null values you have to use other annotations.

If you leave the version field as null you will get an exception while trying to perform update.



来源:https://stackoverflow.com/questions/16183013/null-version-fields-in-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!