Results 1 to 8 of 8

Thread: Insert In SpringMVC

  1. #1
    Join Date
    May 2012
    Posts
    16

    Default Insert In SpringMVC

    Hi I have a Webapp and i want to insert a record in my db
    This is my controller

    Code:
    public class ReservationController {
    	@Autowired
    	private RoomDAO roomdao;
    	@Autowired
    	private ReservationDAO reservationdao;
    	
    	@RequestMapping(value={"/newReservation"},method=RequestMethod.GET)
        public String ReservationFormulier(ModelMap model){
            Reservation reservation = new Reservation();
            model.addAttribute("dereservatie", reservation);
            return "/newReservation";
        }
    	
    	@RequestMapping(value={"/newReservation"},method=RequestMethod.POST)
        public String ReservationMaken2(@ModelAttribute("dereservatie") @Valid Reservation reservation,
                                                            BindingResult result, ModelMap model){
    
    
    
            reservationdao.saveReservation(reservation.getRoomId(),reservation.getFirstname(),
            		reservation.getLastname(),reservation.getArrival(),reservation.getDeparture(),reservation.getNumberOfNights(),reservation.getNumberOfPersons(),reservation.getPrice());
            return "home";
        }
    }
    tis is my dao

    Code:
    public interface ReservationDAO 
    {
    	public Reservation saveReservation(int roomId, String firstname, String lastname, String arrival, String departure,String numNights,String numPersons,double price);
    
    }
    my daoImpl

    Code:
    public class ReservationDAOcoll implements ReservationDAO{
    	
    	@PersistenceContext
    	private EntityManager em;
    	
    	@Override
    	public Reservation saveReservation(int roomId, String firstname,
    			String lastname, String arrival, String departure,
    			String numNights, String numPersons, double price) {
    
    		Reservation reservation = new Reservation();
    		reservation.setRoomId(roomId);
    		reservation.setFirstname(firstname);
    		reservation.setLastname(lastname);
    		reservation.setArrival(arrival);
    		reservation.setDeparture(departure);
    		reservation.setNumberOfNights(numNights);
    		reservation.setNumberOfPersons(numPersons);
    		reservation.setPrice(price);
    		
    		em.persist(reservation);
    		return null;
    	}
    	
    	
    }
    When I press on save in my view it doensn't insert a new record in my db

    I have no errors, the webpage goes back to my home.jsp...

    Someone who knows what im doing wrong ?

    thnx!

  2. #2
    Join Date
    May 2012
    Posts
    3

    Default

    try the em.flush(); to force the writing before returning null. And if you're already receiving a Reservation object why does your DAO displays all the data instead of simply receiving the Reservation object?

  3. #3

    Default

    Quote Originally Posted by Eclectica View Post

    When I press on save in my view it doensn't insert a new record in my db

    I have no errors, the webpage goes back to my home.jsp...

    Someone who knows what im doing wrong ?

    thnx!
    You have not stated it here, but there are two possible options:
    1) Transaction is not working.
    2) You're not using a transaction manager at all.

    In order to check the point #1, enable logging on org.springframework.transaction.interceptor and see what's happening.

    If you're at point #2, then I kindly suggest to add a transaction manager, and let the manager to flush, rollback, etc.

    Best,
    Carlos.

  4. #4
    Join Date
    May 2012
    Posts
    16

    Default

    Hi carlos,
    Where can i enable logging on org.springframework.... ?
    And where can i set the transactionmanaget ?

    Thnx

  5. #5

    Default

    Quote Originally Posted by Eclectica View Post
    Hi carlos,
    Where can i enable logging on org.springframework.... ?
    And where can i set the transactionmanaget ?

    Thnx
    Hi,

    here and here you have documentation from where to grasp spring transaction knowledge.

    I think you're using JPA, but I'm not sure. While you read the documentation, you could post your application context here (inside a code block please), and I'll provide you with the code you need for both transactions and the log4j configuration as well.

    Best,
    Carlos

  6. #6
    Join Date
    May 2012
    Posts
    16

    Default

    Hi Carlos, here my app-context
    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:mvc="http://www.springframework.org/schema/mvc"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<!-- DispatcherServlet Context: defines this servlet's request-processing 
    		infrastructure -->
    
    	<!-- Scans within the base package of the application for @Components to 
    		configure as beans -->
    	<!-- @Controller, @Service, @Configuration, etc. -->
    	<context:annotation-config />
    	<context:component-scan base-package="be.hub.web4" />
    
    	<!-- Enables the Spring MVC @Controller programming model -->
    	<mvc:annotation-driven />
    
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/dbhotel" />
    		<property name="username" value="root" />
    		<property name="password" value="root" />
    	</bean>
    
    	<bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="showSql" value="false" />
    			</bean>
    		</property>
    		<property name="persistenceUnitName" value="persistenceUnitHUB"></property>
    		<property name="jpaDialect">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect">
    			</bean>
    		</property>
    	</bean>
    
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    <!-- 	 <tx: annotation-driven/> -->
    </beans>

  7. #7
    Join Date
    May 2012
    Posts
    16

    Default

    I Comment my tx:annotation-drive because i get this error "Element of attributes dont match"

  8. #8

    Default

    Hi,

    I think the problem with the <tx:annotation-driven /> is the missing schema namespace.
    Change the start of your context file by this and then uncomment the annotation-driven tag:
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xmlns:context="http://www.springframework.org/schema/context"
            xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"">
    Then go to your ReservationDAOcoll class annotate the saveReservation method with @Transactional:
    Code:
            @Transactional
    	@Override
    	public Reservation saveReservation(int roomId, String firstname,
    			String lastname, String arrival, String departure,
    			String numNights, String numPersons, double price) {
    In order to see the transaction activity in your log, enable org.springframework.transaction to trace level.

    Note:
    all the above is just to show you how to use transactions. It's not the cleanest way to do it.
    From forum and documentation you'll be able to find a better way.


    Best,
    Carlos.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •