ClementC (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiYTdmMGMzYTE4...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16360?atlOrigin=eyJpIjoiYTdmMG...
) HHH-16360 (
https://hibernate.atlassian.net/browse/HHH-16360?atlOrigin=eyJpIjoiYTdmMG...
) On Postgresql with hbm2ddl.auto=update, timestamp columns are updated even when correct
(
https://hibernate.atlassian.net/browse/HHH-16360?atlOrigin=eyJpIjoiYTdmMG...
)
Issue Type: Bug Affects Versions: 6.2.0.CR4 Assignee: Unassigned Components:
hibernate-jpamodelgen Created: 23/Mar/2023 03:52 AM Environment: Postgresql 14 Priority:
Major Reporter: ClementC (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
)
With hbm2ddl.auto=update on Postgresql, columns for properties persisted as timestamps
(like Instant, OffsetDateTime, LocalDateTime, …) are updated on every startup, even if the
alter statement does not perform any actual change.
hbm2ddl.auto=validate does not detect any problem.
This did not happen on 6.1.7 (because it did not have the capability to update existing
columns anyway).
*Sample*
My entity:
@Entity
@Getter
@Setter
public class Test {
@Id
private String id;
private Instant instant;
private OffsetDateTime offsetDateTime;
private ZonedDateTime zonedDateTime;
private LocalDateTime localDateTime;
private Date date;
}
Startup logs (even when columns already exist with the right type):
[Hibernate] alter table if exists auth0.test alter column date set data type timestamp(6)
[Hibernate] alter table if exists auth0.test alter column instant set data type
timestamp(6) with time zone
[Hibernate] alter table if exists auth0.test alter column local_date_time set data type
timestamp(6)
[Hibernate] alter table if exists auth0.test alter column offset_date_time set data type
timestamp(6) with time zone
[Hibernate] alter table if exists auth0.test alter column zoned_date_time set data type
timestamp(6) with time zone
*Quick analysis (using 6.2.0.CR4)*
With hbm2ddl.auto=update: StandardTableMigrator#sqlAlterStrings checks both type and
length
//StandardTableMigrator line 88
if ( !ColumnDefinitions.hasMatchingType( column, columnInformation, metadata, dialect )
|| !ColumnDefinitions.hasMatchingLength( column, columnInformation, metadata,
dialect ) )
With hbm2ddl.auto=validate: AbstractSchemaValidator#validateColumnType only checks the
type (which matches)
//AbstractSchemaValidator line 163
if ( !ColumnDefinitions.hasMatchingType( column, columnInformation, metadata, dialect ) )
{
So the problem is in ColumnDefinitions.hasMatchingLength :
final int actualSize = columnInformation.getColumnSize();
if ( actualSize == 0 ) {
return true ;
}
else {
final Size size = column.getColumnSize( dialect, metadata );
final Long requiredLength = size.getLength();
final Integer requiredPrecision = size.getPrecision();
return requiredLength != null && requiredLength == actualSize
|| requiredPrecision != null && requiredPrecision == actualSize
|| requiredPrecision == null && requiredLength == null ;
}
For a timestamp with zone column:
* actualSize = 35 (no idea why)
* requiredLength is null
* requiredPrecision = 6
* ==> requiredPrecision != null && requiredPrecision == actualSize is false
(
https://hibernate.atlassian.net/browse/HHH-16360#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16360#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100219- sha1:0c2c9f2 )