春天 – 例外:预期但是

前端之家收集整理的这篇文章主要介绍了春天 – 例外:预期但是前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我得到这个错误

java.lang.Exception: Unexpected exception,expectedMetadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:85)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:478)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:424)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:388)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:340)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:158)
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:136)
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94)
    at org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:181)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:81)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:286)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)
    at com.yavale.baseProject.test.UsuarioTest.unicidadCampoLogin(UsuarioTest.java:50)
    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:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:22)
    ... 20 more

这是我的考验:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext.xml","classpath:test-applicationContext.xml" }) 
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=true)
public class RolTest extends AbstractTransactionalJUnit4SpringContextTests {

    @Autowired
    RolDAO rolDAO;

    @Autowired
    private SessionFactory mySessionFactory;


    private final static Logger logger = Logger.getLogger(UsuarioTest.class);

    @Test(expected=org.hibernate.exception.ConstraintViolationException.class)
    public void unicidadCampoNombre() {

        logger.info("Prueba: Se trata de insertar dos roles con el mismo nombre");

        String nombre = "Administrador";

        Rol rol1 = new Rol(nombre,"descripcion");
        Rol rol2 = new Rol(nombre,"descripcion");

        rolDAO.insert(rol1);
        rolDAO.insert(rol2);

        mySessionFactory.getCurrentSession().flush();

    }

关于这一点的奇怪之处在于,这曾经起作用了.现在,mySessionFactory.getCurrentSession().flush();抛出这个例外.另一个奇怪的事情是我使用相同的测试与不同的对象,它运作良好:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext.xml",defaultRollback=true)
public class PermisoTest extends AbstractTransactionalJUnit4SpringContextTests {

    @Autowired
    PermisoDAO permisoDAO;

    @Autowired
    private SessionFactory mySessionFactory;


    private final static Logger logger = Logger.getLogger(UsuarioTest.class);

    @Test(expected=org.hibernate.exception.ConstraintViolationException.class)
    public void unicidadCampoNombre() {

        logger.info("Prueba: Se trata de insertar dos permisos con el mismo nombre");

        String nombre = "Eliminar";

        Permiso permiso1 = new Permiso(nombre,"descripcion");
        Permiso permiso2 = new Permiso(nombre,"descripcion");

        permisoDAO.insert(permiso1);
        permisoDAO.insert(permiso2);

        mySessionFactory.getCurrentSession().flush();

    }

有任何想法吗??

最佳答案
从hibernate-validator 4.something更新到5.0.1.Final时,我也遇到了这个问题.显然,这个版本的hibernate-validator(可能还有一些其他的hibernate jar)缺少一些依赖项,即el-api和el-ri.我通过添加这些依赖项解决了这个问题:

在我看来,这是解决这个问题最简洁的方法.

原文链接:https://www.f2er.com/spring/431741.html

猜你在找的Spring相关文章