1

My file has field -

<property name="createdTs" type="timestamp">
    <column name="created_ts" length="29" />
</property>

When I save the table with -

public void persist(DrRequest transientInstance) {
        log.debug("persisting DrRequest instance");

        try {

            Transaction trans=sessionFactory.getCurrentSession().beginTransaction();
            sessionFactory.getCurrentSession().persist(transientInstance);
            trans.commit();
            log.debug("persist successful");
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }

It doesn't saves the created_ts field with current time in postgresql database table.

The type of created_ts is timestamp without time zone.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
sjain
  • 23,126
  • 28
  • 107
  • 185

1 Answers1

1

If you generate your DB schema with hibernate.hbm2ddl.auto, you need to instruct Hibernate to include a default "CURRENT_TIMESTAMP" directive:

<column  name="created_ts" length="29" sql-type="timestamp without time zone" default="CURRENT_TIMESTAMP"/>

If you have an existing schema, you need to alter it to add the DEFAULT value for this timestamp column.

Hibernate will not be aware of the database generated value, so you need to refresh your entity, after you persisted it and you flushed the Hibernate session:

session.flush();
session.refresh(transientInstance);

This way you can reload the entity and fetch the database generated values too.

Community
  • 1
  • 1
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • `If you have an existing schema, you need to alter it to add the DEFAULT value for this timestamp column` - What should be the DEFAULT value entry for the current time at this case ? – sjain Jan 22 '15 at 10:42
  • DEFAULT current_timestamp, according to [the reference documentation](http://www.postgresql.org/docs/8.2/static/sql-createtable.html) – Vlad Mihalcea Jan 22 '15 at 11:10