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

  • Spring, JPA no commit

    Hi,

    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.

    applicationContext.xml
    Code:
    <?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:p="http://www.springframework.org/schema/p"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="
    			http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.1.xsd	
    			http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.1.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.1.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
    
    	<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" />
    			</bean>
    		</property>
    	</bean>	
    	<context:component-scan base-package="com.vaannila" />
    
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>		
    	<tx:annotation-driven />
    	
    </beans>
    model:
    Code:
    package com.vaannila.domain;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity
    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;
    	
    	@Id
    	@GeneratedValue
    	@Column(name="USER_ID")
    	public Long getId() {
    		return id;
    	}
    	public void setId(Long id) {
    		this.id = id;
    	}
    	
    	@Column(name="USER_NAME")
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	@Column(name="USER_PASSWORD")
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	
    	@Column(name="USER_GENDER")
    	public String getGender() {
    		return gender;
    	}
    	public void setGender(String gender) {
    		this.gender = gender;
    	}
    	
    	@Column(name="USER_COUNTRY")
    	public String getCountry() {
    		return country;
    	}
    	public void setCountry(String country) {
    		this.country = country;
    	}
    	
    	@Column(name="USER_ABOUT_YOU")
    	public String getAboutYou() {
    		return aboutYou;
    	}
    	public void setAboutYou(String aboutYou) {
    		this.aboutYou = aboutYou;
    	}
    	
    	@Column(name="USER_COMMUNITY")
    	public String[] getCommunity() {
    		return community;
    	}
    	public void setCommunity(String[] community) {
    		this.community = community;
    	}
    	
    	@Column(name="USER_MAILING_LIST")
    	public Boolean getMailingList() {
    		return mailingList;
    	}
    	public void setMailingList(Boolean mailingList) {
    		this.mailingList = mailingList;
    	}
    
    }
    dao:
    Code:
    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() ;
    }
    daoImpl:
    Code:
    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;
    
    @Repository("myUserDAO")
    @Transactional(readOnly=false)
    @Service
    public class UserDAOImpl implements UserDAO {
    
    	private static String BASEJPA = "SELECT e FROM User e ";
    	
    	@PersistenceContext
    	private EntityManager em;
    	
    	
    
    	public void saveUser(User user) {
    		System.err.println("SAVE USER");
    		em.persist(user);		
    	}
    	
    	public List<User> listUser() {
    		System.err.println("LIST USER");
    		return em.createQuery(BASEJPA, User.class).getResultList();
    	}
    	
    }
    controller:
    Code:
    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;
    
    @Controller
    public class UserController {
    
    	@Autowired
    	private UserDAO userDAO;
    
    	public void setUserDAO(UserDAO userDAO) {
    		this.userDAO = userDAO;
    	}
    
    	@RequestMapping(value="/add")
    	public String add(User user) throws Exception {
    		user.setName("setBySystem");
    		user.setPassword("sadfas");
    		userDAO.saveUser(user);
    		return "redirect:list.html";
    	}
    	
    	@RequestMapping(value="/list")
    	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:
    Code:
    package com.vaannila.main;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.vaannila.dao.UserDAO;
    import com.vaannila.domain.User;
    
    public class MainClass {
    	
    	@Autowired
    	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();
    		u1.setName("Username");
    		u1.setPassword("sadfasdfasf");
    		myUserDao.saveUser(u1);	
    	}
    }
    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.

    Comment


    • #3
      Hi,

      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.

      Cheers
      bb

      *EDIT*
      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.

      Comment


      • #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.

        Comment


        • #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

          Comment

          Working...
          X