users
[Top] [All Lists]

Re: [cinjug-users] Hibernate and database default values

To: Bill Manuel <bill.manuel@xxxxxxxxx>
Subject: Re: [cinjug-users] Hibernate and database default values
From: Jon <inmanjon@xxxxxxxx>
Date: Wed, 20 Jul 2005 21:45:44 -0400
Cc: Kedar Jog <kedar.jog@xxxxxxxxx>, users@xxxxxxxxxx
Delivered-to: mailing list users@cinjug.org
In-reply-to: <bd171aba050720152171d9a708@mail.gmail.com>
Mailing-list: contact users-help@cinjug.org; run by ezmlm
References: <bd171aba05072014081a13ca1b@mail.gmail.com> <d176668005072014186cbb0b6e@mail.gmail.com> <bd171aba050720152171d9a708@mail.gmail.com>
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)
Yes there is.

The XML tag unsaved-value="xxxx" will place that value in a POJO when it is placed in a hibernate transaction scope,  ie. session.saveOrUpdate() method.

I am major league assuming you are creating a new object then creating a new row in a table, thus the SQL error.  If you want a Hibernate error BEFORE you hit the DB, ie. better performance use the not-null="true" tag.  Every tier is a cost, the later the tier the higher the costs, catch this one in the Hibernate tier, not the DB.

For x-doclet(I know nothing about I am an annotator though)  Chris I think mentioned a tag @mandatory? I think in one of his presentations.

Example:
    Car car = new Car();
        (the pojo column value is null)
    session.saveOrUpdate(car);
        (now the column is set with the value from the unsaved-value tag)
    session.commit();
        (now the row can be seen in the DB)

If this doesn't work for you design wise use the tag access="Java.class"  That class should implement Hibernate's Property accessor.  Then you can override the getter/setters and synthesize anything you want.  For instance, we synthesize a legacy DB key.  yech.

Even more psycho...
If your DB has 1500 tables like ours.  Make sure your hibernate map file can generate the same DB schema as your target.  not-nulls, primary keys, etc.  The only way I know to this is to make a DB with the Hibernate Map and and extract the DDL then compare it to the production DB DDL.  Potential future errors will be seen, like POJOs with nulls.

All of these errors are usually design errors, and no tool can fix those.

Later..

Bill Manuel wrote:
I think I could have stated this a little better.  I am trying to figure out how to use the default value on the database.  The database schema is the only place that I am not allowing null values.  I understand that hibernate will insert whatever value that I tell it.  This is what I am trying to prevent.  When I do an insert on a newly created object, I don't want to insert one of the properties, I want the database to set the default value for me.  I know I could just remove the property from the hibernate config, but then I would not be able to read or update the field in the database.  I was hoping that If I set the property to null on the object, hibernate would read my mind and use the database default value.  :)

There certainly has to be a way to do this.

Thanks for the input.

On 7/20/05, Kedar Jog <kedar.jog@xxxxxxxxx> wrote:
Hibernate will try and insert whatever the  value of the property in
your object is if null it will try and persist a null value so on and
so forth

if the column is not null in the schema  why are u passing in a nul
value ? if u expect the value to be null , change your xdoclet tags to
reflect this situation regenerate your hbm files and regen and
redeploy the schema you can also use @hibernate.property formula="some
sql stmt" to add a default computed value....


Kedar Jog
Senior J2EE Engineer
MedPlus (www.medplus.com)


On 7/20/05, Bill Manuel < bill.manuel@xxxxxxxxx> wrote:
> Does anyone know how to make hibernate accept the default values of the
> database on an insert?  The column in the database is not nullable, so when
> I set the value of the property to null, the database crys becasue of the
> null value.
>
> Any help is appreciated.
>
> Thanks
> Bill Manuel
>


--
Regards

Kedar

-----------------------------------
Kedar Jog
5908 Monassas Run Rd
Milford OH 45150

---------
You may unsubscribe from this mailing list
by sending a blank email addressed to:
users-unsubscribe@xxxxxxxxxx

--
Find additional help by sending a blank email
addressed to:
users-help@xxxxxxxxxx


<Prev in Thread] Current Thread [Next in Thread>