on my database I have a column where ack_date_time is stored(but in string), Im not the one who start this model and he saves the date on string on gmt example: Tue Aug 04 16:58:27 GMT+08:00 2015
, now we have large amount of data so I dont want to change it as date field as much as possible. how can I change this query statement on jpa without using native query? I manage to order by it on mysql but not on jpa. im a little bit new to it. thanks in advance.
select *, STR_TO_DATE(CONCAT(SUBSTR(ack_date_time,1,20),SUBSTR(ack_date_time,31,34)),'%W %M %D %H:%i:%s %Y') as acktime from DEMI order by acktime desc;
If I understood correctly, the DB level data type of ack_date_time
column is VARCHAR
, isn't it?
If it is, you can try AttributeConverter
, see example here: http://www.thoughts-on-java.org/jpa-21-how-to-implement-type-converter/
You can define your own attribute converter as follows:
@Converter
public class DemiDateConverter implements AttributeConverter<Timestamp, String> {
/**
* Convert Timestamp object to a String.
*/
@Override
public String convertToDatabaseColumn(Timestamp ts) {
// here you should implement the logic which converts the Timestamp to DB compatible format
}
/**
* Convert a String to a Timestamp object.
*/
@Override
public Timestamp convertToEntityAttribute(String str) {
// here you should implement the logic that parses string from DB and transforms it to regular timestamp.
}
}
Then you need to use the converter in your entity class:
@Entity
public class DemiEntity {
// id and other fields
@Column(name = "ack_date_time")
@Convert(converter = DemiDateConverter.class)
private Timestamp acktime;
// getters and setters
...
}
BTW, have you already tried to use JPA entity for DEMI
table?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.