Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6053935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:05:46+00:00 2026-05-23T08:05:46+00:00

Here is the code: // Hibernate model @Entity @Table(name=contact) public class Contact { @Id

  • 0

Here is the code:

// Hibernate model

@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;
}

}

// DAO layer

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;
    }
}

// Service layer

public void deleteContact(String contactRefId) {
    contactDao.delete(contactRefId);
}

// Unit test

@Test
public void testDeleteContact() {
    contactService.deleteContact("fe43b43a-d77f-45ce-b024-bb6e93264a69");
}

// Spring config

<beans>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="model.hibernate"/>
    <property name="hibernateProperties">
        <value>
            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
        </value>
    </property>
</bean>

<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="contactsDao" class="ContactDaoHibernate">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<aop:config>
  <aop:advisor advice-ref="txAdvice" pointcut="execution(* service..*.*(..))" order="0"/>
</aop:config>

<!-- Enable @Transactional support -->
<tx:annotation-driven/>

<!-- Enable @AspectJ support -->
  <aop:aspectj-autoproxy proxy-target-class="true" />

  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
       <tx:method name="*"/>
    </tx:attributes>
  </tx:advice>
</beans>

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.

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:

public void addContact(Contact contact) {
    try {
        getHibernateTemplate().save(contact);
    } catch (DataAccessException e) {
        log.error(e.getMessage());
        throw e;
    }
}

I’m out of ideas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:05:46+00:00Added an answer on May 23, 2026 at 8:05 am

    It’s highly recommended to implement equals() and hashCode() for your object since you seem to use separate sessions for each operation.

    See http://docs.jboss.org/hibernate/core/3.3/reference/en/html/persistent-classes.html#persistent-classes-equalshashcode


    Update

    It appears that your Spring based unit test does not commit transaction. By default Spring test framework will rollback transaction that it started.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the BlogPost model : @Entity @Table(name = blog_posts) public class BlogPost extends
I have a parent-child relationship: @Entity @Table(name = user) public final class User {
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
enter code here I have a table on SQL server 2005 with bigint primary
Below is the my complete code Here is cfg file <hibernate-configuration> <session-factory> <!-- Database
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here is code from MSDN . I don't understand why the work isn't just
The code here is X++. I know very little about it, though I am
enter code here Hi All, I have a simple windows service application that connects
Edit: The code here still has some bugs in it, and it could do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.