-->
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.  [ 1 post ] 
Author Message
 Post subject: How to define default values for columns in hbm.xml files!!
PostPosted: Tue Apr 01, 2008 9:52 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


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

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.