Domanda

I would like the group some annotation in one and then need that the annotations work fine with Hibernate-validator and the generate-ddl utily of Hibernate

I have a bunch of entities and most of then had similar fields. For example:

@Entity 
public class Usuario implements Serializable {

    @Column(name = "ID", nullable = false)
    @Id
    private Long id;

    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "CODIGO", nullable = false)
    @NotNull
    private Integer code;

    @Column(name = "NOMBRE", length = 75, nullable = false)
    @NotNull
    @Size(max = 75)
    private String name;

    @Column(name = "CLAVE", length = 75, nullable = false)
    @NotNull
    @Size(max = 75)
    private String password;

    @Column(name = "ES_ADMINISTRADOR")
    @NotNull
    @Type(type = "org.hibernate.type.NumericBooleanType")
    private Boolean isAdmin;

    [...]
}

And I want have something like this other

@Entity 
public class Usuario implements Serializable {

    @Column(name = "ID", nullable = false)
    @Id
    private Long id;

    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @CodeField
    private Integer code;

    @Column(name = "NOMBRE")
    @NameField
    private String name;

    @Column(name = "CLAVE", length = 75, nullable = false)
    @NotNull
    @Size(max = 75)
    private String password;

    @Column(name = "ES_ADMINISTRADOR")
    @BooleanField
    private Boolean isAdmin;

    [...]
}

Can I do that?

È stato utile?

Soluzione

No, that's not possible. The composition feature of Bean Validation only allows to aggregate several constraint annotations into a new higher-level constraint, but this does not take any JPA annotations into account.

Altri suggerimenti

I am not sure what exactly you want to achieve, but as far as it comes to Hibernate Validator, it is possible to create your own constraints through implementing ConstraintValidator interface.

I suggest you to read the manual, it's fairly easy: http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html/validator-customconstraints.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top