SSH集成后,测试DAO,发出hibernate的insert,就是不保存到数据库

1.UserDAOImplTest
[code="java"]
public class UserDAOImplTest extends TestCase {

private BeanFactory factory = 
                            new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext-*.xml");
private UserDAO userDAO = (UserDAOImpl)factory.getBean("userDAO");

@Override
protected void setUp() throws Exception {


}

public void testAddUser() {

    User user = new User();

    user.setUserId("d3e3");
    user.setUserName("张三");
    user.setEmail("ddf@134.com");
    user.setContactTel("324e32r4");
    user.setCreateDate(new Date());
    user.setPassword("java");

    userDAO.addUser(user);


}

}
[/code]

2.consle的信息:
[code="java"]
2009-06-13 21:19:58,343 INFO [org.springframework.context.support.FileSystemXmlApplicationContext] - Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1b9240e: display name [org.springframework.context.support.FileSystemXmlApplicationContext@1b9240e]; startup date [Sat Jun 13 21:19:58 CST 2009]; root of context hierarchy
2009-06-13 21:19:58,437 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\Struts2_CSincity\DRP_SSH_1.0\WebRoot\WEB-INF\applicationContext-actions.xml]
2009-06-13 21:19:59,109 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\Struts2_CSincity\DRP_SSH_1.0\WebRoot\WEB-INF\applicationContext-beans.xml]
2009-06-13 21:19:59,187 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\Struts2_CSincity\DRP_SSH_1.0\WebRoot\WEB-INF\applicationContext-common.xml]
2009-06-13 21:19:59,296 INFO [org.springframework.context.support.FileSystemXmlApplicationContext] - Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@1b9240e]: org.springframework.beans.factory.support.DefaultListableBeanFactory@f6ac0b
2009-06-13 21:19:59,390 INFO [org.springframework.context.support.FileSystemXmlApplicationContext] - Bean 'org.springframework.aop.config.internalAutoProxyCreator' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2009-06-13 21:19:59,390 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@f6ac0b: defining beans [/user,userDAO,userManager,sessionFactory,transactionManager,txAdvice,org.springframework.aop.config.internalAutoProxyCreator,allManagerMethod,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor]; root of factory hierarchy
2009-06-13 21:19:59,500 INFO [org.hibernate.cfg.Environment] - Hibernate 3.1.3
2009-06-13 21:19:59,500 INFO [org.hibernate.cfg.Environment] - hibernate.properties not found
2009-06-13 21:19:59,515 INFO [org.hibernate.cfg.Environment] - using CGLIB reflection optimizer
2009-06-13 21:19:59,515 INFO [org.hibernate.cfg.Environment] - using JDK 1.4 java.sql.Timestamp handling
2009-06-13 21:19:59,625 INFO [org.hibernate.cfg.Configuration] - configuring from url: file:/D:/Struts2_CSincity/DRP_SSH_1.0/WebRoot/WEB-INF/classes/hibernate.cfg.xml
2009-06-13 21:19:59,734 INFO [org.hibernate.cfg.Configuration] - Reading mappings from resource: com/drp/model/User.hbm.xml
2009-06-13 21:19:59,859 INFO [org.hibernate.cfg.HbmBinder] - Mapping class: com.drp.model.User -> t_user
2009-06-13 21:19:59,890 INFO [org.hibernate.cfg.Configuration] - Configured SessionFactory: null
2009-06-13 21:19:59,890 INFO [org.springframework.orm.hibernate3.LocalSessionFactoryBean] - Building new Hibernate SessionFactory
2009-06-13 21:19:59,968 INFO [org.hibernate.connection.DriverManagerConnectionProvider] - Using Hibernate built-in connection pool (not for production use!)
2009-06-13 21:19:59,968 INFO [org.hibernate.connection.DriverManagerConnectionProvider] - Hibernate connection pool size: 20
2009-06-13 21:19:59,968 INFO [org.hibernate.connection.DriverManagerConnectionProvider] - autocommit mode: false
2009-06-13 21:19:59,984 INFO [org.hibernate.connection.DriverManagerConnectionProvider] - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/mydrp
2009-06-13 21:19:59,984 INFO [org.hibernate.connection.DriverManagerConnectionProvider] - connection properties: {user=root, password=****, release_mode=on_close}
2009-06-13 21:20:00,312 INFO [org.hibernate.cfg.SettingsFactory] - RDBMS: MySQL, version: 5.0.45-community-nt
2009-06-13 21:20:00,312 INFO [org.hibernate.cfg.SettingsFactory] - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.5 ( Revision: ${svn.Revision} )
2009-06-13 21:20:00,343 INFO [org.hibernate.dialect.Dialect] - Using dialect: org.hibernate.dialect.MySQLDialect
2009-06-13 21:20:00,343 INFO [org.hibernate.transaction.TransactionFactoryFactory] - Using default transaction strategy (direct JDBC transactions)
2009-06-13 21:20:00,343 INFO [org.hibernate.transaction.TransactionManagerLookupFactory] - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
2009-06-13 21:20:00,343 INFO [org.hibernate.cfg.SettingsFactory] - Automatic flush during beforeCompletion(): disabled
2009-06-13 21:20:00,343 INFO [org.hibernate.cfg.SettingsFactory] - Automatic session close at end of transaction: disabled
2009-06-13 21:20:00,343 INFO [org.hibernate.cfg.SettingsFactory] - JDBC batch size: 15
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - JDBC batch updates for versioned data: disabled
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - Scrollable result sets: enabled
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - JDBC3 getGeneratedKeys(): enabled
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - Connection release mode: on_close
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - Maximum outer join fetch depth: 2
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - Default batch fetch size: 1
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - Generate SQL with comments: disabled
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - Order SQL updates by primary key: disabled
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
2009-06-13 21:20:00,359 INFO [org.hibernate.hql.ast.ASTQueryTranslatorFactory] - Using ASTQueryTranslatorFactory
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - Query language substitutions: {}
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - Second-level cache: enabled
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - Query cache: disabled
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - Cache provider: org.hibernate.cache.EhCacheProvider
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - Optimize cache for minimal puts: disabled
2009-06-13 21:20:00,359 INFO [org.hibernate.cfg.SettingsFactory] - Structured second-level cache entries: disabled
2009-06-13 21:20:00,375 INFO [org.hibernate.cfg.SettingsFactory] - Echoing all SQL to stdout
2009-06-13 21:20:00,375 INFO [org.hibernate.cfg.SettingsFactory] - Statistics: disabled
2009-06-13 21:20:00,375 INFO [org.hibernate.cfg.SettingsFactory] - Deleted entity synthetic identifier rollback: disabled
2009-06-13 21:20:00,375 INFO [org.hibernate.cfg.SettingsFactory] - Default entity-mode: pojo
2009-06-13 21:20:00,406 INFO [org.hibernate.impl.SessionFactoryImpl] - building session factory
2009-06-13 21:20:00,875 INFO [org.hibernate.impl.SessionFactoryObjectFactory] - Not binding factory to JNDI, no JNDI name configured
2009-06-13 21:20:00,890 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - Running hbm2ddl schema update
2009-06-13 21:20:00,890 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - fetching database metadata
2009-06-13 21:20:00,890 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - updating schema
2009-06-13 21:20:00,921 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - table found: mydrp.t_user
2009-06-13 21:20:00,921 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - columns: [password, contacttel, userid, createdate, username, email, id]
2009-06-13 21:20:00,921 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - foreign keys: []
2009-06-13 21:20:00,921 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - indexes: [primary]
2009-06-13 21:20:00,921 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - schema update complete
Hibernate: insert into t_user (userId, userName, password, email, createDate, contactTel) values (?, ?, ?, ?, ?, ?)
[/code]

