Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is getHibernateTemplate().delete(entity) failing?
    text
    copied!<p>Here is the code:</p> <p>// Hibernate model</p> <pre><code>@Entity @Table(name="contact") public class Contact { @Id @GeneratedValue(strategy= GenerationType.AUTO) @Column(name = "contact_id") private Long id; @Column(name="contact_ref_id") private String contactRefId; @Column(name="first_name") private String firstName; @Column(name="last_name") private String lastName; @ManyToOne @JoinColumn(name="app_user_id") /** user whose contact list owns this contact */ private AppUser appUser; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } /** * @return AppUser who owns contact list this contact belongs to */ public AppUser getAppUser() { return appUser; } public void setAppUser(AppUser appUser) { this.appUser=appUser; } public String getContactRefId() { return contactRefId; } public void setContactRefId(String contactRefId) { this.contactRefId=contactRefId; } </code></pre> <p>}</p> <p>// DAO layer</p> <pre><code>private Criteria createCriteria() { return getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Contact.class); } public void deleteContact(String contactRefId) { Criteria criteria = createCriteria(); criteria.add(Restrictions.eq("contactRefId", contactRefId)); Contact contact = (Contact)criteria.uniqueResult(); try { contact = getHibernateTemplate().merge(contact); getHibernateTemplate().delete(contact); } catch (DataAccessException e) { log.error(e.getMessage()); throw e; } } </code></pre> <p>// Service layer</p> <pre><code>public void deleteContact(String contactRefId) { contactDao.delete(contactRefId); } </code></pre> <p>// Unit test</p> <pre><code>@Test public void testDeleteContact() { contactService.deleteContact("fe43b43a-d77f-45ce-b024-bb6e93264a69"); } </code></pre> <p>// Spring config</p> <pre><code>&lt;beans&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource"/&gt; &lt;property name="packagesToScan" value="model.hibernate"/&gt; &lt;property name="hibernateProperties"&gt; &lt;value&gt; hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect hibernate.query.substitutions=true 'Y', false 'N' hibernate.cache.use_second_level_cache=true hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider &lt;/value&gt; &lt;/property&gt; &lt;/bean&gt; &lt;!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --&gt; &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;property name="sessionFactory" ref="sessionFactory" /&gt; &lt;/bean&gt; &lt;bean id="contactsDao" class="ContactDaoHibernate"&gt; &lt;property name="sessionFactory" ref="sessionFactory" /&gt; &lt;/bean&gt; &lt;aop:config&gt; &lt;aop:advisor advice-ref="txAdvice" pointcut="execution(* service..*.*(..))" order="0"/&gt; &lt;/aop:config&gt; &lt;!-- Enable @Transactional support --&gt; &lt;tx:annotation-driven/&gt; &lt;!-- Enable @AspectJ support --&gt; &lt;aop:aspectj-autoproxy proxy-target-class="true" /&gt; &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt; &lt;tx:attributes&gt; &lt;tx:method name="*"/&gt; &lt;/tx:attributes&gt; &lt;/tx:advice&gt; &lt;/beans&gt; </code></pre> <p>When I fire the test method, no exceptions are thrown, but the row in the db is not deleted. Examination of the Contact object in the debugger after it's fetched from the db by criteria.uniqueResult() reveals the complete and correct object was fetched. Although the merge call may appear unneccesary, I did that to see if there was an issue w/ the wrong Session being used for the delete. </p> <p>It appears therefore that the the delete is not being called in a transaction, although the Spring config is simple and I don't see any potential issues. What is most perplexing is that this DAO code works:</p> <pre><code>public void addContact(Contact contact) { try { getHibernateTemplate().save(contact); } catch (DataAccessException e) { log.error(e.getMessage()); throw e; } } </code></pre> <p>I'm out of ideas.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload