Java 使用弹簧&x2B;jpa&x2B;冬眠但能';t持久化实体并且从不抛出异常,测试通过

Java 使用弹簧&x2B;jpa&x2B;冬眠但能';t持久化实体并且从不抛出异常,测试通过,java,spring,hibernate,jpa,spring-mvc,Java,Spring,Hibernate,Jpa,Spring Mvc,我已经测试了很长时间,搜索了几个小时。由于存在一些类似的问题,但在尝试之后,它仍然不起作用,我使用SpringMVC和jpa以及hibernate来建立一个基础项目,但问题是当我将实体放入jetty时,我无法将其持久化到数据库中。奇怪的是Junit测试通过了 下面是基本代码 web.xml <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <co

我已经测试了很长时间,搜索了几个小时。由于存在一些类似的问题,但在尝试之后,它仍然不起作用,我使用SpringMVC和jpa以及hibernate来建立一个基础项目,但问题是当我将实体放入jetty时,我无法将其持久化到数据库中。奇怪的是Junit测试通过了

下面是基本代码

web.xml

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

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>controller</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/controller.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>controller</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
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;
    }

    // ...

}
PersonServiceImpl.java

@Service
public class PersonServiceImpl implements PersonService {

    private PersonDao personDao;

    @Inject
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    public Person create(Person person) {
        return personDao.persist(person);
    }

    // @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public Person save(Person person) {
        return personDao.merge(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);  // i can get the value
        personService.create(person); // nothing happend... throws no Excp
        ModelAndView mav = new ModelAndView();
        mav.setViewName("result");
        mav.addObject("val", person.getName());
        return mav;
    }

}
PersonControllerTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/test-context.xml")
public class PersonControllerTest {

    private PersonController controller;

    @Inject
    public void setController(PersonController controller) {
        this.controller = controller;
    }


        // the test passed no matter @Transactional or not, if not, the db get the row
    @Test(/*expected = NullPointerException.class*/)
    @Transactional
    public void testAdd() {
//      PersonController controller = new PersonController();
        assertEquals("test", controller.add("test").getModelMap().get("val"));
    }

}
省略了一些xml,比如上下文、mvc配置xml和导入声明,servie和dao接口,有人能帮我吗

Spring Junit不应该(默认情况下也不会)持久化到数据库中

如果希望数据持久化到数据库中,请将此行添加到测试类中

@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)

您的测试使用与应用程序不同的上下文配置?有什么不同吗?作为一个设计技巧,我会避免类级事务性注释。只需在每个方法上精确地添加所需内容。通常,服务方法应该是事务性的,而不是DAO方法。服务方法是分组到单个事务中的一组DAO调用。也就是说,这些都不应该引起您所看到的问题。您已经显示了show_sql=true,您看到生成的插入内容了吗?@alex文本上下文配置文件只是从WEB-INF的位置复制,并且有相同的内容。我们能看到您的WEB.xml吗?如果您尚未注册ContextLoaderListener,则可能无法在Jetty中加载您的上下文。
@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);  // i can get the value
        personService.create(person); // nothing happend... throws no Excp
        ModelAndView mav = new ModelAndView();
        mav.setViewName("result");
        mav.addObject("val", person.getName());
        return mav;
    }

}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/test-context.xml")
public class PersonControllerTest {

    private PersonController controller;

    @Inject
    public void setController(PersonController controller) {
        this.controller = controller;
    }


        // the test passed no matter @Transactional or not, if not, the db get the row
    @Test(/*expected = NullPointerException.class*/)
    @Transactional
    public void testAdd() {
//      PersonController controller = new PersonController();
        assertEquals("test", controller.add("test").getModelMap().get("val"));
    }

}
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)