-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: How to use Column Default Values defined on Database!!
PostPosted: Tue Apr 01, 2008 9:51 am 
Newbie

Joined: Mon Oct 01, 2007 3:39 am
Posts: 11
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


Last edited by daprile fabio on Mon Apr 07, 2008 4:33 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 01, 2008 4:33 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
There's no provision in "generated" to ignore that if the property is null. However, you're working too hard. The easiest way to create a default value for a field is just to initialize the variable with whatever default value you want. Then if you don't set any value before saving, the default will be in effect and neither Hibernate nor your database has to know about it. It's all about your domain model anyhow right?

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 04, 2008 4:11 am 
Newbie

Joined: Mon Oct 01, 2007 3:39 am
Posts: 11
Hello,

The default values are already defined on the database, i do not want to redefine all the defaults in the code.

What you say could be useful if you define the mappings and you generate the database from the mappings, but i'm doing a reverse engineering.

Fabio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 04, 2008 9:45 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Hibernate Tools does not reverse engineer defaults. Contributions (working ones) are of course welcome! :)

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject: How to use Column Default Values defined on Database!!
PostPosted: Mon Apr 07, 2008 4:31 am 
Newbie

Joined: Mon Oct 01, 2007 3:39 am
Posts: 11
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 04, 2009 3:19 pm 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
cbredesen wrote:
Hibernate Tools does not reverse engineer defaults. Contributions (working ones) are of course welcome! :)


Sorry for reviving this age-old thread... but I see a candidate for exactly this sleeping in http://opensource.atlassian.com/project ... se/HBX-911 since March 2007.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.