I am quite new to spring MVC. Below I mentioned the exception (javax.persistence.TransactionRequiredException: Executing an update/delete query) I am getting whenever I am going to execute DELETE query. The code is working fine with CREATE and READ operations.
------>I am using Spring 4.3.2.RELEASE and HIBERNATE 5.2.7
servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU" xmlns:aop="http://ift.tt/OpNdV1"
xmlns:context="http://ift.tt/GArMu7"
xmlns:jdbc="http://ift.tt/18IIlo0" xmlns:mvc="http://ift.tt/1bHqwjR"
xmlns:oxm="http://ift.tt/KwCHtD" xmlns:task="http://ift.tt/LFBt0P"
xmlns:tx="http://ift.tt/OGfeU2"
xsi:schemaLocation="http://ift.tt/GArMu6 http://ift.tt/1jdM0fG
http://ift.tt/OpNdV1 http://ift.tt/2gztjWH
http://ift.tt/GArMu7 http://ift.tt/1YBgPjE
http://ift.tt/18IIlo0 http://ift.tt/1UqMotm
http://ift.tt/1bHqwjR http://ift.tt/28ICy73
http://ift.tt/KwCHtD http://ift.tt/2lF2YZY
http://ift.tt/LFBt0P http://ift.tt/1YBgUnw
http://ift.tt/OGfeU2 http://ift.tt/1UqMu4g">
<mvc:annotation-driven />
<context:component-scan base-package="com.practice.controller"></context:component-scan>
<context:component-scan base-package="com.practice.dao"></context:component-scan>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/dbutil"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses" value="com.practice.model.Student"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- To create classes -->
<bean id="studentDaoSupport" class="com.practice.dao.StudentDaoImpl"></bean>
<bean id="studentService" class="com.practice.services.StudentServiceImpl"></bean>
</beans>
Model Class
@Entity
@Table(name = "STUDENT")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "STUD_ID")
private int studId;
@Column(name="STUD_NAME")
private String studName;
@Column(name = "STUD_STD")
private String studStd;
public int getStudId() {
return studId;
}
public void setStudId(int studId) {
this.studId = studId;
}
public String getStudName() {
return studName;
}
public void setStudName(String studName) {
this.studName = studName;
}
public String getStudStd() {
return studStd;
}
public void setStudStd(String studStd) {
this.studStd = studStd;
}
}
DAO Implementation Class
@Repository
public class StudentDaoImpl implements StudentDaoSupport {
public StudentDaoImpl(){
System.out.println("Student Dao Impl object created");
}
@Autowired
private SessionFactory sessionFactory;
@Override
@Transactional
public void saveStudent(Student student) {
//Database connection logic goes here
System.out.println("dao layer = saveStudent Method");
Session session = sessionFactory.openSession();
session.saveOrUpdate(student);
System.out.println("student data saved");
}
@Override
@Transactional
public List<Student> studentList() {
// TODO Auto-generated method stub
List<Student> list = new ArrayList<Student>();
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Student.class);
list.clear();
list = criteria.list();
return list;
}
@Override
@Transactional
public void deleteStudent(Integer studId) {
Session session = sessionFactory.openSession();
Query q = session.createQuery("DELETE from Student where studId=:id");
q.setParameter("id", studId);
q.executeUpdate();
System.out.println("deleted successfully");
}
}
Aucun commentaire:
Enregistrer un commentaire