编程语言
首页 > 编程语言> > java – 对hibernate应用程序使用@Transactional注释导致错误

java – 对hibernate应用程序使用@Transactional注释导致错误

作者:互联网

我想在今天早上将春天与冬眠融为一体.我想使用spring transaction manager.但得到以下错误.该错误与@Trasactional Annotation有关.如果我删除注释,我能够从弹簧容器中获取bean.

Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy19 cannot be cast to com.hibernate.dao.EvntDAOImpl
at com.hibernate.action.HibernateAction.main(HibernateAction.java:17)

我粘贴在我的源代码下面.

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hibernate-tutorials</groupId>
<artifactId>hibernate-tutorials</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<properties>
    <spring-framework.version>4.0.3.RELEASE</spring-framework.version>
    <hibernate.version>4.3.5.Final</hibernate.version>
     <slf4j.version>1.7.5</slf4j.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- Spring ORM support -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- Logging with SLF4J & LogBack -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <version>10.2.0.2.0</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
</dependencies>

休眠配置

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd“>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="${url}" />
    <property name="username" value="${username}" />
    <property name="password" value="${password}" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.hibernate.model.Evnt</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernatey.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

EvntDAOImpl.java

package com.hibernate.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.hibernate.model.Evnt;

@Repository(value="evntDAO")
@Transactional
public class EvntDAOImpl implements EvntDAO {

@Autowired
private SessionFactory sessionFactory;

@Override
public void storeEvnt(Evnt evnt) {

    sessionFactory.getCurrentSession().save(evnt);

}

}

HibernateAction.java

package com.hibernate.action;

import java.util.Date;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hibernate.dao.EvntDAO;
import com.hibernate.dao.EvntDAOImpl;
import com.hibernate.model.Evnt;

public class HibernateAction {

public static void main(String[] args) {
    ApplicationContext context  = new         ClassPathXmlApplicationContext("applicationContext.xml");

    EvntDAOImpl evntDAO = (EvntDAOImpl) context.getBean("evntDAO");
    Evnt evnt = new Evnt();
    evnt.setTitle("first Event");
    evnt.setDate(new Date());
    evntDAO.storeEvnt(evnt);

    }
}

提前致谢…

解决方法:

问题出在你注入EventDaoImpl的地方.

只需更换

@Autowired EventDaoImpl eventDaoImpl

@Autowired EventDao eventDao

无论你需要Spring,都要自动装配dao.

或者,如果要从applicationContext获取bean,请使用:

EvntDAO evntDAO =(EvntDAO)context.getBean(“evntDAO”);

问题是由于在dao的实现代码上使用@Transactional注释意味着Spring将为它创建一个JDK dynamic proxy,而不能将其强制转换为实现类. Here是Spring的AOP功能的完整文档(其中完整地解释了JDK动态代理和CGLib类代理的创建).

本质上意味着因为@Transactional,当你调用context.getBean(“evntDAO”)时,你没有得到你的EventDaoImpl(正如人们所期望的那样),但实际上你得到了一个类为java.lang的对象. .ref创建的.reflect.Proxy.该代理对象实现了EventDao(因此可以强制转换为它),但它的签名与EventDaoImpl无关(因此它不会扩展EventDaoImpl,因此也是ClassCastException).当在代理上调用方法时,在调用实际委托给EventDaoImpl之前和/或之后发生了各种事情(事件调用之前/之后或之后发生的事情是由Spring控制的,尽管是InvocationHandler的实现).

标签:spring-transactions,java,spring,hibernate
来源: https://codeday.me/bug/20190830/1768274.html