In my project, previous hibernate version was 4.3.11.Final. After upgrading to 5.2.10.Final, I changed the code as follows and now my schema diff method acts as create settings. I'm using Oracle12c. I both checks Oracle10gDialect and Oracle12cDialect.
I have a spring bean class called
HibernatePersistorService. Some parts of this class:
Code:
public class HibernatePersistorService {
private Metadata metaData;
private SessionFactory sessionFactory;
private List<Class> entities;
...
@Override
public void init() {
String username = ConfigUtil.getString(true, getConfig("db.username"));
MapBuilder<String, Object> settingsBuilder = new MapBuilder<String, Object>(new HashMap<>())
.put("hibernate.dialect", ConfigUtil.getString(true, getConfig("db.dialect")))
.put("hibernate.connection.driver_class", ConfigUtil.getString(true, getConfig("db.driver")))
.put("hibernate.connection.url", ConfigUtil.getString(true, getConfig("db.url")))
.put("hibernate.connection.username", username)
.put("hibernate.connection.password", ConfigUtil.getString(getConfig("db.password"), ""))
.put("hibernate.show_sql", ConfigUtil.getString(getConfig("db.showSQL"), "false"));
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
StandardServiceRegistry serviceRegistry = serviceRegistryBuilder
.applySettings(settingsBuilder.get())
.configure("hibernate.cfg.xml")
.build();
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
entities.forEach(metadataSources::addAnnotatedClass);
metaData = metadataSources.buildMetadata();
SessionFactoryBuilder sessionFactoryBuilder = metaData.getSessionFactoryBuilder();
sessionFactory = sessionFactoryBuilder.build();
logger.info("HibernatePersistorService init()");
}
...
@Override
public void generateSchemaDiff() {
SchemaUpdate schemaUpdate = new SchemaUpdate();
schemaUpdate.setDelimiter(";");
schemaUpdate.setFormat(true);
schemaUpdate.execute(EnumSet.of(TargetType.STDOUT), metaData);
}
}
And my
hibernate.cfg.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.autocommit">false</property>
<property name="connection.isolation">2</property>
<property name="connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<property name="javax.persistence.validation.mode">NONE</property>
<property name="jdbc.batch_size">5</property>
<property name="jdbc.fetch_size">100</property>
<property name="jdbc.wrap_result_sets">true</property>
<property name="org.hibernate.envers.audit_table_prefix">a_</property>
<property name="org.hibernate.envers.audit_table_suffix"/>
<property name="org.hibernate.envers.revision_field_name">r_num</property>
<property name="org.hibernate.envers.revision_type_field_name">r_type</property>
</session-factory>
</hibernate-configuration>
Now, after initialization (calling
init()), when method
generateSchemaDiff() is called, hibernate generates create statements ignoring what is in the database (i.e. it does not generate the diff and the
alter table add <column> statements). For example, I have a table
t_dmt_user and I added two properties to the class
User. The above code generates all the table from scratch instead of generating
alter table t_dmt_user add <column> for those two added properties.
Can anyone help me on this matter?