Results 1 to 6 of 6

Thread: How To Persist a DateTime (JodaTime) with jdbcTemplate into MySQL datetime/timestamp

  1. #1
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Question How To Persist a DateTime (JodaTime) with jdbcTemplate into MySQL datetime/timestamp

    Dear Community

    I have the follow POJO

    Code:
    package com.manuel.jordan.domain;
    
    import java.io.Serializable;
    import java.util.Date;
    
    import org.joda.time.DateTime;
    
    
    public class Persona implements Serializable{
    	
    	private static final long serialVersionUID = 1L;
    	
    	private String idPersona;
    	private String nombrePersona;
    	private String apellidoPersona;
    	private DateTime fechaPersona;
    
            setters/getters
    
            @Override
    	public String toString() {
    	....
    	}
    I have the follow table (MySQL 5.5.28)

    Code:
    create table persona(
    
    	idPersona varchar(10) not null,
    	nombrePersona varchar(20) not null,
    	apellidoPersona varchar(20) not null,
    	fechaPersona timestamp not null,
      
    	PRIMARY KEY(idPersona)
    	 
    )ENGINE=InnoDB;
    And it is my repository

    Code:
    @Transactional
    @Repository
    public class PersonaJdbcDaoImpl implements PersonaDaoService{
    
    	@Autowired
    	private JdbcTemplate jdbcTemplate;
    	
    	@Override
    	public void insertarPersona(Persona persona){
    				
    				            
    		jdbcTemplate.update("INSERT INTO persona(idPersona, nombrePersona, apellidoPersona, fechaPersona) " +
    	            "VALUES(?,?,?,?) ",
    	            new Object[]{persona.getIdPersona(),
    				       persona.getNombrePersona(),
    				       persona.getApellidoPersona(),
    				       persona.getFechaPersona()}		            
    	            );
    		
    	}
    }
    In the Main class I create the Person class and call the service

    Code:
    Persona persona = new Persona();
    		
    persona.setIdPersona("MJE");
    persona.setNombrePersona("Manuel");
    persona.setApellidoPersona("Jordan");
    persona.setFechaPersona(new DateTime("2012-12-25"));
    
    personaBoService.insertarPersona(persona);
    But sadly I got this error

    Code:
    Caused by: com.mysql.jdbc.MysqlDataTruncation: 
    Data truncation: Incorrect datetime value: '??' for column 'fechaPersona' at row 1
    	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4072)
    I already did a research in Google through many hours, but all is related with Hibernate.
    And event worst, seems the unique solution available in the web is this page

    Joda DateTime to Java SQL Timestamp storing into MySQL via Hibernate ORM – Timezone Problems

    Perhaps some member in the community has a solution.

    I am assuming you are working with a repository standalone for JDBC (Spring's jdbcTemplate support) or MyBatis. Nothing related with some JPA annotation. And of course you are working with DateTime (JodaTime)

    How you work around this problem?

    BTW, MySQL expectes the follow pattern

    Code:
    mysql> select * from persona;
    +-----------+---------------+-----------------+---------------------+
    | idPersona | nombrePersona | apellidoPersona | fechaPersona        |
    +-----------+---------------+-----------------+---------------------+
    | MJE       | Manuel        | Jordan          | 2012-10-27 21:34:50 |
    +-----------+---------------+-----------------+---------------------+
    1 row in set (0.00 sec)
    
    mysql>
    Thanks in advanced
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  2. #2
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default

    Hi Manuel,

    Have you looked at the following method in JdbcTemplate?

    Code:
    org.springframework.jdbc.core.JdbcTemplate.update(String, PreparedStatementSetter)
    You should be able to write a custom PreparedStatementSetter that converts the Joda DateTime to a java.sql.Timestamp.

    Regards,

    Sam

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Simply call the toDate method on the DateTime instead of directly storing the datetime.

    Code:
    @Transactional
    @Repository
    public class PersonaJdbcDaoImpl implements PersonaDaoService{
    
    	@Autowired
    	private JdbcTemplate jdbcTemplate;
    	
    	@Override
    	public void insertarPersona(Persona persona){
    				
    				            
    		jdbcTemplate.update("INSERT INTO persona(idPersona, nombrePersona, apellidoPersona, fechaPersona) " +
    	            "VALUES(?,?,?,?) ",
    	            new Object[]{persona.getIdPersona(),
    				       persona.getNombrePersona(),
    				       persona.getApellidoPersona(),
    				       persona.getFechaPersona().toDate()}		            
    	            );
    		
    	}
    }
    Something like this should work.

    BTW... A DaoService, what is it a service or a dao ...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Hello Sam

    Thanks for the reply

    You should be able to write a custom PreparedStatementSetter that converts the Joda DateTime to a java.sql.Timestamp.
    Oh, Seems I forgot such approach.

    Thanks for the suggestion, the "problem" is create a new class just to work around with such variable.

    Hello Marten

    Code:
    persona.getFechaPersona().toDate()
    Your approach work. Thanks a lot!

    BTW... A DaoService, what is it a service or a dao ...
    I use XDaoService to know that is an interface and XJdbcDaoImpl for implementation.
    Same idea for the Business Object XBoService and XBoImpl

    Theferore

    Main -> Bo -> Dao -> DB

    Best Regards for both
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    I use XDaoService to know that is an interface and XJdbcDaoImpl for implementation.
    I would expect something to be either a dao (XDao) or a Service (XService) but not both (XDaoService)... But whats in a name... Glad it worked out.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Hi Marten

    I would expect something to be either a dao (XDao) or a Service (XService) but not both (XDaoService)...
    I see

    Normally for each entity (X) I create XDaoService, XDaoImpl and XBoService, XBoImpl

    But whats in a name...
    Seems each one has our own style

    Glad it worked out.
    Thanks again friend
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

Posting Permissions

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