I am upgrading Hibernate from 5 to 6 and having issues with the reverse engineering. I want all entities to continue using java.util.Date for now, but seems the mapping is now java.sql.Timestamp. I am running it through the maven-antrun-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution> <!--mvn antrun:run@hbm2java -->
<id>hbm2java</id>
<phase>none</phase>
<configuration>
<target>
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" />
<hibernatetool destdir="${reveng.destdir}">
<classpath>
<path location="${basedir}/target/classes" />
</classpath>
<jdbcconfiguration propertyfile="path/to/hibernate.properties" revengfile="path/to/hibernate.reveng.xml" packagename="${reveng.packagename}" detectmanytomany="true" />
<hbm2java jdk5="true" ejb3="true" />
</hibernatetool>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.hibernate.tool</groupId>
<artifactId>hibernate-tools-ant</artifactId>
<version>6.6.2.Final</version>
</dependency>
</dependencies>
</plugin>
I am planning on migrating java.util.Date to java.time, however that is a much larger project and not in scope for this upgrade.
I am looking to have the entities continue to show dates like:
@Temporal(TemporalType.TIMESTAMP)
@Column(name="created")
private Date created;
However it is generating as:
@Column(name="created")
private Timestamp created;
Setting the type mapping to java.util.Date ends up setting java.sql.Timestamp:
<hibernate-reverse-engineering>
<type-mapping>
<sql-type jdbc-type="DATE" hibernate-type="java.util.Date"/>
<sql-type jdbc-type="TIMESTAMP" hibernate-type="java.util.Date"/>
</type-mapping>
</hibernate-reverse-engineering>
Not setting JDBC DATE to java.util.Date defaults to java.sql.Date, so I know the mappings is doing something, just not sure how it is overriding and how to prevent that.
I tried setting the type on the column as well, which does the same thing.
Any help on how to make the generation to use java.util.Date would be appreciated.
I ended up overriding this through templates. You set the templatepath attribute in the plugin and write a template files. The specific part that does this for me is:
<#foreach property in pojo.getAllPropertiesIterator()>
<#if pojo.getMetaAttribAsBool(property, "gen-property", true)>
<#assign javaType = pojo.getJavaTypeName(property, jdk5)>
<#if javaType == "java.sql.Date" || javaType == "Timestamp">
<#assign javaType = '${pojo.importType("java.util.Date")}'>
</#if>
${pojo.getFieldModifiers(property)} ${javaType} ${c2j.keyWordCheck(property.name)}<#if pojo.hasFieldInitializor(property, jdk5) && !(c2h.isManyToOne(property) && property.getValue()?exists)> = ${pojo.getFieldInitialization(property, jdk5)}</#if>;
</#if>
</#foreach>