最后发出insert了,数据库里就是没有数据,那位高人知道原因,

项目里添加用户完全没问题,就是测试没行,郁闷,请问的我的错误在哪里?

[b]问题补充:[/b]
谢谢各位热心帮忙

我加入了事务管理,获得dao对象时,报 java.lang.ClassCastException: $Proxy1

我的事务管理如下:
[code="java"]
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
value="classpath:hibernate.cfg.xml">

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="add*" propagation="REQUIRED"/>
        <tx:method name="del*" propagation="REQUIRED"/>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="test*" propagation="REQUIRED"/>

        <!-- 只读事务不进行脏数据检查,可提高性能 -->
        <tx:method name="*" read-only="true"/>
    </tx:attributes>
</tx:advice>

<!-- 哪些类那些方法是用事务,事务的边界定位的业务逻辑层 -->
<aop:config>
    <aop:pointcut id="allManagerMethod" expression="execution(* com.drp.manager.*.*(..))"/>
    <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>

</aop:config>

     <aop:config>
    <aop:pointcut id="allDaoMethod" expression="execution(* com.drp.dao.*.*(..))"/>
    <aop:advisor pointcut-ref="allDaoMethod" advice-ref="txAdvice"/>
</aop:config>

[/code]

加上最后一句aop:config后
private UserDAO userDAO = (UserDAOImpl)factory.getBean("userDAO");
说这行,发生java.lang.ClassCastException: $Proxy1

怎么回事??
[b]问题补充:[/b]
用aop已经生成了动态代理对象,factory.getBean("userDAO")已经不是UserDAOImpl类型了

是代理对象,通过getClass,返回的是$Proxy,那我怎么取得UserDAOImpl对象呢?

是不是没有引入对应的包啊?CGLIB.JAR,ASM-.*.JAR
这两个都加了。

7个回答

LZ看看这篇文章吧,说得很详细
[url]http://blog.csdn.net/pjchen/archive/2009/03/26/4028161.aspx[/url]

没有提交事务吧,还是你数据库数据没更新

autocommit mode: false
Automatic session close at end of transaction: disabled

No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)


貌似是没有提交事务啊,autocommit mode没用啊

注意提交事务

没开启事务。。。。配置下事务。。。。

private UserDAO userDAO = (UserDAOImpl)factory.getBean("userDAO");

当然不能这么强制类型转换

用aop已经生成了动态代理对象,factory.getBean("userDAO")已经不是UserDAOImpl类型了

[quote]发生java.lang.ClassCastException: $Proxy1 [/quote]
是不是没有引入对应的包啊?CGLIB.JAR,ASM-.*.JAR

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问