grails / gorm-hibernate5

GORM for Hibernate 5
Apache License 2.0
64 stars 70 forks source link

Auto timestamp violations with multiple datasources #211

Open nmkae opened 4 years ago

nmkae commented 4 years ago

Background

Adding automatic timestamp properties (dateCreated and lastUpdated) into existing Micronaut applications with multiple data sources is causing violation exceptions when attempting to save any domain class.

Alternate attempts to use event handlers (beforeInsert, beforeUpdate) did not resolve the issue and did not appear to be called.

Steps to reproduce

  1. Setup an application with multiple data sources and domain classes that have auto timestamp properties
  2. Save domain class

Expected behaviour

Instance can be saved with dateCreated and lastUpdated values automatically set

Actual behaviour

Attempts to save instance causes violation exceptions due to dateCreated value being null

Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "DATE_CREATED"; SQL statement:
insert into time_test (id, version, date_created, last_updated, description) values (null, ?, ?, ?, ?) [23502-199]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:457)
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:427)
    at org.h2.message.DbException.get(DbException.java:205)
    at org.h2.message.DbException.get(DbException.java:181)
    at org.h2.table.Column.validateConvertUpdateSequence(Column.java:418)
    at org.h2.table.Table.validateConvertUpdateSequence(Table.java:824)
    at org.h2.command.dml.Insert.insertRows(Insert.java:175)

Environment Information

Operating System: Ubuntu 18.04.3 LTS GORM Version: 7.0.3.RELEASE Grails Version (if using Grails): N/A JDK Version: Java 8

Example Application

A Micronaut application with multiple datasources and simple endpoint that saves domain class instance.

https://github.com/nmkae/mn-auto-ts

Related Issues

Unclear if this is a Micronaut, hibernate or GORM issue.

Suggested solution in the Micronaut issue is not suitable, as the applications need to support multiple datasources. https://github.com/micronaut-projects/micronaut-core/issues/1014

luke10 commented 3 years ago

@puneetbehl Hi. Are you able to give an update on this ticket? It still does not appear to be working with Gorm on Micronaut version 2. Thanks.