Announcement Announcement Module
No announcement yet.
Spring, JPA no commit Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring, JPA no commit


    i am new in spring and create a very small application to get involved. I Used JPA in the backend and spring MVC with JSTL in the frontend. I am able to connect to the database and read a list of object from the database and show it in the frontend. But if i try to add a new entry in the database, there is no insert statement. If i try the config with a java main it worked perfect. Maybe two or more eyes will see the problem.

    I used spring 3.1.0M2 if you need more information, please let me know and thanks for your help, here is the code.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    	xmlns:xsi="" xmlns:context=""
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="showSql" value="true" />
    				<property name="generateDdl" value="true" />
    				<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
    	<context:component-scan base-package="com.vaannila" />
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	<tx:annotation-driven />
    package com.vaannila.domain;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    public class User {
    	private Long id;
    	private String name;
    	private String password;
    	private String gender;
    	private String country;
    	private String aboutYou;
    	private String[] community;
    	private Boolean mailingList;
    	public Long getId() {
    		return id;
    	public void setId(Long id) { = id;
    	public String getName() {
    		return name;
    	public void setName(String name) { = name;
    	public String getPassword() {
    		return password;
    	public void setPassword(String password) {
    		this.password = password;
    	public String getGender() {
    		return gender;
    	public void setGender(String gender) {
    		this.gender = gender;
    	public String getCountry() {
    		return country;
    	public void setCountry(String country) { = country;
    	public String getAboutYou() {
    		return aboutYou;
    	public void setAboutYou(String aboutYou) {
    		this.aboutYou = aboutYou;
    	public String[] getCommunity() {
    		return community;
    	public void setCommunity(String[] community) { = community;
    	public Boolean getMailingList() {
    		return mailingList;
    	public void setMailingList(Boolean mailingList) {
    		this.mailingList = mailingList;
    package com.vaannila.dao;
    import java.util.List;
    import com.vaannila.domain.User;
    public interface UserDAO {
    	public void saveUser(User user) ;
    	public List<User> listUser() ;
    package com.vaannila.dao;
    import java.util.List;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import org.springframework.stereotype.Repository;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    import com.vaannila.domain.User;
    public class UserDAOImpl implements UserDAO {
    	private static String BASEJPA = "SELECT e FROM User e ";
    	private EntityManager em;
    	public void saveUser(User user) {
    		System.err.println("SAVE USER");
    	public List<User> listUser() {
    		System.err.println("LIST USER");
    		return em.createQuery(BASEJPA, User.class).getResultList();
    package com.vaannila.web;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
    import com.vaannila.dao.UserDAO;
    import com.vaannila.domain.User;
    public class UserController {
    	private UserDAO userDAO;
    	public void setUserDAO(UserDAO userDAO) {
    		this.userDAO = userDAO;
    	public String add(User user) throws Exception {
    		return "redirect:list.html";
    	public String list(Model model) throws Exception {
    		model.addAttribute("userList", userDAO.listUser());
    		model.addAttribute("user", new User());
    		return "userForm";
    The main who worked very well:
    package com.vaannila.main;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import com.vaannila.dao.UserDAO;
    import com.vaannila.domain.User;
    public class MainClass {
    	UserDAO autoUserDAO;
    	 * @param args
    	public static void main(String[] args) {
    		ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    		// of course, an ApplicationContext is just a BeanFactory
    		BeanFactory factory = (BeanFactory) appContext;
    		UserDAO myUserDao = factory.getBean(UserDAO.class);	
    		User u1 = new User();
    Last edited by big_blue; Sep 26th, 2012, 02:36 AM. Reason: edit, after get infos here

  • #2
    First your controller is broken either use @Controller or Controller not both (you do so by extending MultiActionController don't).

    Next your tx support is broken to have a correctly functioning transaction you should never catch and swallow a exception like you do, that breaks proper tx support.

    Another note (not related but saves you a line of xml) you don't need context:annotation-config that is already implied by using component-scanning.


    • #3

      thx a lot for your help. I will update the Controller and config file. The "try - catch" block was still for debugging. Ive also got this problem without having the "try - catch" block in the dao. But i will try.


      Still the same problem after updating my code. I also updated my first post with the current code.
      Last edited by big_blue; Sep 26th, 2012, 02:38 AM.


      • #4
        Is that all the code there is?! You don't have a DispatcherServlet with a configuration (and loading components twice once with and once without transactions).

        Also please use the search as these questions are answered numerous times before.


        • #5
          Hi, and sorry for the very late response. I did not receive a message and was not able to spend time to this project. I looked after this a few days ago and now it is working very well. Thanks for your help