Results 1 to 6 of 6

Thread: Persist data

  1. #1
    Join Date
    May 2012
    Posts
    16

    Default Persist data

    Hi,
    A few weeks ago I posted my problem I could not persist my records.
    I still have this problem but I fixed some errors.

    I have a Form with a few textboxes to make a reservation for a hotel.
    It's need your name,firstame, arrival etc...

    When I press on my save button it doesnt persist and i dont know why

    I put some system.outprintlns to check my function does receive the parameters and it does ....

    I worked on this way:

    My Reservationcontroller

    Code:
    package eu.test.test4.controller;
    
    import javax.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import be.hub.web4.dao.ReservationDAO;
    import be.hub.web4.model.Reservation;
    
    @Controller
    public class ReservationController {
    	@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 ReservationMaken(@ModelAttribute("dereservatie") @Valid Reservation reservation,
                                                            BindingResult result, ModelMap model){
    		System.out.println("Controller wordt opgeroepen");
    	
    	
          reservationdao.saveReservation(reservation.getRoomId(),reservation.getFirstname(),
           reservation.getLastname(),reservation.getArrival(),reservation.getDeparture(),reservation.getNumberOfNights(),reservation.getNumberOfPersons(),reservation.getPrice());
           
          System.out.println("Controllertest second time : " + reservation.getFirstname() );
          
           return "home";
        }
    	
    	
    	
    }
    My ReservationDao

    Code:
    package eu.test.test4.dao;
    import eu.test.test4.Reservation;
    
    
    public interface ReservationDAO 
    {
    	public Reservation saveReservation(int roomId, String firstname, String lastname, String arrival, String departure,String numberOfNights,String numberOfPersons,double price);
    	
    }
    My controllerImplementation

    Code:
    package eu.test.test4.dao;
    
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    
    
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Transactional;
    
    import be.hub.web4.model.Reservation;
    
    @Repository
    @Transactional
    public class ReservationDAOcoll implements ReservationDAO{
    	
    	@PersistenceContext
    	private EntityManager em;
    	
    	//@Transactional
    	public Reservation saveReservation(int roomId, String firstname,
    			String lastname, String arrival, String departure,
    			String numberOfNights, String numberOfPersons, double price) {
    
    		Reservation reservation = new Reservation();
    		
    		reservation.setRoomId(roomId);
    		reservation.setFirstname(firstname);
    		
    		System.out.println("The firstname: " + reservation.getFirstname());
    		reservation.setLastname(lastname);
    		reservation.setArrival(arrival);
    		reservation.setDeparture(departure);
    		reservation.setNumberOfNights(numberOfNights);
    		reservation.setNumberOfPersons(numberOfPersons);
    		reservation.setPrice(price);
    		System.out.println("ReservationDaoImpl saveReservation is called" + firstname + " " + lastname + " successfully booked:" + arrival + " till: " + departure + " #nights: " + numberOfNights + " #persons:" + numberOfPersons + " Price: " + price);
    		//em.persist(reservation);
    		em.merge(reservation);
    		return reservation;
    	}
    }
    My domain

    Code:
    package eu.test.test4.model;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    //import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;
    //import javax.validation.constraints.Digits;
    import org.hibernate.validator.constraints.NotEmpty;
    
    
    
    //import org.hibernate.validator.constraints.*;
    
    
    @Entity
    @Table(name = "tblreservations")
    public class Reservation {
    	@Id@GeneratedValue(strategy = GenerationType.AUTO)
    	@Column(name = "reservationId")
    	private int reservationId;
    	
    	@Column(name = "roomId")
    	private int roomId;
    	
    	//@Length(min = 2)
    	@Column(name = "firstname", nullable = false)
    	@NotEmpty(message="Vul naam in aub")
    	private String firstname;
    	
    
    	//@Length(min = 4)
    	@Column(name = "lastname")
    	private String lastname;
    	
    	@Column(name = "arrival")
    	//@NotEmpty
    	private String arrival;
    	
    	@Column(name = "departure")
    	//@NotEmpty
    	private String departure;
    	
    	//@Digits(integer = 3, fraction = 0)
    	@Column(name = "numNights")
    	private String numberOfNights;
    	
    
    	//@Digits(integer = 3, fraction = 0)
    	@Column(name = "numPers")
    	private String numberOfPersons;
    	
    	@Column(name = "price")
    	private double price;
    	
    public Reservation(){
    		
    	}
    	public Reservation(int roomId, String firstname, String lastname, String arrival,String departure, String numberOfNights, String numberOfPersons, double price){
    		this.roomId = roomId;
    		this.firstname = firstname;
    		this.lastname = lastname;
    		this.arrival = arrival;
    		this.departure = departure;
    		this.numberOfNights = numberOfNights;
    		this.numberOfPersons = numberOfPersons;
    		this.price = price;
    	}
    
    	public int getReservationId() {
    		return reservationId;
    	}
    
    	public void setReservationId(int reservationId) {
    		this.reservationId = reservationId;
    	}
    
    	public int getRoomId() {
    		return roomId;
    	}
    
    	public void setRoomId(int roomId) {
    		this.roomId = roomId;
    	}
    
    	public String getFirstname() {
    		return firstname;
    	}
    
    	public void setFirstname(String firstname) {
    		this.firstname = firstname;
    	}
    
    	public String getLastname() {
    		return lastname;
    	}
    
    	public void setLastname(String lastname) {
    		this.lastname = lastname;
    	}
    
    	public String getArrival() {
    		return arrival;
    	}
    
    	public void setArrival(String arrival) {
    		this.arrival = arrival;
    	}
    
    	public String getDeparture() {
    		return departure;
    	}
    
    	public void setDeparture(String departure) {
    		this.departure = departure;
    	}
    
    	public String getNumberOfNights() {
    		System.out.println("Get number of nights is called" + "  " + numberOfNights);
    		return numberOfNights;
    	}
    
    	public void setNumberOfNights(String numberOfNights) {
    		System.out.println("Set number of nights is called");
    		this.numberOfNights = numberOfNights;
    	}
    
    	public String getNumberOfPersons() {
    		return numberOfPersons;
    	}
    
    	public void setNumberOfPersons(String numberOfPersons) {
    		this.numberOfPersons = numberOfPersons;
    	}
    
    	public double getPrice() {
    		return price;
    	}
    
    	public void setPrice(double price) {
    		this.price = price;
    	}
    }
    My view

    Code:
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            
            
        </head>
        
        <body>
            <p>New Reservation</p>
            <c:url var="url" value="/newReservation.html" />
            <form:form action="${url}" commandName="dereservatie">   <%-- Spring form tags --%>
                <fieldset>
                    <div>
                        <label>roomId:</label><form:input path="roomId"/>
                        
                    </div>
                    <div>
                        <label>firstname:</label><form:input path="firstname"/>
                        
                    </div>
                    <div>
                        <label>lastname:</label><form:input path="lastname"/>
                        
                    </div>
                    <div>
                        <label>arrival:</label><form:input path="arrival"/>
                        
                    </div>
                    <div>
                        <label>departure:</label><form:input path="departure"/>
                        
                    </div>
                    <div>
                        <label>numberOfNights:</label><form:input path="numberOfNights"/>
                        
                    </div>
                    
                     <div>
                        <label>numberOfPersons:</label><form:input path="numberOfPersons"/>
                        
                    </div>
                     <div>
                        <label>price:</label><form:input path="price"/>
                        
                    </div>
    
                    <div><input name="submit" type="submit" value="save"/></div>
                </fieldset>
            </form:form>
        </body>
    </html>
    Thnx!
    Last edited by Eclectica; Jun 19th, 2012 at 05:07 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    If there is no persist there is a problem in your transaction setup.

    You use @Repository and @Controller so I suspect a component-scan and as such make sure you aren't scanning for components twice! This will lead to a transactional and non-transactional version of your dao.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    May 2012
    Posts
    16

    Default

    Do you mean I need to delete the code "@Repository" ?
    If I delete this I get an error:
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'reservationController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private be.hub.web4.dao.ReservationDAO be.hub.web4.controller.ReservationController.reser vationdao; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [be.hub.web4.dao.ReservationDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}
    Or Do I need to delete also other codes ?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default


    Do you mean I need to delete the code "@Repository" ?
    No.. As stated don't scan for the same components twice. You probably have a component-scan in both the context loaded by the ContextLoaderListener (including some tx:annotation-driven) and one in the context loaded by the DispatcherServlet (without a tx:annotation-driven). You now have 2 instances of your bean, 1 with and 1 without transactions and the latter one is used.

    So as stated (and explained many many times before on the forum) don't scan for the same components twice.

    If that isn't the case you have something wrongly configured with regard to your transactions.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    May 2012
    Posts
    16

    Default

    Ok thanks, but im a newbie in Java so can you explain the steps I need to do ?
    Thanks

  6. #6
    Join Date
    May 2012
    Posts
    16

    Default

    PROBLEM SOLVED
    I forget adding my xlmns schema and location for my transaction manager

Posting Permissions

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