Hibernate 3.6.10.Final ²Î¿¼ÊÖ²á ÖÐÎÄ°æ

Hibernate

Hibernate.org Community Documentation

  • auto (default): enable integration between Bean Validation and Hibernate (callback and ddl generation) only if Bean Validation is present in the classpath.

  • none: disable all integration between Bean Validation and Hibernate

  • callback: only validate entities when they are either inserted, updated or deleted. An exception is raised if no Bean Validation provider is present in the classpath.

  • ddl: only apply constraints to the database schema when generated by Hibernate. An exception is raised if no Bean Validation provider is present in the classpath. This value is not defined by the Java Persistence spec and is specific to Hibernate.

注意

You can use both callback and ddl together by setting the property to callback, dll


<persistence ...>
  <persistence-unit ...>
    ...
    <properties>
      <property name="javax.persistence.validation.mode"
                value="callback, ddl"/>
    </properties>
  </persistence-unit>
</persistence>

This is equivalent to auto except that if no Bean Validation provider is present, an exception is raised.

  • javax.persistence.validation.group.pre-persist: groups validated when an entity is about to be persisted (default to Default)

  • javax.persistence.validation.group.pre-update: groups validated when an entity is about to be updated (default to Default)

  • javax.persistence.validation.group.pre-remove: groups validated when an entity is about to be deleted (default to no group)

  • org.hibernate.validator.group.ddl: groups considered when applying constraints on the database schema (default to Default)


<persistence ...>
  <persistence-unit ...>
    ...
    <properties>
      <property name="javax.persistence.validation.group.pre-update"
                value="javax.validation.group.Default, com.acme.group.Strict"/>
      <property name="javax.persistence.validation.group.pre-remove"
                value="com.acme.group.OnDelete"/>
      <property name="org.hibernate.validator.group.ddl"
                value="com.acme.group.DDL"/>
    </properties>
  </persistence-unit>
</persistence>

注意

You can set these properties in hibernate.cfg.xml, hibernate.properties or programmatically.

  • @NotNull leads to a not null column (unless it conflicts with components or table inheritance)

  • @Size.max leads to a varchar(max) definition for Strings

  • @Min, @Max lead to column checks (like value <= max)

  • @Digits leads to the definition of precision and scale (ever wondered which is which? It's easy now with @Digits :) )