|
When a java "double" type is defined in an entity class, a "float" type column is created in oracle database when setting hibernate.hbm2ddl.auto=create(or update).
The java "double" type is interpreted as "double precision" in the table creation script by Hibernate. For example,
a table create script could look like:
CREATE TABLE MyUser ( user_id NUMBER(9) PRIMARY KEY, height double precision NOT NULL)
It generates a column "height" of type "float".
The same table creation script generates type "double" in mysql database.
It appears that oracle database doesn't have a data type "double". It has "BINARY_DOUBLE" instead.
In addition, after a double type being created as "float" in the database, if setting the hibernate.hbm2ddl.auto=validate, it throws error:
Caused by: org.hibernate.HibernateException: Wrong column type in xxx for column xxx. Found: float, expected: double precision at org.hibernate.mapping.Table.validateColumns(Table.java:284) at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1129) at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:139) at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:360) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1340) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:670)
Strong Liu has suggested to change the default type mapping in org.hibernate.dialect.Oracle8iDialect from registerColumnType( Types.DOUBLE, "double precision" ); to registerColumnType( Types.DOUBLE, "float" );
I have tried to create my own dialect to override the default behavior.
For example:
import org.hibernate.dialect.Oracle10gDialect; import java.sql.Types; public class Oracle11gDialectExt extends Oracle10gDialect {
public Oracle11gDialectExt() { super(); registerColumnType(Types.DOUBLE, "float"); }
}
And then set my own dialect in the persistence.xml, for example: <property name="hibernate.dialect" value="jpa.test.hibernate.dialect.Oracle11gDialectExt" />
Then I don't see the error any more.
|