本文介绍了忽略保存hibernate实体的瞬态模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:<$ p
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name =`
@Entity
@Table(name =`REQUEST`接收器,discriminatorType = DiscriminatorType.STRING,长度= 20)
public abstract class Request实现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;
}
}
我想保存它:
请求请求= new Request();
request.setVaucher(123);
request.setName(RequestName.SYSTEM);
Spring产生错误:
javax.validation.ConstraintViolationException:对于组[java.value.groups.Default,]
持久化时间期间,类[ru.icb.cpaclick.domain.OperatorCheckRequest]的验证失败。 hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:132)
处org.hibernate.action org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:71)
。 EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:159)
在org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:65)
在org.hibernate.engine.ActionQueue.execute(ActionQueue.java:
org.hibernate.event.def.AbstractSaveEventListener.performSave(Abs tractSaveEventListener.java:204)美元,org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130 B $ B)
在org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:在org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56 210)
)
。在org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)$ b在org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
$ b在org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
。在组织.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
解决方案
请参阅。默认情况下,Hibernate(ORM)检查默认验证组的每个约束。所以你可以把这个特定的约束放入另一个组中。
参见以获取验证组的解释。
How to save hibernate entity and ignore (not correct)transient pattern.
for example:
@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;
}
}
and i want to save it:
Request request = new Request();
request.setVaucher("123");
request.setName(RequestName.SYSTEM);
Spring generate error:
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)
解决方案
See http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html_single/#validator-checkconstraints-orm. By default, Hibernate (the ORM) checks every constraint of the default validation group. So you might put this particular constraint into another group.
See http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html_single/#validator-usingvalidator-validationgroups for explanations on validation groups.
这篇关于忽略保存hibernate实体的瞬态模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!