getHibernateTemplate().save(transientInstance) 不能写入数据;
看了网上很多的类似问题,都不能解决啊。我的hibernate.cfg.xml配置如下:<session-factory><propertyname="connection...
看了网上很多的类似问题,都不能解决啊。
我的hibernate.cfg.xml配置如下:
<session-factory>
<property name="connection.username">classiccars</property>
<property name="connection.url">
jdbc:derby://localhost:1527/myeclipse
</property>
<property name="dialect">
org.hibernate.dialect.DerbyDialect
</property>
<property name="myeclipse.connection.profile">
MyEclipse Derby
</property>
<property name="connection.password">myeclipse</property>
<property name="connection.driver_class">
org.apache.derby.jdbc.ClientDriver
</property>
<property name="connection.autocommit">true</property>
<property name="show_sql">true</property>
<mapping resource="com/fhh/vo/UserInfo.hbm.xml" />
</session-factory>
applicationContext.xml配置如下:
<!-- sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 实体层类 -->
<bean id="userinfo" class="com.fhh.vo.UserInfo"></bean>
<!-- dao层类 -->
<bean id="userdao" class="com.fhh.daoImpl.UserInfoDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 服务层service -->
<bean id="userinfoService" class="com.fhh.serviceImpl.UserinfoServiceImpl">
<property name="userdao" ref="userdao"></property>
</bean>
<!-- 声明式事务管理开始 (将sessionFactory交给事物管理器管理)-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- aop哪些类使用 -->
<aop:config>
<aop:pointcut id="serviceMethods" expression="execution(* com.fhh.service.UserinfoService.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
</beans>
daoImpl:
public void save(UserInfo transientInstance) {
try {
getHibernateTemplate().save(transientInstance);
} catch (RuntimeException re) {
}
}
自己做的一个测试
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext
("applicationContext.xml");
UserInfo us=(UserInfo)app.getBean("userinfo");
UserinfoService usinfo=(UserinfoService)app.getBean("userinfoService");
try{
us.setEmail("123456@qq.com");
us.setPassword("123");
us.setUsername("小组");
us.setMoney(30);
usinfo.saveUser(us);
System.out.println("保存成功!");
}catch(Exception e){
e.printStackTrace();
}
}
现在的主要问题是:他save的时候没有insert语句,但是有Hibernate: select max(ID) from USER_INFO。 展开
我的hibernate.cfg.xml配置如下:
<session-factory>
<property name="connection.username">classiccars</property>
<property name="connection.url">
jdbc:derby://localhost:1527/myeclipse
</property>
<property name="dialect">
org.hibernate.dialect.DerbyDialect
</property>
<property name="myeclipse.connection.profile">
MyEclipse Derby
</property>
<property name="connection.password">myeclipse</property>
<property name="connection.driver_class">
org.apache.derby.jdbc.ClientDriver
</property>
<property name="connection.autocommit">true</property>
<property name="show_sql">true</property>
<mapping resource="com/fhh/vo/UserInfo.hbm.xml" />
</session-factory>
applicationContext.xml配置如下:
<!-- sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 实体层类 -->
<bean id="userinfo" class="com.fhh.vo.UserInfo"></bean>
<!-- dao层类 -->
<bean id="userdao" class="com.fhh.daoImpl.UserInfoDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 服务层service -->
<bean id="userinfoService" class="com.fhh.serviceImpl.UserinfoServiceImpl">
<property name="userdao" ref="userdao"></property>
</bean>
<!-- 声明式事务管理开始 (将sessionFactory交给事物管理器管理)-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- aop哪些类使用 -->
<aop:config>
<aop:pointcut id="serviceMethods" expression="execution(* com.fhh.service.UserinfoService.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
</beans>
daoImpl:
public void save(UserInfo transientInstance) {
try {
getHibernateTemplate().save(transientInstance);
} catch (RuntimeException re) {
}
}
自己做的一个测试
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext
("applicationContext.xml");
UserInfo us=(UserInfo)app.getBean("userinfo");
UserinfoService usinfo=(UserinfoService)app.getBean("userinfoService");
try{
us.setEmail("123456@qq.com");
us.setPassword("123");
us.setUsername("小组");
us.setMoney(30);
usinfo.saveUser(us);
System.out.println("保存成功!");
}catch(Exception e){
e.printStackTrace();
}
}
现在的主要问题是:他save的时候没有insert语句,但是有Hibernate: select max(ID) from USER_INFO。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询