简体   繁体   English

Javax 约束:如果字段“类型”为 XXX,则应用 @NotNull 注释

[英]Javax Constraints: if field 'Type' is XXX then apply @NotNull annotation

I have a Spring Boot app with Postgres that has the following entity:我有一个带有 Postgres 的 Spring Boot 应用程序,它具有以下实体:

 @Entity
 @Table(name = "message")
 public class Message {

  public enum MessageType {
    TEXT,
    LOCATION,
    IMAGE,
    REPLY,
    LIST,
    BUTTON
  }

  @Id
  @GeneratedValue(generator = "uuid")
  @GenericGenerator(name = "uuid", strategy = "uuid2")
  private String id;

  @Column(nullable = false)
  private MessageType type;

  private String text;

  @Embedded
  private LocationRequest location;

  @Embedded
  private ImageRequest image;

  @Embedded
  private ContextRequest reply;

What I need to do is apply some given constraints depending on the value of the MessageType.我需要做的是根据 MessageType 的值应用一些给定的约束。 For Example, if type is equal to " LOCATION ", then I want to make sure all the fields of LocationRequest are required (ie not blank) Similarly, if instead the type is Image, I want that every field of ImageRequest is required.例如,如果type等于“ LOCATION ”,那么我想确保 LocationRequest 的所有字段都是必需的(即不是空白)。类似地,如果类型是 Image,我希望 ImageRequest 的每个字段都是必需的。

Can this be achieved with Javax Constraints?这可以通过 Javax 约束来实现吗? If not, are there other ways to achieve it, perhaps using Hibernate?如果没有,是否有其他方法可以实现它,也许使用 Hibernate?

Since my colleagues may add a record to the DB directly with an INSERT, I need those constraints to be generated for the table and not for my app (I'm using Hibernate schema autogeneration feature, ddl-auto=true ).由于我的同事可能会使用 INSERT 直接向数据库添加记录,因此我需要为表而不是为我的应用程序生成这些约束(我正在使用 Hibernate 模式自动生成功能ddl-auto=true )。

You can do that with check constraints, although it is a bit cumbersome.您可以使用检查约束来做到这一点,尽管它有点麻烦。 Something like this:像这样的东西:

@Entity
@Table(name = "message")
@org.hibernate.annotations.Check("case type when 'LOCATION' then location_col1 is not null and locatoin_col2 is not null when 'IMAGE' then image_col1 is not null else true end")
public class Message {
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javax @NotNull 注释用法 - Javax @NotNull annotation usage @NotNull javax annotation vs Intellij @NotNull注释 - @NotNull javax annotation vs Intellij @NotNull annotation Java注解处理器:javax.validation.constraints.Past注解改变了字段类型 - Java annotation processor: javax.validation.constraints.Past annotation changed field type @NotNull类型注释-Solrj - @NotNull type annotation - Solrj 对象被标记为“ javax.validation.constraints.NotNull”,但未在此构造方法中初始化 - Object is marked “javax.validation.constraints.NotNull” but is not initialized in this constructor @Notnull具有多个字段的Spring自定义注释验证 - @Notnull Spring Custom Annotation Validation with multiple field 是否有理由将@NotNull注释放在Java实体类的@Id字段中 - Is any reason to put @NotNull annotation on @Id field in Java entity class javax @notnull无效 - javax @notnull is not working 处理SonarQube错误“ javax.validation.constraints.NotNull”的最佳方法是什么? - What is the best way to handle SonarQube error “javax.validation.constraints.NotNull” Lombok 的 @NonNull 在验证期间干扰 javax.validation.constraints.NotNull - Lombok's @NonNull interfering with javax.validation.constraints.NotNull during validation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM