Search code examples
javahibernatejodatimeusertype

Mapping Exception with Hibernate 4.0, UserType 3.0.0.CR4 and Joda Time 2.1


I am currently trying to persist Joda Time in Hibernate using usertype, but I keep on getting the following error in eclipse.

Error Message

(failed.org.hibernate.MappingException: Could not determine type for: org.jadira.usertype.dateandtime.jsr310.PersistentLocalDateTime, at table: MODEL_OBJECT, for columns: [org.hibernate.mapping.Column(MODIFIED_DATE_TIME)])

my xml file looks like this

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="model" >
    <class name="ModelObject" table="MODEL_OBJECT">
        <id name="id" column="MODEL_ID" >
            <generator class="native"></generator>
    </id>
    <property name="modifiedDateTime"             
            type="org.jadira.usertype.dateandtime.jsr310.PersistentLocalDateTime"
            column="MODIFIED_DATE_TIME"/>
        <property name="creationDateTime"
            type="org.jadira.usertype.dateandtime.jsr310.PersistentLocalDateTime"
            column="CREATION_DATE_TIME"/>
    </class>
</hibernate-mapping>`

and my java file looks like this

package model;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import org.joda.time.LocalDateTime;

public abstract class ModelObject  {

private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

private long id;
private LocalDateTime  modifiedDateTime = new LocalDateTime ();
private LocalDateTime  creationDateTime = new LocalDateTime ();

public Long getId(){
    return id;
}

public void setId(Long id){
    firePropertyChange("id", this.id, this.id = id);
}

public LocalDateTime  getModifiedDateTime(){
    return modifiedDateTime;
}

public void setModifiedDateTime(LocalDateTime  modifiedDateTime) {
    firePropertyChange("modifiedDateTime", 
                this.modifiedDateTime, 
                this.modifiedDateTime = modifiedDateTime);
}

public LocalDateTime  getCreationDateTime(){
    return creationDateTime;
}

public void setCreationDateTime(LocalDateTime  creationDateTime){
    firePropertyChange("creationDateTime", 
                this.creationDateTime, 
                this.creationDateTime = creationDateTime);
}

public void addPropertyChangeSupportListener(PropertyChangeListener listener){
    changeSupport.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener){
    changeSupport.removePropertyChangeListener(listener);
}

public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener){
    changeSupport.addPropertyChangeListener(propertyName, listener);
}

public void removePropertyChangeListener(String propertyName, 
                                             PropertyChangeListener listener){
    changeSupport.addPropertyChangeListener(propertyName, listener);
}

protected void firePropertyChange(String propertyName, Object oldValue, Object newValue){
    changeSupport.firePropertyChange(propertyName, oldValue, newValue);
}

}

I have added usertype.spi-3.0.0.jar and usertype.core.3.0.0.CR3.jar to my class path.

I have no idea what is going on here so help would be greatly appreciated


Solution

  • Add separate file with HBM custom types
    CustomTypes.hbm.xml

    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="com.softcomputer.softlab.business.core">
       <typedef name="localJodaDate"
          class="org.jadira.usertype.dateandtime.jsr310.PersistentLocalDateTime" />
    </hibernate-mapping>  
    

    include this file to your mapping

      <mapping resource="CustomTypes.hbm.xml" />