在保存休眠实体时忽略瞬态模式
Posted
技术标签:
【中文标题】在保存休眠实体时忽略瞬态模式【英文标题】:Ignore transient pattern on save hibernate entity 【发布时间】:2011-11-08 04:24:11 【问题描述】:如何保存休眠实体并忽略(不正确)瞬态模式。
例如:
@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)
【问题讨论】:
【参考方案1】:见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。
【讨论】:
【参考方案2】:您应该使用休眠验证器的组功能,并为在瞬态字段上指定的验证约束分配一个不同的group。
如果您希望启用这些约束,您需要特别提及该组。默认情况下,当休眠尝试保存实体时,它将检查所有验证作为默认组的一部分。
【讨论】:
以上是关于在保存休眠实体时忽略瞬态模式的主要内容,如果未能解决你的问题,请参考以下文章
对象引用未保存的瞬态实例 - 在刷新休眠 JPA 之前保存瞬态实例 [重复]
在 Postgresql 上创建模式时,Hibernate 忽略 @GeneratedValue