当前位置:编程学习 > JAVA >>

hibernate 的save方法执行 数据不进数据库也不报错

这是测试代码
public class Test {
public static void main(String[] args) throws SQLException {
BeanFactory bf = new ClassPathXmlApplicationContext("spring/spring-core.xml");
UserinfoService us = (UserinfoService)bf.getBean("userinfoServiceImpl");
Userinfo userinfo = new Userinfo();
userinfo.setId(4);
userinfo.setUsername("ZY");
userinfo.setPassword("ZY");
us.saveUserinfo(userinfo);

}
}


spring 数据配置

<?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: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/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">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="net.sourceforge.jdbclogger.JdbcLoggerDriver"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxIdle" value="${maxIdle}"/>
<property name="minIdle" value="${minIdle}"/>
<property name="logAbandoned" value="${logAbandoned}"/>
<property name="removeAbandoned" value="${removeAbandoned}"/>
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
<property name="maxWait" value="${maxWait}"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
</value>
</property>
<property name="mappingResources">
<list>
<value>mappings/UserInfo.hbm.xml</value>
</list>
</property>
</bean>

<bean  id="genericDao" class="com.test.core.GenericDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!--数据访问 -->
<bean id="userDao" class="com.test.dao.UserDao" parent="genericDao" autowire="byType"></bean>
<!--服务接口实现-->
<bean id="userinfoServiceImpl" class="com.test.service.impl.UserinfoServiceImpl"  autowire="byType"></bean>
</beans>

2012-08-07 17:14:18,465 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@bfbdb0: display name [org.springframework.context.support.ClassPathXmlApplicationContext@bfbdb0]; startup date [Tue Aug 07 17:14:18 CST 2012]; root of context hierarchy
2012-08-07 17:14:18,528 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [spring/spring-core.xml]
2012-08-07 17:14:18,653 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@bfbdb0]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1b9240e
2012-08-07 17:14:18,699 INFO [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] - Loading properties file from class path resource [jdbc.properties]
2012-08-07 17:14:18,715 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b9240e: defining beans [propertyConfigurer,dataSource,sessionFactory,genericDao,userDao,userinfoServiceImpl]; root of factory hierarchy
2012-08-07 17:14:18,762 INFO [org.hibernate.cfg.Environment] - Hibernate 3.1.3
2012-08-07 17:14:18,778 INFO [org.hibernate.cfg.Environment] - hibernate.properties not found
2012-08-07 17:14:18,778 INFO [org.hibernate.cfg.Environment] - using CGLIB reflection optimizer
2012-08-07 17:14:18,778 INFO [org.hibernate.cfg.Environment] - using JDK 1.4 java.sql.Timestamp handling
2012-08-07 17:14:22,574 INFO [org.hibernate.cfg.HbmBinder] - Mapping class: com.test.entity.Userinfo -> b_userinfo
2012-08-07 17:14:22,590 INFO [org.springframework.orm.hibernate3.LocalSessionFactoryBean] - Building new Hibernate SessionFactory
2012-08-07 17:14:22,637 INFO [org.hibernate.connection.ConnectionProviderFactory] - Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
AbandonedObjectPool is used (org.apache.commons.dbcp.AbandonedObjectPool@ec6b00)
   LogAbandoned: true
   RemoveAbandoned: true
   RemoveAbandonedTimeout: 20
