@Size验证消息后的EntityNotFoundException

时间:2016-05-17 17:28:30

标签: java hibernate

当系统使用@Size验证单个字段时,它会返回错误消息(确定,它是正确的),但它也会返回带有id的实体,然后如果我尝试再次保存此实体,它会获得我是一个EntityNotFoundException错误。

@Entity
@Table(name = "DOCUMENTS")
public class Document {

   private static final long serialVersionUID = 1L;

   @Id
   @SequenceGenerator(name = "DOC_SEQ", sequenceName = "ID_DOCUMENT_SEQ", allocationSize = 1)
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DOC_SEQ")
   @Column(name = "ID_DOCUMENT")
   private Integer idDocument;

   @Column(name = "NOME_ARQUIVO")
   @NotNull
   @Size(max = 100, message = "Validation message.")
   private String nomeArquivo;
}

按照以下错误消息:

javax.ejb.EJBException: javax.persistence.EntityNotFoundException: Unable to find com.procergs.pra_aj4.ed.Document with id 13172
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:189)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:274)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:339)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:238)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:55)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:185)
    at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:182)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:73)

2 个答案:

答案 0 :(得分:0)

此问题已解决。如果我使用@Max或@Min代替@Size它可以很好地工作。

答案 1 :(得分:0)

您还可以使用以下方式为字符串字段设置最大列长度:

@Column(nullable = true, length = 1000) // max length will be 1000
private String someString;