@Repository
public class userDaoImpl implements userDao {
@PersistenceContext(unitName="usersEntity")
private EntityManager em;
@Override
@Transactional
public void userAdd(usersEntity user) {
em.persist(user);
}
求大家给我看看,我是新人,使用Spring +jpa hibernate整合数据库持久化时,不能把数据insert进数据库中,也不抛异常啊
我的user有个id是数据库自增序列,insert的时候,em.persist(user) user.id输出会是null,会影响吗?如何解决?
我听别人说加个EntityManager.flush();如下:@Repository
public class userDaoImpl implements userDao {
@PersistenceContext(unitName="usersEntity")
private EntityManager em;
@Override
@Transactional
public void userAdd(usersEntity user) {
em.persist(user);
em.flush();
}
这时候会抛异常:javax.persistence.TransactionRequiredException: no transaction is in progress
我的applicationContext.xml<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" ></property>
<property name="url" value="jdbc:mysql://localhost:3306/dzb"></property>
<property name="username" value="root"></property>
<property name="password" value="dzb1"></property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
<property name="persistenceUnitName" value="usersEntity" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory">
<ref bean="entityManagerFactory"/>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
快崩溃了,大神们,走过路过,快点帮忙看看,小弟感激不尽!
解决方案 »
- 浏览器和服务器是否连接?怎么判断?
- spring中注入的bean必须要实现接口?
- rollback执行了,但数据还是被修改了~~~请帮忙
- 编写JSP代码时如何使之能给出帮助提示?求助
- struts数据源问题--在struts-config.xml中配置的数据源如何在非action对象中使用?
- 请教一个问题,希望帮帮忙。
- 从properties文件中读出来的中文值取出后怎么跟原来的值不等了,大家帮忙呀!
- weblogic6中如何更新一个已发布的web application (abc.war)中的部分文件(如某个.jsp的文件)
- 关于unicode和gb的问题,虽然我知道如何去做,可是我总想知道为什么这么做!! help
- JSP页面加载时自动执行action
- amcharts JS版,折线图如何添加鼠标单击事件,就是在线上点击一下,弹出改点对应的标题或者值
- 【更新】 对java中一些关键的,容易迷惑的知识点的归纳总结, 请朋友们指正,完善, 一同交流 ,我会持续更新
用save()吧。