2016-03-28 9 views
0

Im Moment habe ich eine Tabelle "Buchungen" auf meiner Hauptseite, die Informationen zu verschiedenen Buchungen anzeigt. Eines der Felder ist "Buchungsdatum", das Datum und Uhrzeit der Erstellung der Buchung anzeigt. Aber wenn ich die Buchung bearbeite, wird sie automatisch durch ein neues Datum überschrieben. Wie verhindere ich das?Datum wird überschrieben (Spring/Hibernate)

Meine Buchung Klasse

@Entity 
@Table(name="booking") 
public class Booking { 

    @Id 
    @Column(name="id") 
    @GeneratedValue(strategy= GenerationType.IDENTITY) 
    private Long id; 

    @ManyToOne 
    @JoinColumn(name="R_id") 
    private Restaurant restaurant; 

    @Column(name="date") 
    @Temporal(TemporalType.DATE) 
    private Date date; 

    @Column(name="start") 
    private String start; 

    @Column(name="duration") 
    private float duration; 

    @Column(name="amount_of_people") 
    private int amountOfPeople; 

    @Column(name="name") 
    private String name; 

    @Column(name="contact_preference") 
    private String contactPreference; 

    @Column(name="phone_number") 
    private String phoneNumber; 

    @Column(name="comments") 
    private String comments; 

    // Date that gets overwritten 
    @Column(name="current_datetime") 
    @Type(type="timestamp") 
    private Date currentDatetime; 

    @Column(name="new_date") 
    @Type(type="timestamp") 
    private Date newDate; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public Restaurant getRestaurant() { 
     return restaurant; 
    } 

    public void setRestaurant(Restaurant restaurant) { 
     this.restaurant = restaurant; 
    } 

    public Date getDate() { 
     return date; 
    } 

    public void setDate(Date date) { 
     this.date = date; 
    } 

    public String getStart() { 
     return start; 
    } 

    public void setStart(String start) { this.start = start; } 

    public float getDuration() { 
     return duration; 
    } 

    public void setDuration(float duration) { 
     this.duration = duration; 
    } 

    public int getAmountOfPeople() { 
     return amountOfPeople; 
    } 

    public void setAmountOfPeople(int amountOfPeople) { 
     this.amountOfPeople = amountOfPeople; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getContactPreference() { 
     return contactPreference; 
    } 

    public void setContactPreference(String contactPreference) { 
     this.contactPreference = contactPreference; 
    } 

    public String getPhoneNumber() { return phoneNumber; } 

    public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } 

    public String getComments() { 
     return comments; 
    } 

    public void setComments(String comments) { 
     this.comments = comments; 
    } 

    public Date getCurrentDatetime() { 
     return currentDatetime; 
    } 

    public void setCurrentDatetime(Date currentDatetime) { this.currentDatetime = currentDatetime; } 

    public Date getNewDate() { return newDate; } 

    public void setNewDate(Date newDate) { this.newDate = newDate; } 

    // Comparator 
    public static class BookingDateComparator implements Comparator<Booking> { 
     @Override 
     public int compare(Booking booking1, Booking booking2) { 
      if (booking1.getDate().compareTo(booking2.getDate()) == 0) { 
       return booking1.getStart().compareTo(booking2.getStart()); 
      } 
      return booking1.getDate().compareTo(booking2.getDate()); 
     } 
    } 

} 

Relevante Methoden in meiner MainController Klasse:

@RequestMapping(value = "bookings", method = RequestMethod.GET) 
public String bookings(Model model) { 
    List<Booking> bookingList = bookingService.getBookings(); 
    model.addAttribute("bookings", bookingList); 
    initModelList(model); 
    return "bookings"; 
} 
    @RequestMapping(value = "booking/create", method = RequestMethod.GET) 
public String createBooking(Model model) { 
    model.addAttribute("booking", new Booking()); 
    initModelList(model); 
    return "newBooking"; 
} 

