- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 spring 和 JPA 构建一个应用程序。我的读取功能正在运行,但是当我尝试保存任何对象时,我收到异常没有可用的事务性EntityManager。
这是我的 web.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml,/WEB-INF/spring/webservices-context.xml,/WEB-INF/spring/security-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<mime-mapping>
<extension>js</extension>
<mime-type>application/x-javascript</mime-type>
</mime-mapping>
<mime-mapping>
<extension>properties</extension>
<mime-type>application/text</mime-type>
</mime-mapping>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这是我的 spring/appServlet/servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<beans:bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<beans:property name="location" value="classpath:resources.properties"></beans:property>
</beans:bean>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/**" location="${resources.base}, ${resources.modules}, ${resources.lib}, ${resources.apps}" /> <!-- resource mapp for extjs and Designer to / -->
<resources mapping="/resources/**" location="${resources.resources}" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.app.webapp" />
</beans:beans>
这是我的 spring/root-context.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<context:annotation-config />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="appDS"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost/schema1"
p:username="user1" p:password="741147">
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="appDS" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<context:spring-configured />
<context:annotation-config />
</beans>
这是我的 persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="myPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.transaction.auto_close_session" value="false"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.generate_statistics" value="false"/>
<property name="hibernate.connection.autocommit" value="true"/>
<!-- <property name="hibernate.hbm2ddl.auto" value="update"/> -->
<property name="hibernate.query.jpaql_strict_compliance" value="false"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
这是我的 DaoImpl.java:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.app.webapp.data.dao.impl.GenericDaoImpl;
import com.app.webapp.data.dao.interfaces.security.IUserDao;
import com.app.webapp.data.domains.entities.Account;
import com.app.webapp.data.domains.entities.security.AccessPrivilege;
import com.app.webapp.data.domains.entities.security.ActiveSession;
import com.app.webapp.data.domains.entities.security.Process;
import com.app.webapp.data.domains.entities.security.Profile;
import com.app.webapp.data.domains.entities.security.User;
import com.app.webapp.data.exceptions.DuplicatedUserException;
import com.app.webapp.data.exceptions.UserAlreadyHasAnAccountException;
import com.app.webapp.data.exceptions.UserNotFoundException;
@Repository("userDao")
@Transactional
public class UserDaoImpl implements IUserDao {
@PersistenceContext(unitName = "myPU")
protected EntityManager entityManager;
public UserDaoImpl() {
super(User.class);
}
@Override
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void save(User user) throws DuplicatedUserException {
logger.info("Creating User with username: " + user.getUsername());
User foundUser = null;
logger.info("Checking if user already existis...");
entityManager.persist(user);
logger.info("User " + user.getUsername() + " was been saved with success!");
}
}
然后执行保留用户的行,我得到了异常:
javax.persistence.TransactionRequiredException: No transactional EntityManager available
但是我不明白为什么我可以从数据库获取数据但无法写入。我不知道 servlet 上下文中是否有任何内容并且不与其他上下文共享。有人可以帮助我吗?
最佳答案
您是否在@Service
或服务层类中提供了@Transactional
注解?如果是,请检查您的 hibernet/entityManager.jar
版本兼容性。
关于Spring - JPA - 读取工作但坚持给我没有可用的事务EntityManager。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27277803/
在我的 POJO 中,我有这个: @Transient private int qtyMentee; 在我的 DAO 中我有这个查询: public List findQtyMentee(){
如果我正在编写的应用程序终止,我需要将当前位置存储到 iphone 的“磁盘”中。然后,当应用程序再次启动时,我想恢复这些信息。但是,CLLocation 坐标属性是只读的。 如何在程序调用之间保存此
这个问题有点来回,因为我已经了解了一些关于 unix 系统的 g++ 知识(抱歉,如果我把任何人搞砸了)。 对于我目前正在尝试完成的项目,我想获得 twitcurl使用 Xcode 和 OpenFra
我想在表格中显示用户的姓名以及本周发布的照片数量。 示例:用户 1 发布了 10 张照片,用户 2 发布了 20 张照片...... 所以我需要一个计数,但我不知道如何。 图片型号: public
我正在尝试保留具有@OneToMany 和@ManyToOne 关系的实体。 @OneToMany(mappedBy="customer", cascade=CascadeType.ALL, fetc
我有一个表单,我从另一个表中获取字段,例如,第一个字段是硬编码的,第二个字段是从另一个表中获取的,如果其他表有 10 条记录,则向用户显示 10 个新字段。我的问题是如何将这种数据插入表中。 我得到这
我一直在努力控制导入和导出,这样我就可以精简我的 JS 应用程序。我试过合并这些 Mozilla和 this Stack Overflow examples没有任何运气。 It looks like
我的数据库中的一个字段中包含以下文本: [quote:5a7b87febe="mr smith"]This is some text. This is more text on another lin
我正在使用 cling UPnP android 中的框架连接到支持 UPnP 的设备。我成功地创建了一个设备并浏览了网络中的可用设备。但对于三星电视在网络框架内没有任何反应。在这里我添加了 Rend
我正在制作一个问答网站,类似于此网站和 Yahoo answers。我有 3 个表 - smf_members、qa_questions 和 qa_answers。 在此查询中,我想从 qa_ques
阅读后this question - 它还提供了文档链接,我仍然对文档有疑问。 MDN:Date.parse A string representing an RFC2822 or ISO 8601
我有一个父实体客户端。该客户可以访问该网站并创建约会。这意味着约会是在客户反对的其他时间创建的。 我的问题是:如何将子对象添加到已持久化的父对象中?如果调用下面示例中的函数 addData1(),则会
我正在尝试创建气泡,重复几次后我的浏览器卡住了。这是我的代码。有人请帮助....我如何在不提出许多请求的情况下完成它。 看起来我的帖子主要是代码,但我为这个 Stackoverflow 添加了足够的细
我被这个 linq 查询困住了,我需要做的就是优化最后的价格计算,因为我得到了大约 1000 篇文章,而且销量很大,所以它变得很慢...... var result = from article in
我有一列用于对象创建 的时间,一列用于对象更新 的时间。当我创建并保留新对象时,我从 MySQL 收到错误: updated cannot be null. 我没有为它设置任何值,因为我希望 upda
我以前使用 git 没有任何问题,但突然间我无法推送 或克隆 任何东西。当我使用这个命令时,没有任何反应,甚至没有错误,所以我必须按 ctrl + c 或关闭 git 窗口。 我使用这个简单的命令来推
我似乎不知道下一步该做什么。我的目标是使用图像包中的 SubImage 函数从原始图像创建一个包含所有子图像的数组。我能够在 imageSplit() 函数中分割图像并通过 channel 传递给 i
我有一个 STM32L-Discovery 板,它有一个 STM32L152R8 微处理器。我很难让基本的事情发挥作用。 我看过ST给出的例子(电流消耗触摸传感器和温度传感器),我认为它们不太用户友好
这是一个散列,其中mysql列与散列的键相关,值与散列的值相关 {:jobID=>"1", :checkoutArtificateFolder=>"/cmf/new/build/Artifacts/
我有两个实体 @Entity @Table(name="parent") public class Parent { @Id String uuid; @ElementCollection
我是一名优秀的程序员,十分优秀!