3

I am taking date of birth as input using JSON

{"dateOfBirth":"1973-08-26"}

This field exists in Person.java class

import java.util.Date;

public class Person {

    Date dateOfBirth;

    //Some other fields

    public void setDateOfBirth(Date dateOfBirth) {

         this.dateOfBirth = dateOfBirth;
       }

   public Date getDateOfBirth() {

        return dateOfBirth;
       }
   }

This is mapped to person table in mysql database. I am querying the database like:

entityId = (Long) session.selectOne("ValidatePerson", registerUserRequestParams);

Following are the entries I am making in my mapper.xml

<select id="ValidatePerson" parameterMap="ValidatePersonMap" resultType="long">
    select person.entityId
    from person
    where 
                //Some other Validation checks
        <if test="dateOfBirth != null">
        and person.dateOfBirth = #{dateOfBirth}
         </if>
        );
</select>

I have a prameter Map as

<parameterMap id="ValidatePersonMap" type="java.util.HashMap">
     <parameter property="dateOfBirth" javaType="java.util.Date" jdbcType="DATE"  mode="IN"/>
</parameterMap>

I am not able to get any result from database.It does not select any row even though value exists.I have checked that none of other validation checks are failing. If I pass dateOfBirth as null in JSON then then I get result.

I have also written a test case and setting request as follows:

Date dob = new Date(73,7,26);
request.setDateOfBirth(dob);

When I pass values from test case as mentioned above I get result from database. Problem occurs only when i get request parameters using json.

  • The format of JSOn and the format stored in DB are same
  • One work around I have is to manually convert java.util.Date to String in above format and pass it as string. But this is pretty bad approach and client would not like it.
Aneesh Garg
  • 522
  • 1
  • 6
  • 16

3 Answers3

1

Use Timestamp. Consider Joda time plug. ...and read this answer.

These three will absolutely do the magic.

Good luck!

Community
  • 1
  • 1
aviad
  • 8,229
  • 9
  • 50
  • 98
0

This worked for me:

mysql attribute type is Timestamp and I format the date for JSON in Java like this:

Date d = new Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
String sd = sdf.format(d);
java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat("HH:MM:ss");
String fakeit =sd+"T"+sdf2.format(d);

I then use fakeit

GeegeeK
  • 11
  • 1
0

Have you tried formatting the java date. Maybe the formats are different so it can't match between the JSON and the one stored in your db?