2

I'm using hibernate 4.1.10 in my java console app and I've got a Model with following column:

@Column(name = "created_at", columnDefinition="datetime")
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;

I just want java to handle entire MySQL datetime properly. At now, it can only read the date part and the rest is truncated. I've got a simple test where I just print the getCreatedAt() property to stdout:

System.out.println(income.getCreatedAt());

and it prints out the following:

2012-05-11 00:00:00.0
2012-05-29 00:00:00.0
2012-05-30 00:00:00.0
2012-06-28 00:00:00.0
2012-07-30 00:00:00.0
2012-08-29 00:00:00.0
2012-09-28 00:00:00.0
2012-10-30 00:00:00.0
2012-11-29 00:00:00.0
2012-12-02 00:00:00.0
2012-12-21 00:00:00.0
2012-12-24 00:00:00.0
2013-01-30 00:00:00.0
2013-02-27 00:00:00.0
2013-02-27 00:00:00.0

This is my show create table for the table:

CREATE TABLE `category` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `parent_id` bigint(20) DEFAULT NULL,
 `name` varchar(32) NOT NULL,
 `type` varchar(255) DEFAULT NULL,
 `created_at` datetime NOT NULL,
 `updated_at` datetime NOT NULL,
 `created_by` bigint(20) DEFAULT NULL,
 `updated_by` bigint(20) DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `category_type_idx` (`type`),
 KEY `created_by_idx` (`created_by`),
 KEY `updated_by_idx` (`updated_by`),
 KEY `category_parent_id_category_id` (`parent_id`),
 CONSTRAINT `category_created_by_sf_guard_user_id` FOREIGN KEY (`created_by`) REFERENCES `sf_guard_user` (`id`),
 CONSTRAINT `category_parent_id_category_id` FOREIGN KEY (`parent_id`) REFERENCES `category` (`id`),
 CONSTRAINT `category_updated_by_sf_guard_user_id` FOREIGN KEY (`updated_by`) REFERENCES `sf_guard_user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8

I've been trying to follow several tuts/answers found on the web (also on stackoverflow) and decided to use java.util.Date with annotation instead of java.sql.Date (because the MySQL type is datetime). And this is all I've managed to achieve. What can I do to retrieve the time part?

1 Answer 1

2

You have probably data in the database already truncated. Try storing Timestamp instead of Date. For example

@Column(name = "created_at", length = 19)
public Timestamp getCreatedAt() {
    return this.createdAt;
}

setCreatedAt(new Timestamp(Calendar.getInstance().getTimeInMillis())); 
Sign up to request clarification or add additional context in comments.

1 Comment

At first, I couldn't believe in what you wrote, but afterall, you were right. The data was already truncated. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.