@RequestMapping(value = "booking/create", method = RequestMethod.POST) 
public String createBookingAction(@Valid @ModelAttribute ("booking") Booking booking, BindingResult result, Model model) { 
    BookingFormValidator bookingFormValidator = new BookingFormValidator(); 
    bookingFormValidator.validate(booking, result); 
    if (result.hasErrors()) { 
     initModelList(model); 
     return "newBooking"; 
    } 
    bookingService.addBooking(booking); 
    return "redirect:/bookings"; 
} 

@RequestMapping(value = "booking/edit/{id}", method = RequestMethod.GET) 
public String editBooking(@PathVariable Long id, Model model) { 
    initModelList(model); 
    Booking booking = bookingService.getBooking(id); 
    model.addAttribute("booking", booking); 
    return "editBooking"; 
} 

@RequestMapping(value = "booking/edit/{id}", method = RequestMethod.POST) 
public String editBookingAction(@Valid @ModelAttribute ("booking") Booking booking, BindingResult result, Model model) { 
    BookingFormValidator bookingFormValidator = new BookingFormValidator(); 
    bookingFormValidator.validate(booking, result); 
    if (result.hasErrors()) { 
     initModelList(model); 
     return "editBooking"; 
    } 
    bookingService.updateBooking(booking); 
    return "redirect:/bookings"; 
} 

Die .jsp meiner Haupt Seite

<body> 
<jsp:include page="../fragments/menu.jsp"/> 
<div id="body"> 

    <section class="content-wrapper main-content clear-fix"> 


     <h2>List of bookings</h2> 

     <p> 
      <a href="/restaurant/create">Add restaurant</a> 
      <a href="/booking/create">Create New Booking</a> 
     </p> 
     <table class=""> 
      <tr> 
       <th> 
        Booking information 
       </th> 
       <th></th> 
      </tr> 

      <c:forEach items="${restaurants}" var="restaurants"> 
       <tr> 
        <td><h3>${restaurants}</h3></td> 
        <td valign="bottom"><a href="/restaurant/edit/${restaurants.id}">Edit restaurant</a></td> 

        <tr> 
         <table> 
          <col align="left" width="100"> 
          <col align="left" width="60"> 
          <col align="left" width="60"> 
          <col align="left" width="60"> 
          <col align="left" width="60"> 
          <col align="left" width="90"> 
          <col align="left" width="90"> 
          <col align="left" width="90"> 
          <col align="left" width="100"> 
          <col align="left" width="100"> 
          <tr> 
           <td align="left"><b>Date</b></td> 
           <td align="left"><b>Start</b></td> 
           <td align="left"><b>Duration</b></td> 
           <td align="left"><b># Of People</b></td> 
           <td align="left"><b>Name</b></td> 
           <td align="left"><b>Contact Pref.</b></td> 
           <td align="left"><b>Phone #</b></td> 
           <td align="left"><b>Comments</b></td> 
           <td align="left"><b>Booking date</b></td> 
          </tr> 
          <c:forEach items="${sortedBooking}" var="sortedBooking"> 
           <c:choose> 
            <c:when test="${restaurants == sortedBooking.restaurant.restaurantName}"> 
             <tr> 
              <td>${sortedBooking.date}</td> 
              <td>${sortedBooking.start}</td> 
              <td>${sortedBooking.duration}</td> 
              <td>${sortedBooking.amountOfPeople}</td> 
              <td>${sortedBooking.name}</td> 
              <td>${sortedBooking.contactPreference}</td> 
              <c:choose> 
               <c:when test="${sortedBooking.phoneNumber.equals('')}"> 
                <td>NO PHONE</td> 
               </c:when> 
               <c:otherwise> 
                <td>${sortedBooking.phoneNumber}</td> 
               </c:otherwise> 
              </c:choose> 
              <c:choose> 
               <c:when test="${sortedBooking.comments.equals('')}"> 
                <td>NO COMMENTS</td> 
               </c:when> 
               <c:otherwise> 
                <td>${sortedBooking.comments}</td> 
               </c:otherwise> 
              </c:choose> 
              <td>${sortedBooking.currentDatetime}</td> 
              <td><a href="/booking/edit/${sortedBooking.id}">Edit booking</a></td> 
             </tr> 
            </c:when> 
           </c:choose> 
          </c:forEach> 
         </table> 
        </tr> 
       </tr> 
      </c:forEach> 
     </table> 

    </section> 
