Spring4.1 + Hibernate4.3 + Jpa

瘦不了花 2014-11-06 04:22:15

我就是整合之后,查询没有问题,但是在保存的时候就好像没有事物参与进来,麻烦各位帮忙看看

下边是我的代码

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="myProjectUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<properties>
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@192.168.8.28:1521:orcl" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.connection.username" value="activiti" />
<property name="hibernate.connection.password" value="123123" />

<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>

</persistence>
,


root-content.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

<context:component-scan base-package="com.ls.myproject">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.8.28:1521:orcl"></property>
<property name="username" value="activiti"></property>
<property name="password" value="123123"></property>

<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="3"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="validationQuery" value="SELECT 1 from dual"/>
</bean>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"></bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- <property name="dataSource" ref="dataSource"></property> -->
<property name="persistenceUnitName" value="myProjectUnit"></property>

<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"></property>
<property name="database" value="ORACLE"></property>
</bean>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>



</beans>
,


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">

<welcome-file-list>
<welcome-file>zyxh/index.jsp</welcome-file>
</welcome-file-list>

<!-- 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</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>openEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
<init-param>
<param-name>entityManagerFactoryBeanName</param-name>
<param-value>entityManagerFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-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>
,

dao

package com.ls.myproject.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.ls.myproject.dao.ITreeDAO;
import com.ls.myproject.model.TreeModel;

@Repository
public class TreeDAOImpl implements ITreeDAO {

@PersistenceContext
private EntityManager entityManager = null;

@Override
public void save(TreeModel model) throws Exception {
System.out.println("saveing......");
this.entityManager.persist(model);
System.out.println("saveing end......");
this.entityManager.createQuery("select tree from TreeModel tree").getResultList();

}

}


service

package com.ls.myproject.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.myproject.dao.ITreeDAO;
import com.ls.myproject.model.TreeModel;
import com.ls.myproject.service.ITreeService;

@Service
public class TreeServiceImpl implements ITreeService {

private ITreeDAO treeDAO = null;
@Autowired( required = true )
public void setTreeDAO(ITreeDAO treeDAO) {
this.treeDAO = treeDAO;
}

@Override
public void save(TreeModel model) throws Exception {
this.treeDAO.save(model);
}

}


controller

package com.ls.myproject.controller;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ls.myproject.model.TreeModel;
import com.ls.myproject.service.ITreeService;

@Controller
@RequestMapping( value = "/tree" )
public class TreeController {

@Autowired( required = true )
private ITreeService treeService = null;

@RequestMapping( value = "/save", method = RequestMethod.GET )
@Transactional( rollbackFor = java.lang.Exception.class )
public void saveTree(HttpServletRequest request, HttpServletResponse response, TreeModel model) throws Exception {
response.setContentType("text/json;charset=utf-8");

System.out.println("is runing.....");
String uuid = UUID.randomUUID().toString();
model.setTreeId("11111");
model.setTreeName("test tree");
model.setTreeUrl("test url");
model.setTreeNote("sdfsdf");
model.setParentId("");
model.setSortLogo(1);

this.treeService.save(model);


response.getWriter().print("{\"success\":true, \"isPass\":false}");
}

}
...全文
146 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
kky2010_110 2014-11-06
  • 打赏
  • 举报
回复
@Transactional (propagation = Propagation.REQUIRED) public void save(TreeModel model) throws Exception { this.treeDAO.save(model); }
驱动器 D 中的卷没有标签。 卷的序列号是 0004-19C1 D:\spring 3.2.0+hibernate 4.1.6+struts2整合需要的jar包 的目录 2013/05/11 15:26 . 2013/05/11 15:26 .. 2013/03/21 18:20 445,288 antlr-2.7.7.jar 2013/03/21 18:20 4,467 aopalliance-1.0.jar 2013/03/21 18:20 43,578 asm-3.3.jar 2013/03/21 18:20 38,275 asm-commons-3.3.jar 2013/03/21 18:20 1,887,781 aspectjweaver-1.5.3.jar 2013/03/21 18:20 115,709 bonecp-0.7.1.RELEASE.jar 2013/03/21 18:20 282,338 cglib-2.1.3.jar 2013/03/21 18:20 121,757 commons-dbcp.jar 2013/03/21 18:20 59,590 commons-fileupload-1.2.2.jar 2013/03/21 18:20 159,509 commons-io-2.0.1.jar 2013/03/21 18:20 315,805 commons-lang3-3.1.jar 2013/03/21 18:20 60,686 commons-logging-1.1.1.jar 2013/03/21 18:20 42,492 commons-pool.jar 2013/03/21 18:20 313,898 dom4j-1.6.1.jar 2013/05/11 15:26 0 filesnames.txt 2013/03/21 18:20 931,168 freemarker-2.3.19.jar 2013/03/21 18:20 639,592 google-collections-1.0.jar 2013/03/21 18:20 1,795,936 guava-12.0.jar 2013/03/21 18:20 81,271 hibernate-commons-annotations-4.0.1.Final.jar 2013/03/21 18:20 4,451,544 hibernate-core-4.1.6.Final.jar 2013/03/21 18:20 478,440 hibernate-entitymanager-4.1.6.Final.jar 2013/03/21 18:20 391,378 hibernate-envers-4.1.6.Final.jar 2013/03/21 18:20 102,661 hibernate-jpa-2.0-api-1.0.1.Final.jar 2013/03/21 18:20 648,253 javassist-3.15.0-GA.jar 2013/03/21 18:20 60,768 jboss-logging-3.1.0.GA.jar 2013/03/21 18:20 11,209 jboss-transaction-api_1.1_spec-1.0.0.Final.jar 2013/03/21 18:20 802,721 mysql-connector-java-5.1.20-bin.jar 2013/03/21 18:20 227,807 ognl-3.0.5.jar 2013/03/21 18:20 1,977,267 ojdbc6.jar 2013/03/21 18:20 26,083 slf4j-api-1.7.2.jar 2013/03/21 18:20 334,628 spring-aop-3.2.0.RC2.jar 2013/03/21 18:20 69,796 spring-aspects-3.2.0.RC2.jar 2013/03/21 18:20 605,733 spring-beans-3.2.0.RC2.jar 2013/03/21 18:20 853,183 spring-context-3.2.0.RC2.jar 2013/03/21 18:20 126,740 spring-context-support-3.2.0.RC2.jar 2013/03/21 18:20 863,315 spring-core-3.2.0.RC2.jar 2013/03/21 18:20 193,557 spring-expression-3.2.0.RC2.jar 2013/03/21 18:20 400,914 spring-jdbc-3.2.0.RC2.jar 2013/03/21 18:20 391,926 spring-orm-3.2.0.RC2.jar 2013/03/21 18:20 240,414 spring-tx-3.2.0.RC2.jar 2013/03/21 18:20 622,166 spring-web-3.2.0.RC2.jar 2013/03/24 17:46 794,039 struts2-core-2.3.7.jar 2013/03/21 18:20 21,810 struts2-spring-plugin-2.3.4.1.jar 2013/03/21 18:20 636,751 xwork-core-2.3.7.jar 44 个文件 22,672,243 字节 2 个目录 81,522,536,448 可用字节

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