忽略保存休眠实体的瞬态模式

时间:2011-09-01 07:09:11

标签: java regex hibernate spring

如何保存休眠实体并忽略(不正确)瞬态模式。

例如:

@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);

Spring生成错误:

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)

2 个答案:

答案 0 :(得分:3)

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/en-US/html_single/#validator-usingvalidator-validationgroups

答案 1 :(得分:2)

您应该使用hibernate验证器的groups功能,并为瞬态字段上指定的验证约束分配不同的group

如果您希望启用这些约束,则需要特别提及该组。默认情况下,当hibernate尝试保存实体时,它会将所有验证作为默认组的一部分进行检查。