如何保存hibernate实体并忽略(不正确)瞬态模式。
例如:
@Entity @Table(name="`REQUEST`") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="`receiver`",discriminatorType=DiscriminatorType.STRING,length=20) public abstract class Request implements UUIDAble{ @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; @Transient @NotNull @Pattern(regexp="^([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}$") private String vaucher; @Enumerated(EnumType.STRING) @Column(name="name",nullable=false) private RequestName name; public RequestName getName() { return name; } public void setName(RequestName name) { this.name = name; } public String getVaucher() { return vaucher; } public void setVaucher(String vaucher) { this.vaucher = vaucher; } }
我想保存它:
Request request = new Request(); request.setVaucher("123"); request.setName(RequestName.SYSTEM);
弹簧产生错误:
javax.validation.ConstraintViolationException: validation failed for classes [ru.icb.cpaclick.domain.OperatorCheckRequest] during persist time for groups [javax.validation.groups.Default, ] at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:132) at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:71) at org.hibernate.action.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:159) at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:65) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
参见http://docs.jboss.org/hibernate/validator/4.2/reference/en- US/html_single/#validator-checkconstraints- orm。默认情况下,Hibernate(ORM)检查默认验证组的每个约束。因此,您可以将此特定约束放入另一个组中。
有关验证组的说明,请参见http://docs.jboss.org/hibernate/validator/4.2/reference/zh- CN/html_single/#validator-usingvalidator- validationgroups。