Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7633685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:57:18+00:00 2026-05-31T06:57:18+00:00

Situation : I have a persistable class with variable of java.util.Date type: import java.util.Date;

  • 0

Situation:

I have a persistable class with variable of java.util.Date type:

import java.util.Date;

@Entity
@Table(name = "prd_period")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Period extends ManagedEntity implements Interval {

   @Column(name = "startdate_", nullable = false)
   private Date startDate;

}

Corresponding table in DB:

CREATE TABLE 'prd_period' (
  'id_' bigint(20) NOT NULL AUTO_INCREMENT,
 ...
  'startdate_' datetime NOT NULL
)

Then I save my Period object to DB:

Period p = new Period();
Date d = new Date();
p.setStartDate();
myDao.save(p);

After then if I’m trying to extract my object from DB, it is returned with variable startDate of Timestamp type – and all the places where I’m trying to use equals(…) are returning false.

Question: are there any means to force Hibernate to return dates as object of java.util.Date type instead of Timestamp without explicit modification of every such variable (e.g it must be able just work, without explicit modification of existed variables of java.util.Date type)?

NOTE:

I found number of explicit solutions, where annotations are used or setter is modified – but I have many classes with Date-variables – so I need some centralized solution and all that described below is not good enough:

  1. Using annotation @Type: – java.sql.Date will be returned

    @Column
    @Type(type="date")
    private Date startDate;
    
  2. Using annotation @Temporal(TemporalType.DATE) – java.sql.Date will be returned

    @Temporal(TemporalType.DATE)
    @Column(name=”CREATION_DATE”)
    private Date startDate;
    
  3. By modifying setter (deep copy) – java.util.Date will be returned

    public void setStartDate(Date startDate) {
        if (startDate != null) {
            this.startDate = new Date(startDate.getTime());
        } else {
            this.startDate = null;
        }
    }
    
  4. By creation of my own type: – java.util.Date will be returned

