2012-09-09 2 views
1

나는에 따라 클래스가 : 처리하는 방법에 콩 검증 예외가

@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하려고 말, 있어야하지 않는 null입니다. 다음과 같은 예외가 발생합니다 :

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