Results 1 to 7 of 7
Thread: Persist data
- 06-18-2012, 10:33 PM #1
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
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
My ReservationDaoJava 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 controllerImplementationJava 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 domainJava 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 viewJava 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; } }
Thnx!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>Last edited by eclectica; 06-18-2012 at 10:36 PM.
- 06-19-2012, 10:17 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 06-19-2012, 10:35 AM #3
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
- 06-19-2012, 11:14 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 06-19-2012, 11:26 AM #5
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
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 progressLast edited by eclectica; 06-19-2012 at 12:00 PM.
- 06-19-2012, 11:38 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 06-19-2012, 12:01 PM #7
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
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
-
How to persist multidimensional Java arrays using JPA
By T_O_B_E_E in forum JDBCReplies: 0Last Post: 02-14-2011, 03:24 PM -
Getting problem inserting data in data base
By anupama in forum New To JavaReplies: 4Last Post: 12-15-2010, 10:03 PM -
error while retrieving data from data base
By kirtesh4u in forum New To JavaReplies: 5Last Post: 11-15-2008, 04:10 PM -
Data Pipeline 2 - Data Transformation Toolkit for Java Released
By dele in forum Java SoftwareReplies: 0Last Post: 10-31-2008, 02:13 PM -
Data Sorting in a .data file using java
By stutiger99 in forum New To JavaReplies: 2Last Post: 10-08-2008, 02:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks