Results 1 to 7 of 7

Thread: Persist data

  1. #1
    eclectica is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    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

    Java 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

    Java 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

    Java 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

    Java 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

    Java 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; 06-18-2012 at 10:36 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Persist data

    Do all those printlns get executed?
    Are there any exceptions in your logs?
    Do you end up at whatever screen you are supposed to at the end of this transaction?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    eclectica is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Persist data

    Quote Originally Posted by Tolls View Post
    Do all those printlns get executed?
    Are there any exceptions in your logs?
    Do you end up at whatever screen you are supposed to at the end of this transaction?
    Yes I get outputs for my printlns
    I get a warning while running my project
    log4j:WARN No appenders could be found for logger (org.springframework.web.servlet.DispatcherServlet ).
    log4j:WARN Please initialize the log4j system properly.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Persist data

    That's just log4j not set up correctly, nothing to do with this problem.

    Have you tried flush()ing the EntityManager?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    eclectica is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Persist data

    If i try to flushing i get this error

    SEVERE: Servlet.service() for servlet appServlet threw exception
    javax.persistence.TransactionRequiredException: no transaction is in progress
    Last edited by eclectica; 06-19-2012 at 12:00 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Persist data

    Well, there you go then.
    You need to read up on JPA and transactions.
    In fact, I'd look up the Spring docs on JPA and Spring, since they'll handle a lot of that for you.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    eclectica is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Persist data

    Hi Tolls

    Thanks for your tip I solved my problem
    I add xmlns:tx="http://www.springframework.org/schema/tx" and Index of /schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    as location and then I could add my transaction manager
    Thanks! You made my day

Similar Threads

  1. Replies: 0
    Last Post: 02-14-2011, 03:24 PM
  2. Getting problem inserting data in data base
    By anupama in forum New To Java
    Replies: 4
    Last Post: 12-15-2010, 10:03 PM
  3. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 04:10 PM
  4. Replies: 0
    Last Post: 10-31-2008, 02:13 PM
  5. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 02:52 AM

Posting Permissions

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