我的服务中有这个删除方法。
public boolean delete(Integer id) {
Vacancy vacancy = vacancyDao.findById(id);
return vacancy != null && vacancyDao.remove(vacancy);
}
dao实现:
@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;
}
在地方1
我有classNotFoundException
,我不在控制台。 Hibernate从'类'中搜索。
但执行后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)
你能帮忙做出我想要的吗?
P.S。
VacancyService标记为@Transactional
答案 0 :(得分: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
。
来自Hibernate文档
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();
}
答案 1 :(得分:0)
试试这个:
Query query = session.createQuery("delete Vacancy vac where vac.id=:id");
query.setInteger("id", id);
query.executeUpdate();