Details are given here:
http://blogs.sourceallies.com/2012/02/hibernate-date-vs-timestamp/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:57:19+00:00Added an answer on May 31, 2026 at 6:57 am

    So, I spent some time with this issue and found a solution. It is not pretty one, but at least a start point – maybe someone will supplement this with some useful comments.

    Some info about mapping that I found in process:

    • Class that contains basic mapping of Hibernate types to property types is org.hibernate.type.TypeFactory. All this mappings are stored in unmodifiable map

      private static final Map BASIC_TYPES;
      ...
      basics.put( java.util.Date.class.getName(), Hibernate.TIMESTAMP );
      ...
      BASIC_TYPES = Collections.unmodifiableMap( basics );
      

    As you can see with java.util.Date type assosited with Hibernate type org.hibernate.type.TimestampType

    • Next interesting moment – creation of Hibernate org.hibernate.cfg.Configuration – object that contains all info about mapped classes. This classes and their properties can be extracted like this:

      Iterator clsMappings = cfg.getClassMappings();
      while(clsMappings.hasNext()){
          PersistentClass mapping = (PersistentClass) clsMappings.next();
          handleProperties(mapping.getPropertyIterator(), map);
      }
      
    • Vast majority of properties are the objects of org.hibernate.mapping.SimpleValue types. Our point of interest is the method SimpleValue.getType() – in this method is defined what type will be used to convert properties values back-and-forth while working with DB

      Type result = TypeFactory.heuristicType(typeName, typeParameters);
      

    At this point I understand that I am unable to modify BASIC_TYPES – so the only way – to replace SimpleValue object to the properties of java.util.Date types to my custom Object that will be able to know the exact type to convert.

    The solution:

    • Create custom container entity manager factory by extending HibernatePersistence class and overriding its method createContainerEntityManagerFactory:

      public class HibernatePersistenceExtensions extends HibernatePersistence {
      
          @Override
          public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) {
      
              if ("true".equals(map.get("hibernate.use.custom.entity.manager.factory"))) {
                  return CustomeEntityManagerFactoryFactory.createCustomEntityManagerFactory(info, map);
              } else {
                  return super.createContainerEntityManagerFactory(info, map);
              }
          }
      }
      
    • Create Hibernate configuration object, modify value ojects for java.util.Date properties and then create custom entity manager factory.

      public class ReattachingEntityManagerFactoryFactory {
      
      
          @SuppressWarnings("rawtypes")
          public static EntityManagerFactory createContainerEntityManagerFactory(
          PersistenceUnitInfo info, Map map) {
              Ejb3Configuration cfg = new Ejb3Configuration();
      
              Ejb3Configuration configured = cfg.configure( info, map );
      
              handleClassMappings(cfg, map);
      
              return configured != null ? configured.buildEntityManagerFactory() : null;
          }
      
          @SuppressWarnings("rawtypes")
          private static void handleClassMappings(Ejb3Configuration cfg, Map map) {
              Iterator clsMappings = cfg.getClassMappings();
              while(clsMappings.hasNext()){
                   PersistentClass mapping = (PersistentClass) clsMappings.next();
                   handleProperties(mapping.getPropertyIterator(), map);
              }
          } 
      
      
      
          private static void handleProperties(Iterator props, Map map) {
      
              while(props.hasNext()){
                   Property prop = (Property) props.next();
                   Value value = prop.getValue();
                   if (value instanceof Component) {
                       Component c = (Component) value;
                       handleProperties(c.getPropertyIterator(), map);
                   } else {
      
                       handleReturnUtilDateInsteadOfTimestamp(prop, map);
      
                   }
               }
      
          private static void handleReturnUtilDateInsteadOfTimestamp(Property prop, Map map) {
              if ("true".equals(map.get("hibernate.return.date.instead.of.timestamp"))) {
                  Value value = prop.getValue();
      
                  if (value instanceof SimpleValue) {
                      SimpleValue simpleValue = (SimpleValue) value;
                      String typeName = simpleValue.getTypeName();
                      if ("java.util.Date".equals(typeName)) {
                          UtilDateSimpleValue udsv = new UtilDateSimpleValue(simpleValue);
                          prop.setValue(udsv);
                      }
                  }
              }
          }
      
      }
      

    As you can see I just iterate over every property and substitute SimpleValue-object for UtilDateSimpleValue for properties of type java.util.Date. This is very simple class – it implements the same interface as SimpleValue object, e.g org.hibernate.mapping.KeyValue. In constructor original SimpleValue object is passed – so every call to UtilDateSimpleValue is redirected to the original object with one exception – method getType(…) return my custom Type.

    public class UtilDateSimpleValue implements KeyValue{
    
        private SimpleValue value;
    
        public UtilDateSimpleValue(SimpleValue value) {
            this.value = value;
        }
    
        public SimpleValue getValue() {
            return value;
        }
    
        @Override
        public int getColumnSpan() {
            return value.getColumnSpan();
        }
    
        ...
    
        @Override
        public Type getType() throws MappingException {
            final String typeName = value.getTypeName();
    
            if (typeName == null) {
                    throw new MappingException("No type name");
            }
    
            Type result = new UtilDateUserType();
    
            return result;
        }
        ...
    }
    
    • And the last step is implementation of UtilDateUserType. I just extend original org.hibernate.type.TimestampType and override its method get() like this:

      public class UtilDateUserType extends TimestampType{
      
          @Override
          public Object get(ResultSet rs, String name) throws SQLException {
              Timestamp ts = rs.getTimestamp(name);
      
              Date result = null;
              if(ts != null){
                  result = new Date(ts.getTime());
              }
      
              return result;
          }
      }
      

    That is all. A little bit tricky, but now every java.util.Date property is returned as java.util.Date without any additional modifications of existing code (annotations or modifying setters). As I find out in Hibernate 4 or above there is a much more easier way to substitute your own type (see details here: Hibernate TypeResolver). Any suggestions or criticism are welcome.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have situation where my Java class needs to create a ton of certain
Situation I have a template class TIppImage<T> for image of type T . I
Pseudo-situation: have a class (let's say BackgroundMagic ), and it has Start() and Stop()
i have situation like this: class IData { virtual void get() = 0; virtual
I have situation like this: // Object Class class Person_Object { protected $_id; public
I have situation to extend the Enumerable class in c# to add the new
Situation I have web application I have class which does complicated mathematics computation Equations
Situation I have an Activity with an AutoCompleteTextView. As you type, the AutoCompleteTextView finds
Situation: I have this log4j logger: private static final Logger logger = Logger.getLogger(ThisClassName.class); And
I have situation where I am mapping table's columns to the primary key of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.