简体   繁体   中英

Mapping Oracle types TimestampWithTimezone and TimestampWithLocalTimeZone to Hibernate types in reverse engineering

I have a table with three columns with sql type as TimeStamp,TimeStampWithTimeZone and TimeStampWithLocalTimeZone.

In the hibernate reverse engineer process,

TimeStamp is correctly mapped to Timestamp .

But TimeStampWithTimeZone and TimeStampWithTimeZone is mapped to Seralizable , expecting both should map to TimeStamp .

After going though this link ,I have add below mapping in hibernate.reveng.xml.

<sql-type jdbc-type="OTHER" hibernate-type="java.sql.Timestamp" />

But still I they are mapping it to Seralizable.Do any one faced this issue ?

Resolved it by extending default RevengNamingStrategy.

Approach : Sql Types of TIMESTAMP_WITH_TIMEZONE and TIMESTAMP_WITH_LOCAL_TIMEZONE types are -101 and -102 and as there is no hibernate mapping types in java.sql.Types for these types,hence they are mapping to seralizable.

So Wrote my own RevengNamingStrategy,which converts these type to Timestamp.Which intern converts to hibernate TimeStampType.

public class OracleRevengNamingStrategy extends DefaultRevengNamingStrategy {

    private static final Integer TIMESTAMP_WITH_TIMEZONE_SQL_CODE = -101;

    private static final Integer TIMESTAMP_WITH_LOCAL_TIMEZONE_SQL_CODE = -102;


    public OracleRevengNamingStrategy(ReverseEngineeringStrategy delegate) {
        super(delegate);
    }

    // Converts Timestamp with tomezone and Time stamp with local time zone to Timestamp
    @Override
    public String columnToHibernateTypeName(TableIdentifier table, String columnName, int sqlType, int length, int precision, int scale,
                                            boolean nullable, boolean generatedIdentifier) {
        String type;

        if (sqlType == TIMESTAMP_WITH_TIMEZONE_SQL_CODE || sqlType == TIMESTAMP_WITH_LOCAL_TIMEZONE_SQL_CODE) {
            type = "timestamp";
        } else {
            type = super.columnToHibernateTypeName(table, columnName, sqlType, length, precision, scale, nullable, generatedIdentifier);
        }

        return type;
    }

}

Please let me know if any other approaches to handle this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM