- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 JPA 和 Hibernate 以及 JPA 提供程序使用 Spring 3.2 开发一个简单的薪资应用程序。我将其部署在 JBoss AS 7 服务器上。我不断收到以下错误:
16:42:13,811 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/PayrollApp].[payrollApp]] (http--127.0.0.1-8080-1) Servlet.service() for servlet payrollApp threw exception: javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:993) [hibernate-entitymanager-4.2.2.Final.jar:4.2.2.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at com.sun.proxy.$Proxy30.flush(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at com.sun.proxy.$Proxy30.flush(Unknown Source)
at com.colenso.payrollapp.persistence.daos.GenericDAO.add(GenericDAO.java:34) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155) [spring-tx-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at com.sun.proxy.$Proxy31.add(Unknown Source)
at com.colenso.payrollapp.service.HRServiceImpl.addEmployee(HRServiceImpl.java:29) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) [spring-tx-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at com.sun.proxy.$Proxy32.addEmployee(Unknown Source)
at com.colenso.payrollapp.controllers.PaySlipController.showHomePage(PaySlipController.java:42) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25]
我正在使用没有任何 persistence.xml 文件的 Spring 3.2。我的 Spring App Config 文件的相关部分:
<!-- Turn on annotation driven features of spring-->
<mvc:annotation-driven/>
<!--<context:annotation-config />-->
<!--Enable Spring to detect the Controllers and other components automatically and configure them as beans-->
<context:component-scan base-package="com.colenso.payrollapp" />
<!--Look up the Datasource that's been configured in JBoss as a JNDI datasource-->
<jee:jndi-lookup id="datasource" jndi-name="java:jboss/datasources/MySqlDS" resource-ref="false" />
<!--Configuration for the Entity Manger Factory-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="com.colenso.payrollapp.persistence.entities" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="dataSource" ref="datasource"/>
<property name="jpaPropertyMap" ref="jpaPropertyMap" />
<property name="persistenceUnitName" value="payrollPU" />
</bean>
<util:map id="jpaPropertyMap">
<entry key="show_sql" value="true" />
<entry key="dialect" value="org.hibernate.dialect.MySQLDialect" />
<entry key="hibernate.hbm2ddl.auto" value="validate" />
<entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
<entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<entry key="hibernate.cache.use_second_level_cache" value="true" />
<entry key="hibernate.cache.use_query_cache" value="false" />
<entry key="hibernate.generate_statistics" value="false" />
<entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
</util:map>
<!--Enable injection of the Entity Manager-->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<!--Enable Spring's Exception Translation Mechanism-->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!--Configuration for the Transactions-->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:jboss/TransactionManager"/>
</bean>
<tx:annotation-driven />
<tx:jta-transaction-manager />
如您所见,我正在提取在 standalone.xml 文件中配置的数据源。
Controller :
@Controller
public class PaySlipController {
@Autowired
HRService hrService;
public PaySlipController() {
}
@RequestMapping("/")
public String showHomePage(Map<String, Object> model) {
Employee employee1;
employee1 = new Employee();
employee1.setFirstName("FN");
employee1.setLastName("LN");
Calendar myDOB = new GregorianCalendar(1965, 6, 15);
employee1.setDateOfBirth(myDOB);
hrService.addEmployee(employee1);
return "home";
}
}
我遵循本文中提到的类型安全的通用 DAO 模式
enter link description here这是所有相关代码:界面 公共(public)接口(interface) GenericDAOInterface {
void add(E entitry);
void remove(E entity);
E findById(K id);
实现
public abstract class GenericDAO<E, K> implements GenericDAOInterface<E, K> {
protected Class entityClass;
@PersistenceContext
protected EntityManager entityManager;
public GenericDAO() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
this.entityClass = (Class) genericSuperclass.getActualTypeArguments()[1];
}
@Override
public void add(E entity) {
if (entityManager==null)
System.out.println("Entity Manager Not Initialised!!");
entityManager.persist(entity);
entityManager.flush();
}
@Override
public void remove(E entity) {
entityManager.remove(entity);
}
@Override
public E findById(K id) {
return (E) entityManager.find(entityClass, id);
}
接口(interface):
public interface EmployeeDAO extends GenericDAOInterface<Employee, Integer>{
}
实现
@Repository
public class EmployeeDAOImpl extends GenericDAO<Employee, Integer> implements EmployeeDAO{
}
接口(interface):
public interface HRService {
public void addEmployee(Employee employee);
}
实现
@Component
@Transactional(propagation = Propagation.REQUIRED)
public class HRServiceImpl implements HRService{
@Autowired
private EmployeeDAO employeeDao;
@Override
public void addEmployee(Employee employee){
employeeDao.add(employee);
}
}
最佳答案
我设法根据这个 Question 的答案找到了我的问题的解决方案
假设要使用 JTA,它是 persistence.xml 所必需的。这就是我最终所做的。
我的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>
<jta-data-source>java:/jboss/datasources/MySqlDS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<class>com.colenso.payrollapp.persistence.entities.Employee</class>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.current_session_context_class" value="jta"/>
<property name="hibernate.transaction.manager_lookup_class" value="blah blah"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.connection.release_mode" value="after_statement"/>
</properties>
</persistence-unit>
</persistence>
我更改的Spring Context xml:
<!--********************DataBase Configurations********************************-->
<!--Look up the Datasource that's been configured in JBoss as a JNDI datasource-->
<jee:jndi-lookup id="datasource" jndi-name="java:jboss/datasources/MySqlDS" resource-ref="false" />
<!--Configuration for the Entity Manger Factory-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="com.colenso.payrollapp.persistence.entities" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="dataSource" ref="datasource"/>
<property name="jpaPropertyMap" ref="jpaPropertyMap" />
<property name="persistenceUnitName" value="myPU" />
</bean>
<util:map id="jpaPropertyMap">
<entry key="show_sql" value="true" />
<entry key="dialect" value="org.hibernate.dialect.MySQLDialect" />
<entry key="hibernate.hbm2ddl.auto" value="validate" />
<entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
<entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<entry key="hibernate.cache.use_second_level_cache" value="true" />
<entry key="hibernate.cache.use_query_cache" value="false" />
<entry key="hibernate.generate_statistics" value="false" />
<entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
</util:map>
<!--Enable injection of the Entity Manager-->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<!--Enable Spring's Exception Translation Mechanism-->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!--Configuration for the Transactions-->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" >
<property name="allowCustomIsolationLevels" value="true" />
<property name="transactionManagerName" value="java:/TransactionManager"/>
</bean>
<tx:annotation-driven />
关于hibernate - JBoss 7 Spring JPA + Hibernate 应用程序 : no transaction is in progress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17260328/
我不明白注释之间的实际区别是什么javax.transaction.Transactional和org.springframework.transaction.annotation.Transacti
我不明白注释 javax.transaction.Transactional 和 org.springframework.transaction.annotation.Transactional 之间
我正在尝试删除一个节点。 我知道要先删除节点,我必须删除关系。 MATCH (n:`Dummy`) WHERE n.uuid='1aa41234-aaaa-xxxx-ffff-xxxx11xx0x62
假设我有一个共享钱包,可以为我和我的兄弟收集以太币。我们彼此分享这个钱包的 50%。 如果有一笔 ETH 交易进入这个钱包,是否有一种自动方式可以将收到的以太币自动发送到我的个人钱包和我兄弟的钱包,而
我已经阅读并重新阅读了文档 re: mnesia:activity/3、mnesia:activity/4 和 mnesia/transaction/2,但它们对我来说仍然像是一种晦涩难懂的外语。 在
精简版: 在 Firebase 事务(在 Java 中)中,如果我从 MutableData.getValue() 中得到意外的或不一致的(陈旧的)值,我应该如何进行错误检查并确保事务在必要时重复运行
使用 Spring 时@Transcational在服务层,我需要放置 在 xml 文件上。 我想知道 可以javax.jdo.annotations.Transactional像spring一样用在
这是我的情况。 我正在构建一个 RESTful Web 服务,从客户端接收数据,然后根据该数据创建一个事件,然后我想将这个新事件推送到 celery 以异步处理它。 我使用 Pyramid 构建 RE
这是我的情况。 我正在构建一个 RESTful web 服务,它从客户端接收数据,然后从该数据创建一个事件,然后我想将这个新事件推送到 celery 以异步处理它。 我使用 pyramid 构建 RE
当我启动 jetty 时,以下行出现在日志中: :INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp
@Transactional(rollbackFor = someException.class) public void methodA() throws someException { t
我花了几个小时试图解决这个问题。谷歌和 Stackoverflow 也没有多大帮助。所以这里非常欢迎任何建议。 我正在尝试在更新两个相关表时对事务应用回滚逻辑: 一般的代码是: // ... $em
我在 Service 类中看到了一个方法,它被标记为 @Transactional,但它还在同一个类中调用了一些其他方法,这些方法没有被标记为 @Transactional。 这是否意味着对单独方法的
我目前正在使用 Microsoft Enterprise Library 5.0,我想知道下面的代码是否是处理事务的可接受方式。 我已经稍微简化了场景,但本质是我想在同一个事务中在不同的数据库中执行多
我已将以下服务方法注释为事务性: /* (non-Javadoc) * @see a.b.service.CustomerService#activateCustomer(a.b.m
以下是我的代码的一个代表性片段,其中在 transaction.Rollback() 处抛出了一个意外的异常,至少对我而言是这样。声明。 异常(exception)是类型 NHibernate.Tra
我试过将 COMMIT TRAN 放在 if else 循环中,但我仍然收到此错误。 我必须为一个类(class)招收一名学生。如果注册后的座位数为负数,我必须将其反转并打印一条消息说不能注册。我已经
我已经实现了一个具有事务的路由。当用户通过单击“后退”按钮移出这条路线时,我希望用户能够确认退出并丢失通过回滚事务所做的任何更改。 问题是,如果用户返回路由,Ember Data 会引发错误并指出:
当我从另一个事务方法调用一个事务方法时会发生什么,现在我的第二个事务方法已完成,并且它返回到第一个事务方法,不幸的是它失败了,所以它会回滚所有内容,意味着它会回滚第二个事务方法吗?交易方式改变..??
这个问题在这里已经有了答案: @Transactional method called from another method doesn't obtain a transaction (4 个回答)
我是一名优秀的程序员,十分优秀!