Hi,

I had a similar problem trying to make an Instant to persist as
datetime in db. Looking at source of joda-time-hibernate I simply came
up with writing the mapping myself (see below).

HTH,
Tacio

PS:

public abstract class AbstractPersistence implements UserType {
       @Override
       public Object assemble(Serializable cached, Object value)
       throws HibernateException {
               return cached;
       }
       @Override
       public Serializable disassemble(Object value) throws HibernateException {
               return (Serializable) value;
       }
       @Override
       public int hashCode(Object obj) throws HibernateException {
               return obj.hashCode();
       }
       @Override
       public boolean isMutable() {
               return false;
       }
       @Override
       public Object replace(Object original, Object target, Object owner)
       throws HibernateException {
               return original;
       }
}

public class PersistentInstantAsDateTime extends AbstractPersistence
implements UserType {
       private static final int[] SQL_TYPES = new int[]{Types.TIMESTAMP};
       @Override
       public boolean equals(Object x, Object y) throws HibernateException {
               if (x == y){
                       return true;
               }
               if (x == null || y == null){
                       return false;
               }
               Instant dtx = (Instant) x;
               Instant dty = (Instant) y;
               return dtx.equals(dty);
       }

   public Object nullSafeGet(ResultSet resultSet, String[] strings,
Object object) throws HibernateException, SQLException{
               return nullSafeGet(resultSet, strings[0]);
       }

       public Object nullSafeGet(ResultSet resultSet, String string) throws
SQLException{
               Object timestamp =
Hibernate.TIMESTAMP.nullSafeGet(resultSet, string);
               if (timestamp == null){
                       return null;
               }
               return new Instant(new DateTime(timestamp).getMillis());
       }

       public void nullSafeSet(PreparedStatement preparedStatement, Object
value, int index) throws HibernateException, SQLException{
               if (value == null){

Hibernate.TIMESTAMP.nullSafeSet(preparedStatement, null, index);
               }
               else{

Hibernate.TIMESTAMP.nullSafeSet(preparedStatement, (new DateTime(
value)).toDate(), index);
               }
       }
       @SuppressWarnings("unchecked")
       @Override
       public Class returnedClass() {
               return Instant.class;
       }

       @Override
       public int[] sqlTypes() {
               return SQL_TYPES;
       }
       @Override
       public Object deepCopy(Object value) throws HibernateException {
               if (value == null)       {
                       return null;
               }
               return new Instant(value);
       }

}

Usage example:

@Column(name="start_ts", columnDefinition="datetime")
@Type(type="com.example.PersistentInstantAsDateTime")
       private Instant startTs;

On Nov 14, 2007 10:09 PM, Jim Breen <[EMAIL PROTECTED]> wrote:
>
>
> On 2007-09-27, Stephen Colebourne wrote:
>
>
>
> Following a number of requests, I have released the hibernate project v1.0:
> http://joda-time.sourceforge.net/contrib/hibernate/index.html
>
> This version provides persistence of LocalDate, LocalTime,
> LocalDateTime, Period and Duration.
>
> Please note that if you used a pre-release of the library for persisting
> Period objects, then the final release uses a different database format
> (ISO8601 string rather than descriptive string).
>
> Also note that I have no personal experience of Hibernate, so this
> project is released separate to Joda-Time, and with slightly less
> guarantees. ie. we hope it works well for you!
>
> Stephen
>
> How can I persist LocalDateTime?  The persistence classes seem to support
> LocalDate and LocalTime, but none seem to work with LocalDateTime.  I've
> tried PersistenceLocalDate, PersistenceLocalTimeAsString, and
> PersistentDateTime, but always get an error that my LocalDateTime field
> can't be copied or converted to another Joda type.  Am I missing something?
>
> Thanks for any ideas about how to persist LocalDateTime.
>
> Jim
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems?  Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Joda-interest mailing list
> Joda-interest@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/joda-interest
>
>

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Joda-interest mailing list
Joda-interest@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/joda-interest

Reply via email to