2012-09-09 46 views
1

我有根據類:如何處理Bean驗證例外

@Entity 
public class Car { 


    @Id 
    @GeneratedValue 
    private Long id; 

    @NotNull 
    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "OWNER_ID")  
    private Owner owner; 

    @NotNull() 
    @Column(nullable = false) 
    private String make; 

    @NotNull() 
    @Column(nullable = true) 
    private String model; 


    @Column(nullable = false) 
    private String gears; 

    // More fields 

說我想percist對象和一個字段,進行實地例如,是不應該爲空。但下列情況除外拋出:

WARNING: EJB5184:A system exception occurred during an invocation on EJB CarEJB, method: public se.while_se.domain.Car se.while_se.business.CarEJB.createCar(se.while_se.domain.Car) 
Sep 09, 2012 12:37:37 PM com.sun.ejb.containers.BaseContainer postInvoke 
WARNING: 
javax.ejb.EJBException 
     at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5215) 
     at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5113) 
     at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4901) 
     at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045) 
     at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994) 
     at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222) 
     at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88) 
     at $Proxy137.createCar(Unknown Source) 
     at se.while_se.business.__EJB31_Generated__CarEJB__Intf____Bean__.createCar(Unknown Source) 
     at test.CarTest.populate(CarTest.java:197) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:601) 
     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) 
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
     at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
     at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:45) 
     at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) 
     at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:601) 
     at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) 
     at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) 
     at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) 
     at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) 
     at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) 
Caused by: javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'. Please refer to embedded ConstraintViolations for details. 

我想這樣做的是找到之前驗證實體在數據庫中堅持其最佳實踐。當然,我可以在實體類中實現一種幫助方法,如:

public boolean validate() { 
     if(owner == null) {    
      return false; 
     } 
     if(make == null) { 
      return false; 
     } 
     if(model == null) { 
      return false; 
     } 
     return true; 
    } 

但是,這並不是正確的方法。你能指導我嗎?

問候

回答

8

你可以和一直喜歡在此之前應驗證實體,並返回相應的錯誤:

Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); 
Set<ConstraintViolation<Car>> constraintViolations = validator.validate(myCar); 

if (constraintViolations.size() > 0) { 
    Set<String> violationMessages = new HashSet<String>(); 

    for (ConstraintViolation<T> constraintViolation : constraintViolations) { 
     violationMessages.add(constraintViolation.getPropertyPath() + ": " + constraintViolation.getMessage()); 
    } 

    throw new RuntimeException("Car is not valid:\n" + StringUtils.join(violationMessages, "\n")); 
} 
+0

非常感謝你。這正是我所期待的! =) – kungcc