Bean named 'userService' is expected to be of type 'demo.spring.orm.userService.UserService' but was actually of type 'com.sun.proxy.$Proxy20'

Viewed 208

I started with spring ORM + Hibernate tutorials. While running the main.java, getting bellow exception and since exception does not have more details about the error. I am not able to figure out the issue. Could please someone can help where is an issue in my code.

config.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:p="http://www.springframework.org/schema/p"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/util
 http://www.springframework.org/schema/util/spring-util.xsd
  http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx.xsd
 ">
 
 <context:component-scan base-package="demo.spring.orm.userService.userEntity" />
 <context:component-scan base-package="demo.spring.orm.userService.UserService" />
 <context:annotation-config />
 <tx:annotation-driven/>
 <tx:annotation-driven transaction-manager="transactionManager" />
 
 <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="ds">
   <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
   <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=pathDB_New" />
   <property name="username" value="sa" />
   <property name="password" value="arst@dm1n" />
 </bean>
 
 <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" name="lsfb">
     <property name="dataSource" ref="ds"></property>
     <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
         </props>
     </property>
     <property name="annotatedClasses">
         <list>
            <value>demo.spring.orm.userEntity.User</value>
         </list>
     </property>
  </bean>
 
  <bean class="org.springframework.orm.hibernate5.HibernateTemplate" name="ht">
      <property name="sessionFactory" ref="lsfb"></property>
  </bean>
 
  <bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" name="transactionManager">
    <property name="sessionFactory" ref="lsfb"></property>
  </bean>
 
  <bean class="demo.spring.orm.userService.UserService" name="userService">
     <property name="hibernateTemplate" ref="ht"></property>
  </bean>
</beans>

UserService.java

package demo.spring.orm.userService;

import javax.transaction.Transactional;
import org.springframework.orm.hibernate5.HibernateTemplate;
import demo.spring.orm.userDao.UserDao;
import demo.spring.orm.userEntity.User;

public class UserService implements UserDao{

    public HibernateTemplate hibernateTemplate;
    
    @Transactional
    public void add(User user) {
        hibernateTemplate.save(user);
    }

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }
}

User.java

package demo.spring.orm.userEntity;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class User {
      
      @Id
      private int userId;
      private String userName;
      private String email;
      private String title;
    
      public User() {
        super();
        // TODO Auto-generated constructor stub
      }

     public User(int userId, String userName, String email, String title) {
        super();
        this.userId = userId;
        this.userName = userName;
        this.email = email;
        this.title = title;
     }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }  
}

main.java

package demo.spring.orm;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import demo.spring.orm.userEntity.User;
import demo.spring.orm.userService.UserService;

public class main {
   public static void main(String args[]) 
   {
       ApplicationContext context= new ClassPathXmlApplicationContext("config.xml");
       UserService userService=context.getBean("userService",UserService.class);
       User user=new User(2,"Arunendra","mail@mail.com","Software Engineer");
       userService.add(user);
   }
}

Exception:

Jul 27, 2021 8:48:39 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 5.4.30.Final
Jul 27, 2021 8:48:40 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
Jul 27, 2021 8:48:44 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
Hibernate: create table User (userId int not null, email varchar(255), title varchar(255), userName varchar(255), primary key (userId))
Jul 27, 2021 8:48:46 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
WARN: GenerationTarget encountered exception accepting command : Error executing DDL "create table User (userId int not null, email varchar(255), title varchar(255), userName varchar(255), primary key (userId))" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table User (userId int not null, email varchar(255), title varchar(255), userName varchar(255), primary key (userId))" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.createTable(AbstractSchemaMigrator.java:277)
    at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:71)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:207)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:318)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:616)
    at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:600)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:860)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
    at demo.spring.orm.main.main(main.java:11)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'User'.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1632)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:872)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:767)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7225)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:3053)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:247)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:222)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.execute(SQLServerStatement.java:743)
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
    ... 28 more

Jul 27, 2021 8:48:46 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userService' is expected to be of type 'demo.spring.orm.userService.UserService' but was actually of type 'com.sun.proxy.$Proxy20'
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:395)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1114)
    at demo.spring.orm.main.main(main.java:12)
1 Answers

You can solve that problem by enabling CGLIB proxy, add proxy-target-class attribute in your spring context :

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
Related