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

  • Home
  • SEARCH
  • 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 6320141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:55:11+00:00 2026-05-24T15:55:11+00:00

I have a Spring MVC sample application it uses UserDaoImpl class to save a

  • 0

I have a Spring MVC sample application it uses UserDaoImpl class to save a User type object to database. Following is the UserDaoImpl code.

public class UserDaoImpl implements UserDao<User> {

private EntityManagerFactory emf;

@PersistenceContext
private EntityManager em;

@Transactional
public void saveUser(User user){
    em.persist(user);
}

}

and my UserDao interface looks like this.

public interface UserDao<User> {
public void saveUser(User user);

}

Weird thing is happening when when my UserDaoImpl class implements the UserDao interface. When trying to persist it gives the following error.

java.lang.ClassCastException: $Proxy71 cannot be cast to org.samith.UserDaoImpl

When I remove the “implements UserDao” part from the UserDaoImpl class user is persisted to database as expected.

Also I tried UserDao interface without User parameter(that is non generic version). Still above error occurs.

This may be a trivial question to answer but I am scratching my head finding a solution for about few hours.

What wrong am I doing ??

  • 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-24T15:55:12+00:00Added an answer on May 24, 2026 at 3:55 pm

    You’re not providing the problem code, (or the full stack trace) but the rundown is this:

    When you annotate a class as @Transactional, and Spring creates an instance for you, what you get is not that class, but a Java Dynamic Proxy that implements that classes’ interfaces. http://download.oracle.com/javase/1.3/docs/guide/reflection/proxy.html

    Because of this, you cannot cast that object to the original type (it’s not that type anymore!) and must use it’s interface(s) instead.

    If there aren’t interfaces to implement, it will give you a CGLib proxy of the class, which is basically just a runtime modified version of your class, and so is assignable to the class itself.

    Search for where you’re injecting, or casting to type UserDaoImpl, and change the reference to UserDao instead, and it will work properly.

    I’ve read the performance difference between CGLib proxies and Dynamic Java Proxies is quite minimal, so you can also add the following to your spring config to force it to use CGLib proxies instead of Java Dynamic Proxies:

    <aop:config proxy-target-class="true">
    

    In general however, I’d recommend you not use CGLib Proxies, but instead access your class from its interface. Loose coupling will allow you to do runtime substitution, and limits your ability to accidentally introduce brittle class dependencies.

    There are some problems that using Java Dynamic Proxies will introduce however, and what you’re doing is close enough to what I was doing that you should be aware of this:

    How to use Dynamic Proxies with JSF when the method signature contains Object … args

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

Sidebar

Related Questions

We have a Hibernate/Spring application that have the following Spring beans: <bean id=transactionManager class=org.springframework.orm.hibernate3.HibernateTransactionManager
I basically have the following flow: XML -> JSON -> Spring MVC -> jsp
I am using Spring MVC to build my web application, and I have a
I'm working on a Spring MVC project, and I have unit tests for all
I have two Spring proxies set up: <bean id=simpleBean class=org.springframework.aop.framework.ProxyFactoryBean> <property name=target> <ref local=simpleBeanTarget/>
We have started to use spring aop for cross cutting aspects of our application
I'm currently building a Spring MVC application. I was looking to use JSP pages
For an application build on Spring MVC + Groovy + Google App Engine i
I'm working on a Java web application (Adobe Flex front-end, JPA/Hibernate/BlazeDS/Spring MVC backend) and
So I've got a simple web application using Spring MVC + Hibernate and am

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.