急~~~new一个实体对象时自增长的Id为0而不是Null.为什么? 本帖最后由 xiechunmei13 于 2009-08-26 10:50:40 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 有人说是与事务有关。相关配置。 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>${db.driver}</value> </property> <property name="url"> <value>${db.url}</value> </property> <property name="username"> <value>${db.username}</value> </property> <property name="password"> <value>${db.password}</value> </property> <property name="initialSize"> <value>${db.pool.initial}</value> </property> <property name="maxActive"> <value>${db.pool.max}</value> </property> <property name="defaultAutoCommit" value="false" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" /> <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="2" /> <property name="keepAliveSeconds" value="200" /> <property name="maxPoolSize" value="10" /> <property name="queueCapacity" value="60" /> </bean> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> </bean> 那得看你的数据库id字段是自增长的?如果是,不要在SQL里面包含ID,那是会出异常的。 new一个对象时ID都是0的 因为程序里面ID是整型的 而到数据库里面之后 这个值会被重新定义 根据数据库的自增类型 当然了 不同数据库自增的时候在映射文件里面设置的generator是不一样的 ORACLE还要创建一个SEQUENCE 这是完整的配置文件。我觉得像是事务没有提交还是回滚了?我现在还是没找出原因。<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="db-properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:ignoreUnresolvablePlaceholders="true"> <property name="locations"> <list> <value>classpath:DataBase.properties</value> </list> </property> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>${db.driver}</value> </property> <property name="url"> <value>${db.url}</value> </property> <property name="username"> <value>${db.username}</value> </property> <property name="password"> <value>${db.password}</value> </property> <property name="initialSize"> <value>${db.pool.initial}</value> </property> <property name="maxActive"> <value>${db.pool.max}</value> </property> <property name="defaultAutoCommit" value="false" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" /> <bean id="userDao" class="com.sns.bestv.dao.impl.VisUsersDaoImpl" p:dataSource-ref="dataSource"/> <bean id="customerDao" class="com.sns.bestv.dao.impl.VisCustomerDaoImpl" p:dataSource-ref="dataSource"/> <bean id="custUserDao" class="com.sns.bestv.dao.impl.VisCustomerUsersDaoImpl" p:dataSource-ref="dataSource"/> <bean id="aipUsersDao" class="com.sns.bestv.dao.impl.VisApiUsersDaoImpl" p:dataSource-ref="dataSource"/> <bean id="aipLogDao" class="com.sns.bestv.dao.impl.VisApiLogDaoImpl" p:dataSource-ref="dataSource"/> <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="2" /> <property name="keepAliveSeconds" value="200" /> <property name="maxPoolSize" value="10" /> <property name="queueCapacity" value="60" /> </bean> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> </bean> </beans> 如果没有使用hibernate,可以直接将数据库中的id修改为自增类型如果采用了hibernate,那么需要在VisCustomerUsers对应的hbm.xml文件中设置id的生成策略为increment,或将id的生成策略改为native并将数据库中的id设置为自增类型 问题已解决。附上解决方案。 数据库中创建表时 表类型为InnoDB.改成MyISAM就可以 了。 只有id为封装类时才是null(如Integer)当为int时为0. sax解析xml的问题 程序开发过程中类中使用静态方法好还是非静态方法好? 装了Tomcat 5.5.26,jsp文件应该放哪里?? 如何同时执行两条SQL语句 Iframe中获得JSP中数据 有关面部识别的东西,问下大家 Struts中的几个问题 加急!!!!!!jsp 怎么获取公网ip!!!! 有用struts连接数据库的例子吗? Can't contact Servlet Runner at localhost:6802 为什么会这样? 在客户端打印Excel!!!!!!!! 如何在光标处显示表格中某条记录的相关信息?
<property name="driverClassName">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
<property name="initialSize">
<value>${db.pool.initial}</value>
</property>
<property name="maxActive">
<value>${db.pool.max}</value>
</property>
<property name="defaultAutoCommit" value="false" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />
<bean id="threadPoolTaskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="2" />
<property name="keepAliveSeconds" value="200" />
<property name="maxPoolSize" value="10" />
<property name="queueCapacity" value="60" />
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
</bean>
如果是,不要在SQL里面包含ID,那是会出异常的。
我觉得像是事务没有提交还是回滚了?
我现在还是没找出原因。
<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="db-properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:ignoreUnresolvablePlaceholders="true">
<property name="locations">
<list>
<value>classpath:DataBase.properties</value>
</list>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
<property name="initialSize">
<value>${db.pool.initial}</value>
</property>
<property name="maxActive">
<value>${db.pool.max}</value>
</property>
<property name="defaultAutoCommit" value="false" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />
<bean id="userDao" class="com.sns.bestv.dao.impl.VisUsersDaoImpl" p:dataSource-ref="dataSource"/>
<bean id="customerDao" class="com.sns.bestv.dao.impl.VisCustomerDaoImpl" p:dataSource-ref="dataSource"/>
<bean id="custUserDao" class="com.sns.bestv.dao.impl.VisCustomerUsersDaoImpl" p:dataSource-ref="dataSource"/>
<bean id="aipUsersDao" class="com.sns.bestv.dao.impl.VisApiUsersDaoImpl" p:dataSource-ref="dataSource"/>
<bean id="aipLogDao" class="com.sns.bestv.dao.impl.VisApiLogDaoImpl" p:dataSource-ref="dataSource"/> <bean id="threadPoolTaskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="2" />
<property name="keepAliveSeconds" value="200" />
<property name="maxPoolSize" value="10" />
<property name="queueCapacity" value="60" />
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
</bean>
</beans>
附上解决方案。 数据库中创建表时 表类型为InnoDB.改成MyISAM就可以 了。
当为int时为0.