1

我的服务中有这个删除方法。

public boolean delete(Integer id) {
        Vacancy vacancy = vacancyDao.findById(id);
        return vacancy != null && vacancyDao.remove(vacancy);
    }

道实现:

@Override
    public boolean remove(Vacancy vacancy) throws HibernateException {
        Session session = sessionFactory.getCurrentSession();
        if (vacancy == null) {
            return false;
        }

        int result = session.createQuery("delete from Vacancy where id = :id")
                .setInteger("id", vacancy.getId()).executeUpdate();// place 1
        return result > 0;

    }

在我有的地方1classNotFoundException我不在控制台中。休眠搜索“来自”类。

但执行后result == 1

为了测试我写了这样的方法

 @Test
 public void delete(){
            vacancyService.delete(vacancy.getId());

            sessionFactory.getCurrentSession().flush();

            Vacancy vacancyFromDb = vacancyService.findById(vacancy.getId());       

            Assert.assertNull(vacancyFromDb);
        }

在日志中我看到

Hibernate: delete from vacancy where id=?

但在测试中我有例外:

java.lang.AssertionError: expected null, but was:<com.epam.hhsystem.model.vacancy.Vacancy@1>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotNull(Assert.java:664)
    at org.junit.Assert.assertNull(Assert.java:646)
    at org.junit.Assert.assertNull(Assert.java:656)
    at com.epam.hhhsystem.services.VacancyServiceTest.delete(VacancyServiceTest.java:72)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

你能帮我做我想要的吗?

附言

VacancyService 标记为@Transactional

4

2 回答 2

0

你可以使用session.delete(),改变你的remove方法,比如

@Override
    public boolean remove(Vacancy vacancy) throws HibernateException {
        Session session = sessionFactory.getCurrentSession();
        if (vacancy == null) {
            return false;
        }

        session.delete(vacancy);
        //delete method does not returns anything, implement some logic to 
        determine the result.
    }

编辑:检查您的transaction. 从休眠文档

 Session sess = factory.openSession();
 Transaction tx;
 try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();
 }
 catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
 }
 finally {
     sess.close();
 }
于 2013-09-26T12:16:21.447 回答
0

尝试这个:

Query query = session.createQuery("delete Vacancy vac where vac.id=:id");
query.setInteger("id", id);
query.executeUpdate();
于 2013-09-26T12:28:00.287 回答