Vince V.
Vince V.

Reputation: 3143

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.

Upvotes: 3

Views: 4953

Answers (2)

Nomad
Nomad

Reputation: 1100

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.

Upvotes: 0

Biju Kunjummen
Biju Kunjummen

Reputation: 49935

You may be missing tx:annotation-driven in your xml - this is the one which triggers creation of proxies for @Transaction annotated beans - if the request is not within a transaction the delete will not work.

Upvotes: 2

Related Questions