Announcement Announcement Module
Collapse
No announcement yet.
Insert In SpringMVC Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


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

      Comment


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

        Thnx

        Comment


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

          Comment


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

            Comment


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

              Comment


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

                Comment

                Working...
                X