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 8847423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:11:13+00:00 2026-06-14T12:11:13+00:00

This is a very simple spring + hibernate example. What am I doing wrong

  • 0

This is a very simple spring + hibernate example. What am I doing wrong here?

DTO:

package com.xx.dto;

import java.util.Date;
import javax.persistence.*;

@Entity
@Table(name = "users")
public class UserData {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Integer id;

    @Column(name = "user_firstname")
    private String firstname;

    @Column(name = "user_lastname")
    private String lastname;

    @Column(name = "user_sex")
    private String sex;

    @Column(name = "user_birthdate")
    private Date birthDate;

    @Column(name = "user_email")
    private String email;

    public Integer getId() {
        return id;
    }

    public void setId(Integer 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;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

DAO interface:

package com.xx.dao;

import java.util.List;
import com.provisori.dto.UserData;

public interface UserDataDao {

    void saveUser(UserData user);

    void deleteUser(String key);

    void updateUser(UserData user);

    List<UserData> listUser();
}

DAO implementation:

package com.xx.imp;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.provisori.dao.UserDataDao;
import com.provisori.dto.UserData;

public class UserDataImp implements UserDataDao{

    @Autowired
    SessionFactory sessionFactory;

    @Transactional
    @Override
    public void saveUser(UserData user) {
        sessionFactory.getCurrentSession().save(user);
    }

    @Transactional
    @SuppressWarnings("unchecked")
    @Override
    public List<UserData> listUser() {
        return sessionFactory.getCurrentSession()
                .createCriteria(UserData.class).list();
    }

    @Transactional
    @Override
    public void updateUser(UserData user) {
        sessionFactory.getCurrentSession().update(user);

    }

    @Transactional
    public UserData getUser(String key) {
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(UserData.class);
        criteria.add(Restrictions.eq("key", key));
        return (UserData) criteria.uniqueResult();
    }

    @Transactional
    @Override
    public void deleteUser(String key) {
        UserData user = getUser(key);
        sessionFactory.getCurrentSession().delete(user);
    }
}

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <!-- Needed for Autowiring -->
    <context:annotation-config />

    <!-- MySQL DataSource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/xx" />
        <property name="user" value="root" />
        <property name="password" value="" />
    </bean>

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.provisori.dto.UserData</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.hbm2ddl.auto=update
                hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
            </value>
        </property>
    </bean>

    <!-- Transaction Management -->
    <tx:annotation-driven transaction-manager="txManager" />
    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- Java Bean -->
    <bean id="userDataDao" class="com.provisori.imp.UserDataImp" />

</beans>

Main.class

package com.provisori.dto;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.provisori.dao.UserDataDao;
import com.provisori.imp.UserDataImp;

public class TestMain {

   public static void main(String[] args) {

      // Construct the spring application context
      AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

      // Register hook to shutdown Spring gracefully
      // See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-shutdown
      context.registerShutdownHook();

      // Get the business bean from context
      UserDataDao dao = (UserDataImp) context.getBean("userDataDao");

      // Create simple property objects
      UserData user = new UserData();
      user.setFirstname("firstnameTest");
      user.setLastname("lastnameTest");
      dao.saveUser(user);
   }
}

result

Exception in thread “main” java.lang.ClassCastException: $Proxy13 cannot be cast to com.provisori.imp.UserDataImp
at com.provisori.dto.TestMain.main(TestMain.java:21)

  • 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-06-14T12:11:14+00:00Added an answer on June 14, 2026 at 12:11 pm

    The error is a good indicator — interfaces can’t be instantiated. Use the bean implementation:

    <bean id="userDataDao" class="com.provisori.imp.UserDataImp"> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this very simple code example, messages get lost every once in a while.
I have this very simple example that I am using to learn structs in
I'm trying to persist a very simple class using Hibernate. I'm not using Spring,
Trying to do a very simple form in Spring/Hibernate. It should be adding an
I have this very simple br2nl function that I use to take a string
I know this might be very easy to some,, I have a simple string
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
I have this very simple jQuery function: $(.milestone-in-tree).live({ mouseenter: function() { setTimeout( $.ajax({ type:
So I made this (very simple) program with a swing GUI with NetBeans, and
I wrote this very simple macro to delete all rows when Column P has

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.