开发者社区> 问答> 正文

springmvc+jpa使用persist()方法无效(在测试时又可以)

使用的都是最新的版本,在junit测试是顺利的,但是放到web上测试始终都无法插进数据库,也不报错,弄了很久,郁闷死了。再网上查了资料,大多数人都涉及到事物,但是我已经添加了注解了并且做了配置了。

下面是配置文件

db-context.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"
    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.1.xsd">
 
    <!-- DBCP DataSource -->
    <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/spring" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
 
    <!-- JPA EntityManagerFactory -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean id="jpaVendorAdapter"
                class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
            </bean>
        </property>
        <property name="dataSource" ref="dataSource" />
    </bean>
 
    <!-- Persistence Context Annotated -->
    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"
        id="persistenceAnnotationBeanPostProcessor" />
 
    <!-- Transaction -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSource" />
    </bean>
 
    <tx:annotation-driven transaction-manager="transactionManager" />
 
</beans>
 persistence.xml(在META-INF下)

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
    version="2.0">
 
    <persistence-unit name="persist" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.transaction.flush_before_completion"
                value="true" />
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
        </properties>
    </persistence-unit>
 
</persistence>
Person.java

@Entity
public class Person {
 
    private int id;
    private String name;
 
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
 
    // ...
 
}
PersonDaoImpl.java

@Repository
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class PersonDaoImpl implements PersonDao {
 
    private EntityManager entityManager;
 
    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
 
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public Person persist(Person person) {
        entityManager.persist(person);
        return person;
    }
  
        // ...
          
}
PersonController.java

@Controller
@Scope("prototype")
@RequestMapping("/person")
public class PersonController {
 
    private PersonService personService;
 
    @Inject
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }
     
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView add(@RequestParam("username") String name) {
        Person person = new Person();
        person.setName(name);
        System.out.println(name); // 可以获取到
        personService.create(person);  // 不报错,但是检查数据库没有
        ModelAndView mav = new ModelAndView();
        mav.setViewName("result");
        return mav;
    }
     
        // ...
}
 

PersonServiceTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/test-context.xml")
public class PersonControllerTest {
 
    private PersonController controller;
     
    @Inject
    public void setController(PersonController controller) {
        this.controller = controller;
    }
 
 
        // 这个测试是通过的,(加事物注解和不加都通过,不加直接写进数据库)
    <a href="http://my.oschina.net/test45" class="referer" target="_blank">@Test</a> 
    public void testAdd() {
//      PersonController controller = new PersonController();
        assertEquals("longkai", controller.add("longkai").getModelMap().get("haha"));
    }
 
}

省略了springmvc和context基于注解的代码,还有一个service类(直接调用dao方法),这个问题困扰我好久了,恳请各位帮忙解决一下!不甚感激啊。。。

展开
收起
a123456678 2016-03-13 17:08:17 3628 0
1 条回答
写回答
取消 提交回答
  • springmvc配置文件中将 配置到controller那一层,别配置在基础包上面。

    第二srping配置文件中将controller排除掉:

    <context:component-scan base-package="com.famous.easybug">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
    </context:component-scan>
    2019-07-17 19:02:53
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
移动互联网测试到质量的转变 立即下载
给ITer的技术实战进阶课-阿里CIO学院独家教材(四) 立即下载
F2etest — 多浏览器兼容性测试整体解决方案 立即下载