0

I've been working on a little project with Java Spring. I've already set the table structure in Navicat to be able "null" values. I have also set the model to be able to null values.

But still, I can't input null values into the column and received this error messages.

FYI: I'm using Spring Tool Suite 3.9.7 IDE

Field error in object 'sertifikasihapus' on field 'modified_on': rejected value []; 
codes [typeMismatch.sertifikasihapus.modified_on,typeMismatch.modified_on,typeMismatch.java.sql.Timestamp,typeMismatch]; 
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [sertifikasihapus.modified_on,modified_on]; 
arguments []; 
default message [modified_on]]; 
default message [Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Timestamp' for property 'modified_on'; 
nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.Column java.sql.Timestamp] for value ''; 
nested exception is java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]]]

<script>
  $('#idBtnHapusHapus').click(function() {
   var vDataRole = $('#idFrmHapusSertifikasi').serialize();
   var angka = $('#Id').val();
   var nama = $('#idNamaSertifikasi').val();
   if ($("#idModifiedOn").val().length < 1) {
    $('#idModifiedOn').val(null);
    debugger;
    $.ajax({
     url : './hapussertifikasi/' + angka,
     type : 'Put',
     data : vDataRole,
     dataType : "json",
     success : function(model) {
      debugger;
      window.location = './sertifikasi'

     },
     error : function(model) {
      debugger;
     }
    });
   } else {
    debugger;
    $.ajax({
     url : './hapussertifikasi/' + angka,
     type : 'Put',
     data : vDataRole,
     dataType : "json",
     success : function(model) {
      debugger;
      window.location = './sertifikasi'

     },
     error : function(model) {
      debugger;
     }
    });
   }
  });
 </script>
<div class="col-xs-12">
  <div class="row">
   <form id="idFrmHapusSertifikasi" method="post">
    <input type="hidden" class="form-control" id="Id" name="id"
     placeholder=""> 
    <input type="hidden" class="form-control"
     id="idNamaSertifikasi" name="certificate_name" placeholder="">
    <input type="hidden" class="form-control" id="idBulanBerlaku"
     name="valid_start_month" placeholder=""> 
    <input
     type="hidden" class="form-control" id="idTahunBerlaku"
     name="valid_start_year" placeholder=""> 
    <input
     type="hidden" class="form-control" id="idPenerbit" name="publisher"
     placeholder=""> 
    <input type="hidden" class="form-control"
     id="idBulanBerlakuS" name="until_month" placeholder=""> 
    <input
     type="hidden" class="form-control" id="idTahunBerlakuS"
     name="until_year" placeholder=""> 
    <input type="hidden"
     class="form-control" id="idCatatan" name="notes" placeholder="">
    <input type="hidden" class="form-control" id="idCreateOn"
     name="createOn" placeholder="">
     <input type="hidden"
     class="form-control" id="idCreateBy" name="createBy" placeholder="">
    <input type="hidden" class="form-control" id="idModifiedOn"
     name="modified_on" placeholder=""> 
    <input type="hidden"
     class="form-control" id="idModifiedBy" name="modified_by"
     placeholder="">
   </form>
   <div class="col-xs-2">
    <i class="glyphicon glyphicon-trash center" style="font-size: 50px"></i>
   </div>
   <div class="col-xs-8">
    <div class="clTulisanHapus center" id="idTulisanHapus">
     <p>Anda Yakin ingin menghapus Sertifikasi</p>
     <!-- I WANTED THE CERTIFICATE NAME TO BE HERE -->
     <div th:each="item:${oneprofil}">
      <b><p th:text="${item.certificate_name}+' ?'"></p></b>
     </div>
    </div>
   </div>
  </div>
 </div>
 <div class="col-md-offset-8">
  <div class="btn-group">
   <input type="hidden" id="idDataId"> <input type="hidden"
    id="idDataNama">
   <button type="button" id="idBtnHapusBatal" data-dismiss="modal"
    class="btn clBtnMdlHapus">Tidak</button>
   <button type="button" id="idBtnHapusHapus" data-id="${item.id}"
    class="btn clBtnMdlHapus">Ya</button>
  </div>
 </div>
PiPio
  • 89
  • 1
  • 3
  • 11

2 Answers2

0

Looks like your value is not null but an empty String (""). You can use the keyword null without any "quotation marks".

For more info, see this question:

in practice you can simply pretend that it's "merely a special literal that can be of any reference type".

Meiswjn
  • 752
  • 6
  • 17
  • Hey i've insert some of my code, can you take a quick look at it ? Since i've already make the id value null? – PiPio Aug 01 '19 at 06:28
0

[Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Timestamp' for property 'modified_on';

You are receiving the value as an empty string instead of null which leads to type mismatch and causing the issue.

From HTML I see that you are sending it as for hidden parameter. There is an alternative way to automatically set the creation date and modified date in Spring Boot.

Use below in Entity

    @PrePersist
    protected void prePersist() {
        createdAt = set value here
    }

    @PreUpdate
    protected void preUpdate() {
        modifiedOn = set valye here
    }

Hibernate

Spring Data JPA

Romil Patel
  • 12,879
  • 7
  • 47
  • 76
  • Hello thanks for your answer, i've tried to read the documentation in the Hibernate section. I've tried it but still gave me the same error messages. – PiPio Aug 01 '19 at 07:50
  • @PiPio Can you share the entity, controller with where you save it – Romil Patel Aug 01 '19 at 07:54
  • 1
    Hey Patel! Thanks now it works, but that's not what i meant. That's instead input the date when i delete it in the modified_on column. What i meant is in the database should be also null value. Thankyou Patel :) – PiPio Aug 01 '19 at 07:59
  • I am glad it is working. @PiPio, can you please more clarity on last comment – Romil Patel Aug 01 '19 at 08:01
  • What i meant is . If i added the way you gave me it's inputing into the database the current time to the record.. What i want is , it can be input null values into the record. Since i make CRUD ,the data hasn't been modified by anyone so it should be giving null since no one edit the data. The problem is if i delete the data without editing, it gave me the error messages above. If i delete data after edit the data first, it works fine. I track the error, and i figured out the modified_on column has bugs. – PiPio Aug 01 '19 at 08:03
  • sometimes you want to enter null values instead of the current time for modifiedOn is it? if yes why we should enter null values for the creation and modified fields. – Romil Patel Aug 01 '19 at 08:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/197339/discussion-between-patel-romil-and-pipio). – Romil Patel Aug 01 '19 at 10:10