简体   繁体   English

尝试使JPA / Hibernate与REST配合使用— TransactionRequiredException:没有事务正在进行中错误

[英]Trying to Get JPA/Hibernate working with REST — TransactionRequiredException: No Transaction is in progress error

I've been trying to get Hibernate / JPA working with my simple Spring 3.2 REST application. 我一直在尝试让Hibernate / JPA与我简单的Spring 3.2 REST应用程序一起工作。

Hibernate/JPA successfully create my table in MySQL, but the transaction fails once it gets into the Repository -- saying no transaction is in progress. Hibernate / JPA在MySQL中成功创建了我的表,但是一旦事务进入存储库,事务就会失败-表示没有事务在进行中。 I'm really out of my element here and not even sure how to troubleshoot this as it seems most of the code is happening behind the scenes in xml. 我真的不在这里,甚至不确定如何解决此问题,因为似乎大多数代码都发生在xml幕后。

Any help much appreciated. 任何帮助,不胜感激。 UPDATE -- jpaContext.xml 更新-jpaContext.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:annotation-config />
<context:component-scan base-package="com.saltcitywifi"></context:component-scan>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="punit" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
            <property name="showSql" value="true" />
        </bean>
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <entry key="hibernate.hbm2ddl.auto" value="create" />
            <entry key="hibernate.format_sql" value="true" />
        </map>
    </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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/salt_city_wifi?autoReconnect=true" />
    <property name="username" value="wifi_admin" />
    <property name="password" value="password" />
</bean>

Controller: 控制器:

@Controller
public class HotSpotController {
@Autowired
private HotSpotService hotSpotService;

@RequestMapping(value = "/hotSpots", method = RequestMethod.GET)
public @ResponseBody
List<HotSpot> getHotSpots() {
    hotSpotService = new HotSpotServiceImpl();
    List<HotSpot> spots = hotSpotService.getAllHotSpots();
    return spots;
}

@RequestMapping(value = "/hotSpot", method = RequestMethod.POST)
public @ResponseBody
HotSpot addHotSpot(@RequestBody HotSpot hotSpot) {
    hotSpotService.addHotSpot(hotSpot);
    return hotSpot;
}

}

Service: 服务:

@Service("hotSpotService")
@Transactional
public class HotSpotServiceImpl implements HotSpotService {
@Autowired
private HotSpotRepository hotSpotRepository;
private AtomicLong counter = new AtomicLong();

public List<HotSpot> getAllHotSpots() {
    List<HotSpot> spots = new ArrayList<HotSpot>();
    HotSpot spot;
    for (int i = 0; i < 10; i++) {
        spot = new HotSpot("location " + i, "http://www.url" + i + ".com",
                counter.incrementAndGet());
        spots.add(spot);
    }
    return spots;
}

public HotSpot getHotSpotById(long id) {
    HotSpot spot = new HotSpot("New Spot", "New Url", id);
    return spot;
}

@Transactional
public HotSpot addHotSpot(HotSpot hotSpot) {
    return hotSpotRepository.addHotSpot(hotSpot);

}

}

@Repository("hotSpotRepository")
public class HotSpotRepositoryImpl implements HotSpotRepository {
@PersistenceContext
private EntityManager em;


public HotSpot addHotSpot(HotSpot hotSpot) {
    em.persist(hotSpot);
    em.flush();
    return hotSpot;
}

}

OK I finally figured out what was going on here: 好了,我终于弄明白了这里发生了什么:

I have two component scanners, one for my controllers in servlet-config.xml and one for my JPA stuff in jpaContext.xml. 我有两个组件扫描程序,一个用于servlet-config.xml中的控制器,另一个用于jpaContext.xml中的JPA东西。

They were both scanning basically my whole application: 他们基本上都在扫描我的整个应用程序:

<context:component-scan base-package="com.saltcitywifi" />

When I forced the component scanner for controllers to only look in the controller package: 当我强制控制器的组件扫描程序仅查看控制器软件包时:

<context:component-scan base-package="com.saltcitywifi.controller" />

All of a sudden my requests worked and were processed in the database. 突然我的请求开始工作并在数据库中得到处理。

I can only assume since my servlet-config.xml configuration only contains: 我只能假定,因为我的servlet-config.xml配置仅包含:

<context:component-scan base-package="com.saltcitywifi.controller" />
<mvc:annotation-driven />

That Spring was somehow letting servlet-config.xml register the JPA beans, so the transactional annotations weren't being picked up. 那个春天以某种方式让servlet-config.xml注册JPA bean,因此没有处理事务性注释。 This is still confusing to me, so if anyone can help explain why this occurs that would be very helpful. 这仍然让我感到困惑,因此,如果有人可以帮助解释为什么会这样,那将非常有帮助。 I might just ask another stack overflow question on this subject. 我可能会问这个问题的另一个堆栈溢出问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate JPA和Spring javax.persistence.TransactionRequiredException:没有事务正在进行中 - Hibernate JPA and Spring javax.persistence.TransactionRequiredException: no transaction is in progress TransactionRequiredException:Spring Data JPA正在进行任何事务 - TransactionRequiredException: no transaction is in progress with Spring Data JPA Hibernate 5.2和Spring 4.3,非JPA-javax.persistence.TransactionRequiredException:没有事务在进行中 - Hibernate 5.2 and Spring 4.3, Non JPA - javax.persistence.TransactionRequiredException: no transaction is in progress 错误javax.persistence.TransactionRequiredException:没有事务正在进行 - Error javax.persistence.TransactionRequiredException: no transaction is in progress 交易需要接收,没有正在进行的交易 - Transactionrequiredexception, no transaction in progress Hibernate 5.2.10.Final - javax.persistence.TransactionRequiredException:没有事务正在进行中 - Hibernate 5.2.10.Final - javax.persistence.TransactionRequiredException: no transaction is in progress Hibernate + Spring:javax.persistence.TransactionRequiredException:没有正在进行的事务 - Hibernate + Spring: javax.persistence.TransactionRequiredException: no transaction is in progress 即使应用了事务拦截器,“ TransactionRequiredException:没有事务正在进行中”-hibernate-5和spring-4.3 - “TransactionRequiredException: no transaction is in progress” even when transaction interceptor is applied - hibernate-5 and spring-4.3 javax.persistence.TransactionRequiredException错误:JPA 2 +休眠-本机查询 - javax.persistence.TransactionRequiredException Error: jpa 2 + hibernate - Native Query javax.persistence.TransactionRequiredException:Spring 5 中没有正在进行的事务 - javax.persistence.TransactionRequiredException: no transaction is in progress in Spring 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM