Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何处理Bean验证异常_Java_Bean Validation - Fatal编程技术网

Java 如何处理Bean验证异常

Java 如何处理Bean验证异常,java,bean-validation,Java,Bean Validation,我有一门课是根据: @Entity public class Car { @Id @GeneratedValue private Long id; @NotNull @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "OWNER_ID") private Owner owner; @NotNull() @Column(nullable = fa

我有一门课是根据:

@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
假设我试图输入对象,一个字段,例如makefield,为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;
    }
但这并不是正确的做法。你能指引我吗


致以最诚挚的问候

您可以而且应该在像这样持续之前验证实体,并返回适当的错误:

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"));
}
Validator Validator=Validation.buildDefaultValidatorFactory().getValidator();
Set constraintViolations=validator.validate(myCar);
if(constraintViolations.size()>0){
Set violationMessages=new HashSet();
for(ConstraintViolation ConstraintViolation:constraintViolations){
添加(constraintViolation.getPropertyPath()+”:“+constraintViolation.getMessage());
}
抛出新的运行时异常(“Car无效:\n”+StringUtils.join(violationMessages,“\n”);
}

非常感谢。这正是我想要的!=)