Hello all,
There is a way to use default values defined on DB Tables, when using the Reverse engineering tools of hibernate.
A) First, before you generate, you have to specify the datatypes that you want to use to map the DB datatypes.
To make an example: normally number(4) in oracle is mapped to "short", this means that when this datatype is instanciated it automatically takes the default value "0". Then any default value you set on DB is ignored. To avoid this problem you have to specify that you want to use "java.lang.Short" as datatype. In this way you can pass a "null" object, and the correct default value will be used.
To configure these Datatype mappings please see:
http://www.hibernate.org/hib_docs/tools ... ering.html
Exactly paragraph: 5.2.2. Type mappings (<type-mapping>)
There is an explanation on how to customize your datatype mappings.
Here how my reveng.xml file looks:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
<type-mapping>
<sql-type jdbc-type="DECIMAL" precision="1" scale="0" not-null="true" hibernate-type="java.lang.Short" />
<sql-type jdbc-type="DECIMAL" precision="1" scale="0" not-null="false" hibernate-type="java.lang.Short" />
<sql-type jdbc-type="VARCHAR" length="1" not-null="true" hibernate-type="java.lang.Character"/>
<sql-type jdbc-type="VARCHAR" length="1" not-null="false" hibernate-type="java.lang.Character"/>
</type-mapping>
<table-filter match-name="ANAG_ESTERNE" />
<table-filter match-name="ASSISTIBILI" />
<table-filter match-name="BASICDATATYPE" />
B) Second, in the generated HBM.XML files you need to specify in the Tag <class> the attributes <dynamic-insert> and <dynamic-update>, and set the 2 attributes to "true". This tells Hibernate that it shouldn't include the properties that are "null" in the INSERT or UPDATE statements. In this way the correct Default values you defined on the Database Tables will be used.
For ex:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 4-apr-2008 16.09.32 by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
<class name="com.reveng.Syncromedfield" table="SYNCROMEDFIELD" dynamic-insert="true" dynamic-update="true">
<composite-id name="id" class="com.reveng.SyncromedfieldId">
<key-property name="syncromedsegmentfk" type="string">
<column name="SYNCROMEDSEGMENTFK" length="10" />
</key-property>
<key-property name="seqNo" type="big_decimal">
<column name="SEQ_NO" precision="22" scale="0" />
</key-property>
</composite-id>
<property name="mycolumn" type="java.lang.Short">
<column name="MYCOLUMN" precision="1" scale="0" not-null="false"/>
</property>
Unfortunately this works only if the <column> tag contains the property <not-null="false">.
If you have columns declared NOT NULL on the database, you manually need to edit the hbm.xml file and change <not-null="true"> to <not-null="false">. This is not a problem, because if you do not set any value for that property the Default will be used.
Greetings,
Fabio