</div> 
<jsp:include page="../fragments/footer.jsp"/> 

</body> 

Mein editBooking.jsp:

<body> 
<jsp:include page="../fragments/menu.jsp"/> 
<div id="body"> 
    <section class="content-wrapper main-content clear-fix"> 

     <h2>Edit</h2> 

     <form:form modelAttribute="booking"> 
      <table> 
       <tr> 
        <td>Restaurant*:</td> 
        <td><form:select path="restaurant.id"> 
          <form:option value="" label="--- Select ---" /> 
          <form:options items="${restaurants}" itemValue="id" itemLabel="restaurantName" /> 
        <td><form:errors path="restaurant.id" cssClass="error"/></td> 
        </form:select> 
       </tr> 
       <tr> 
        <td>Date*:</td> 
        <td><form:input path="date" type="date"/></td> 
        <td><form:errors path="date" cssClass="error"/></td> 
       </tr> 
       <tr> 
        <td>Starting time*:</td> 
        <td><form:input path="start" type="time"/></td> 
        <td><form:errors path="start" cssClass="error"/></td> 
       </tr> 
       <tr> 
        <td>Duration*:</td> 
        <td><form:input path="duration"/></td> 
        <td><form:errors path="duration" cssClass="error"/></td> 
       </tr> 
       <tr> 
        <td>Amount of people*:</td> 
        <td><form:input path="amountOfPeople"/></td> 
        <td><form:errors path="amountOfPeople" cssClass="error"/></td> 
       </tr> 
       <tr> 
        <td>Name*:</td> 
        <td><form:input path="name"/></td> 
        <td><form:errors path="name" cssClass="error"/></td> 
       </tr> 
       <tr> 
        <td>Contact preference*:</td> 
        <td><form:select path="contactPreference"> 
          <form:option value="" label="--- Select ---" /> 
          <form:option value="e-mail" label="E-mail" /> 
          <form:option value="phone" label="Phone" /> 
          <form:option value="other" label="Other" /> 
        <td><form:errors path="contactPreference" cssClass="error"/></td> 
        </form:select> 
       </tr> 
       <tr> 
        <td>Phone number:</td> 
        <td><form:input path="phoneNumber"/></td> 
        <td><form:errors path="phoneNumber" cssClass="error"/></td> 
       </tr> 
       <tr> 
        <td>Comments:</td> 
        <td><form:textarea path="comments" rows="5" cols="30"/></td> 
        <td><form:errors path="comments" cssClass="error"/></td> 
       </tr> 
       <tr> 
        <td colspan="3"><input type="submit" /></td> 
       </tr> 
      </table> 
     </form:form> 
     <div> 
      <a href="/bookings">Back to List</a> 
     </div> 


    </section> 
</div> 
<jsp:include page="../fragments/footer.jsp"/> 

</body> 

Jede Hilfe wird geschätzt! Entschuldigung für die lange Post.

EDIT: Ich habe versucht, meinen Code zu ändern, um die Aktualisierung zu deaktivieren, aber es funktioniert immer noch nicht.

@Column(name="current_datetime", updatable=false, nullable=false) 
@Type(type="timestamp") 
private Date currentDatetime = new Date(); 
+0

was macht Sie 'editBookingAction' tun? ist es SaveOrUpdate? Bist du sicher, dass es keinen neuen Rekord schafft? Können Sie versuchen, Ihr Datum mit '@ CreationTimestamp' zu kommentieren? –

Antwort

1

Sie @Type(type="timestamp") nicht Tage für die Speicherung verwenden. In vielen Datenbanken sind Zeitstempel dazu gedacht, Änderungen zu verfolgen, anstatt ein gewöhnliches Datumsfeld für Ihre Anwendungslogik zu sein. Wechseln Sie in der Datenbank zu DATETIME und entfernen Sie die @Type Annotation, da der Ruhezustand bereits Date in den richtigen Typ konvertiert. auch

Siehe:

+0

Vielen Dank! Es funktioniert jetzt. – charliekelly

+0

Gern geschehen, Kumpel ;-) –

Verwandte Themen