2012-08-07 17:14:23,153 INFO [org.hibernate.cfg.SettingsFactory] - RDBMS: MySQL, version: 5.1.51-community
2012-08-07 17:14:23,153 INFO [org.hibernate.cfg.SettingsFactory] - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.5 ( Revision: ${svn.Revision} )
2012-08-07 17:14:23,168 INFO [org.hibernate.dialect.Dialect] - Using dialect: org.hibernate.dialect.MySQLDialect
2012-08-07 17:14:23,168 INFO [org.hibernate.transaction.TransactionFactoryFactory] - Transaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory
2012-08-07 17:14:23,168 INFO [org.hibernate.transaction.TransactionManagerLookupFactory] - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
2012-08-07 17:14:23,168 INFO [org.hibernate.cfg.SettingsFactory] - Automatic flush during beforeCompletion(): disabled
2012-08-07 17:14:23,168 INFO [org.hibernate.cfg.SettingsFactory] - Automatic session close at end of transaction: disabled
2012-08-07 17:14:23,168 INFO [org.hibernate.cfg.SettingsFactory] - JDBC batch size: 15
2012-08-07 17:14:23,168 INFO [org.hibernate.cfg.SettingsFactory] - JDBC batch updates for versioned data: disabled
2012-08-07 17:14:23,168 INFO [org.hibernate.cfg.SettingsFactory] - Scrollable result sets: enabled
2012-08-07 17:14:23,168 INFO [org.hibernate.cfg.SettingsFactory] - JDBC3 getGeneratedKeys(): enabled
2012-08-07 17:14:23,168 INFO [org.hibernate.cfg.SettingsFactory] - Connection release mode: auto
2012-08-07 17:14:23,168 INFO [org.hibernate.cfg.SettingsFactory] - Maximum outer join fetch depth: 2
2012-08-07 17:14:23,168 INFO [org.hibernate.cfg.SettingsFactory] - Default batch fetch size: 1
2012-08-07 17:14:23,168 INFO [org.hibernate.cfg.SettingsFactory] - Generate SQL with comments: disabled
2012-08-07 17:14:23,168 INFO [org.hibernate.cfg.SettingsFactory] - Order SQL updates by primary key: disabled
2012-08-07 17:14:23,168 INFO [org.hibernate.cfg.SettingsFactory] - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
2012-08-07 17:14:23,184 INFO [org.hibernate.hql.ast.ASTQueryTranslatorFactory] - Using ASTQueryTranslatorFactory
2012-08-07 17:14:23,184 INFO [org.hibernate.cfg.SettingsFactory] - Query language substitutions: {}
2012-08-07 17:14:23,184 INFO [org.hibernate.cfg.SettingsFactory] - Second-level cache: enabled
2012-08-07 17:14:23,184 INFO [org.hibernate.cfg.SettingsFactory] - Query cache: disabled
2012-08-07 17:14:23,184 INFO [org.hibernate.cfg.SettingsFactory] - Cache provider: org.hibernate.cache.EhCacheProvider
2012-08-07 17:14:23,184 INFO [org.hibernate.cfg.SettingsFactory] - Optimize cache for minimal puts: disabled
2012-08-07 17:14:23,184 INFO [org.hibernate.cfg.SettingsFactory] - Structured second-level cache entries: disabled
2012-08-07 17:14:23,184 INFO [org.hibernate.cfg.SettingsFactory] - Echoing all SQL to stdout
2012-08-07 17:14:23,184 INFO [org.hibernate.cfg.SettingsFactory] - Statistics: disabled
2012-08-07 17:14:23,184 INFO [org.hibernate.cfg.SettingsFactory] - Deleted entity synthetic identifier rollback: disabled
2012-08-07 17:14:23,184 INFO [org.hibernate.cfg.SettingsFactory] - Default entity-mode: pojo
2012-08-07 17:14:23,199 INFO [org.hibernate.impl.SessionFactoryImpl] - building session factory
2012-08-07 17:14:23,418 INFO [org.hibernate.impl.SessionFactoryObjectFactory] - Not binding factory to JNDI, no JNDI name configured



执行test 的时候不报错 但是数据也不进入数据库 是hibernate的几种状态问题 还是配置问题 麻烦各位大神看看! --------------------编程问答-------------------- 我觉得是没有配置事务管理器和事务拦截器。。 --------------------编程问答--------------------

<!--事务管理 -->
<!-- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">-->
<!-- <property name="sessionFactory" ref="sessionFactory"></property>-->
<!-- </bean>-->
<!-- <aop:config proxy-target-class="true">-->
<!-- <aop:pointcut expression="execution(* com.test.service.*.*(..))" id="method"/>-->
<!-- <aop:advisor advice-ref="txAdvice" pointcut-ref="method"/>-->
<!-- </aop:config>-->
<!-- <tx:advice id="txAdvice">-->
<!-- <tx:attributes>-->
<!-- <tx:method name="*" propagation="REQUIRED"/>-->
<!-- <tx:method name="update*" propagation="REQUIRED"/>-->
<!-- <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true"/>-->
<!-- </tx:attributes>-->
<!-- </tx:advice>-->
--------------------编程问答-------------------- 上边就是我的事物管理  加上也不行的! --------------------编程问答-------------------- hibernate的话测试建议用@test  --------------------编程问答-------------------- 把事务代码注释掉也不行吗,debug一下看看有没有存进去
--------------------编程问答-------------------- beanfactory换成ApplicationContext试试 --------------------编程问答-------------------- 楼主,有解决么? --------------------编程问答-------------------- Transaction --------------------编程问答-------------------- 是不是把事务的transaction的属性readonly设置为true了
--------------------编程问答-------------------- 这个问题解决了吗?
我也遇到这问题了 --------------------编程问答-------------------- 457945927 这是我qq号 如果您有时间的话 给我说说怎么解决的 --------------------编程问答-------------------- 楼主事务的配置那块有问题吧?
<tx:method name="*" propagation="REQUIRED"/>
<tx:method name="*" propagation="NOT_SUPPORTED" read-only="true"/>
两个都是name = "*" ?
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,