我有一个Spring实体,其中有一个用@NotNull注释的字段来自javax.validation.constraints
@Entity
public abstract class IdentifiableNamedEntity {
@NotNull
@Column(unique = true)
private String name;
}
问题是如果为name字段设置null值,它将存储在数据库中.但是,如果我按照以下方式更改类,则会引发我希望收到的异常:
@Entity
public abstract class IdentifiableNamedEntity {
@Column(unique = true, nullable=false)
private String name;
}
有没有办法可以避免指定nullable = false,但让@NotNull按照我的意愿行事?是否存在依赖于标准Java注释的nullable = false的替代方法,例如:一些Hibernate配置?
这是我的弹簧配置:
的ApplicationContext
的applicationContext持久性
destroy-method="close">
transaction-manager="transactionManager" />
id="transactionManager">
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
的applicationContext-JPA
由于我使用的是存储库,我还报告了相应的实体存储库:
@Repository
public interface IdentifiableNamedEntityRepository extends JpaSpecificationExecutor, JpaRepository {
}