The problem is that the code fragment does not delete the record in the database.
import org.hibernate.Session; import org.hibernate.SessionFactory; ... ... void deleteForm() { Session session = sessionFactory.openSession(); FormDO formDO = new FormDO(); formDO.setId(formId); session.delete(formDO);
However, if I call session.flush () after deletion, it works fine. Please note that I do NOT use transactions.
In the JavaDoc class of the Session class, the description of the delete method:
Remove the persistent instance from the data store. The argument may be an instance associated with the receiving session, or a temporary instance with an identifier associated with an existing persistent state.
And I saw a lot of code snippets on the Internet that shows that there is no need to call flush () after delete (). A similar question was raised in another forum here , but it remained unanswered.
In addition, session.save works fine without session.flush.
I am using Hibernate 4.2.16 + Spring 4.0.9 + JPA 1.0 annotations. Below are the source files for future reference.
FormDO.java
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="form") public class FormDO { @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") Integer id; @Column(name="name") String name; ... ...
Spring configuration file
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/tempdb" /> <property name="username" value="root" /> <property name="password" value="****" /> </bean> <bean id="hibernate4AnnotatedSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name="dataSource" ref="dataSource" /> <property name="annotatedClasses"> <list> <value>com.test.FormDO</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.current_session_context_class">thread</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
FormDAO.java
@Named public class FormDAO { @Inject private SessionFactory sessionFactory; public boolean deleteForm(Integer formId) { Session session = sessionFactory.openSession(); FormDO formDO = new FormDO(); formDO.setId(formId); session.delete(formDO); session.flush();
UPDATE:
My dilemma was largely due to inconsistency .. session.save inserted the record immediately, but session.delete does not reflect unless flush () was explicitly called. But when I link to the Flushing the Session link sent by Afsun, my doubts were cleared by reading the following line,
An exception is that objects using built-in authentication are inserted when they are saved.
I really appreciate the answers that everyone publishes, since almost all of them point in the right direction, but Afsun completely clears my doubts. Thanks!