Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: Hibernate-Version: 3.2.5.ga
Hello all,
i'm trying to understand how to achive the following functionalities:
A) On ORACLE tables i have some Columns defined with Default values. Is it possible to tell hibernate to consider these Default values during reverse Engineering?
B) How to define Default Values in hbm.xml files?
I have a table Person which includes a column: Age which has a default value : 18
This is my mapping file:
<?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 1-apr-2008 12.13.34 by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
<class name="com.reveng.Persons" table="PERSONS">
<id name="pkpersons" type="int">
<column name="PKPERSONS" precision="9" scale="0" />
<generator class="assigned" />
</id>
<property name="age" type="int">
<column name="AGE" precision="5" scale="0" not-null="true" />
</property>
B-1) I want that if i leave the field empty at insert time the default value defined on the Oracle Column should be used.
Person pers = new Person();
pers.pkpersons = 10;
em.persist(age);
commit();
On database the column Age must have value 18.
I tried to set in the <Property> tag the following Attributes:
insert = "false" or generated = "insert"
, and it works.
B-2) But i would also like that if i set the value of the field, this value is used.
Person pers = new Person();
pers.pkpersons = 10;
pers.age = 20;
em.persist(age);
commit();
This last thing does not work.
On the database i expect to find 20, but i find 18.
The default value is always used.
How to make both things: At insert time use default value if age is left NULL, use the inserted value if age has been given a value.
Greetings,
Fabio