1.Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误)
spring的文档中这么写的:Spring AOP部分使用JDK动态代理或者CGLIB来为目标对象创建代理,如果被代理的目标对象实现了至少一个接口,则会使用JDK动态代理。所有该目标类型实现的接口都将被代理。若该目标对象没有实现任何接口,则创建一个CGLIB代理。使用beanNameAutoProxyCreator来进行事务代理的话,他的proxyTargetClass这个属性设置为false(默认是false),即使用JDK动态代理,如果你的service类没有实现接口的话,就会报类型转换错误。
解决办法有:
1、给service类添加一个接口iService,让service类实现它,则创建代理类时使用JDK动态代理就不会出现问题
2、设置beanNameAutoProxyCreator的proxyTargetClass属性为true,意思是强制使用CGLIB代理,前提是你已经将CGLIB包加入到项目中
3、 将Hibernate版本由4.3.8换成4.3.7就好了2.hibernate报错:save is not valid without active transaction
Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:341)
at $Proxy16.save(Unknown Source)
配置文件如下:
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >
- <!-- Creates the registry of flow definitions for this application -->
- <!-- 要使用注解必须添加此项 -->
- <context:annotation-config />
- <!-- 自动检测组件
- -->
- <context:component-scan base-package="com.spring.service.impl,com.spring.dao,com.spring.aop" />
- <!-- 配置数据源 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/test" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- </bean>
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
- <!-- 扫描实体包 -->
- <property name="packagesToScan">
- <value>com.spring.domain</value>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
- <prop key="hibernate.show_sql">true</prop>
- <!--
- 加上下面这句话会出错
- Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction
- -->
- <prop key="hibernate.current_session_context_class">thread</prop>
- <prop key="hibernate.hbm2ddl.auto">update</prop>
- </props>
- </property>
- </bean>
- <!-- 设定transactionManager -->
- <bean id="txManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!--启动spring事务注解功能-->
- <tx:annotation-driven transaction-manager="txManager"/>
- </beans>
正如配置文件中红色文字所示,在单独使用hibernate时,总是会加上这项配置,但spring与hibernate结合时,千万不能加上这句话,我猜测出错原因是事务管理器中的session和当前拿到的session不是同一个session,事务管理器中拿到的session开启了事务,但当前得到的session并没有开启事务,导致出错
Hibernate 4.3.5 JPA实现的常见错误
(1)使用Hibernate JPA实现需要添加hibernate lib里面的包之外还要添加一个com.springsources.slf4j.api.jar,否则包错,unable to build entity manager factory
如果使用4.3.5 版本的hibernate,实体类里面不要使用@table(name=”…”),而是要用@entity(name=”...”) 否则报错javax.persistence.Table.indexes([Ljavax/persistence/Index;

(2)Hibernate JPA 不希望实体更新时自动修改数据库表,需要在persistence.xml中加入<property name="hibernate.hbm2ddl.auto" value="none" />
(3)Hibernate4.3.5 jpa 使用manytoone 不要使用 @JoinColumn(name="person_id", nullable=false, updatable=true), 否则会报错javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
(4)Hibernate4.3.5 jpa 使用manytomany,会在数据库里面生成一张关系表
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type
1.如果没有实现接口那么可以用类型拿到。private static UserServiceImpl userService=null;
@BeforeClass
public static void init(){
ctx = new ClassPathXmlApplicationContext(
"/applicationConext.xml");
userService = ctx.getBean(UserServiceImpl.class);
}
private static UserService userService=null;
@BeforeClass
public static void init(){
ctx = new ClassPathXmlApplicationContext(
"/applicationConext.xml");
userService = ctx.getBean(UserService.class);
}

本文探讨了SpringAOP代理时出现的类型转换错误及解决方法,并分析了Hibernate在未激活事务的情况下尝试保存对象引发的异常。同时,还提供了针对Hibernate4.3.5 JPA实现时常见的错误解决方案。

2862

被折叠的 条评论
为什么被折叠?



