简体   繁体   中英

Hibernate and spring mvc Deleting and updating

We are developing a spring application with Spring MVC and hibernate. Now we ran into a problem that we can't solve.. The problem arises when we try to delete something.

If we delete the page just loads fine and goes further like everything is succeeded, but the value in the database isn't deleted.

Here is our code:

This is the TestDao

@Repository
public class TestDaoImpl implements TestDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public Test get(int id) {
        return (Test)this.sessionFactory.getCurrentSession().createQuery("FROM Test WHERE id =:ident").setParameter("ident",id).uniqueResult();
    }

    @Override
    public void delete(int id) {

        this.sessionFactory.getCurrentSession().delete(this.get(id));

    }
}

This is our service (business layer)

@Service("testService")
public class TestServiceImpl implements TestService {

    private final TestDao testDao;

    @Inject
    public TestServiceImpl(TestDao testDao)
    {
        this.testDao = testDao;
    }

    @Override
    @Transactional
    public void delete(int id) {
          testDao.delete(id);
    }
}

And this is the controller:

@Controller
public class TestingController {

    @Qualifier("testService")
    @Autowired
    private TestService testService;

    @RequestMapping(value = "/testing")
    public ModelAndView testing()
    {
        testService.delete(1);
        return new ModelAndView("home");
    }

}

This is the hibernate configuration:

<!-- Parse database properties -->
    <context:property-placeholder location="classpath:db/db.properties"/>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="mappingResources">
            <list>
                <value>db/mappings/User.hbm.xml</value>
                <value>db/mappings/Authority.hbm.xml</value>
                <value>db/mappings/Car.hbm.xml</value>
                <value>db/mappings/Address.hbm.xml</value>
                <value>db/mappings/DrivingDay.hbm.xml</value>
                <value>db/mappings/Message.hbm.xml</value>
                <value>db/mappings/Ride.hbm.xml</value>
                <value>db/mappings/RouteAgreement.hbm.xml</value>
                <value>db/mappings/Route.hbm.xml</value>
                <value>db/mappings/RouteTime.hbm.xml</value>
                <value>db/mappings/SocialMediaLogin.hbm.xml</value>
                <value>db/mappings/Variables.hbm.xml</value>
                <value>db/mappings/Waypoint.hbm.xml</value>
                <value>db/mappings/Test.hbm.xml</value>
            </list>
        </property>
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
               <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

I know it's a wall of code, I'm sorry about that, but I thought I provide as many details as possible.

Thanks in advance

Edit: There is a value with id 1 in the database.

您可能在xml中缺少tx:annotation-driven - 这是触发为@Transaction带注释的bean创建代理的那个 - 如果请求不在事务中,则删除将不起作用。

Try, enclosing your transcation like begin/end like this.

Transaction tx = session.beginTransaction();
// your operation.
tx.commit();

Looks like the operation is rolled back after the code execution. you need to commit the transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM