-->
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.  [ 5 posts ] 
Author Message
 Post subject: Middlegen generate default values for columns
PostPosted: Sun Jan 28, 2007 2:42 pm 
Newbie

Joined: Sun Jan 28, 2007 2:22 pm
Posts: 3
Location: Menlo Park, CA
I've inherited a codebase that uses Middlegen to generate hbm.xml files from a MySQL database.

Hibernate has an option to specify the default value for a column in a mapping, and from what I can tell, Middlegen code detects default values, but it doesn't write them to the xml file. Is there a way to configure Middlegen to so do? I've hunted around for awhile but haven't been able to find the answer. Perhaps this isn't possible?

In particular, I'd like to be able to generated something like:

<property
name="created"
type="java.sql.Timestamp"
column="created"
default="CURRENT_TIMESTAMP" (<--- or whatever)
not-null="true"
length="19"
/>

I'd rather not have to edit xml files by hand and it seems a little redundant to indicate default values in the MySQL schema and the Java code.

Thanks for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 28, 2007 3:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what would be the point of specifying a default-type reverse engineering from a db when the default-type is only used when you use schemaexport (which one normally does not do when the db already exists)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 28, 2007 6:07 pm 
Newbie

Joined: Sun Jan 28, 2007 2:22 pm
Posts: 3
Location: Menlo Park, CA
I didn't realize that default was only used for schemaexport. I am still getting my head around how all the pieces fits together.

I'll back up a little bit. The root of the problem is that I upgraded MySQL to 5.0.27, which happens to fix a bug with timestamps in which the db now properly indicates them as not-nullable. So now the generated xml file has not-null="true" (as it should) and all the code now breaks when there is not a call like: setDate(new Date()). I could add this everywhere (which is perhaps better, style-wise), but I thought there may be a way to automate the detection of the default value in the db and that I should investigate first. Obviously default isn't helpful...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 11:33 pm 
Newbie

Joined: Sun Jan 28, 2007 2:22 pm
Posts: 3
Location: Menlo Park, CA
Well, after briefly toying around with a custom UserType, I ended up just setting all the timestamps in question to with new Date() after all.

It still leaves me wondering if there isn't a way to extract default values for columns from legacy databases. Again, perhaps you would never want to do this from the bottom up...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 3:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
default values in the database is used when you perform inserts and do *not* specify the value for these columns; then the database will evaluate the default value and use it for creating the row

...hence if you set them in your java object you

a) don't actually use the default values since you already set it to something

b) that "something" might not be what the default value actually should be because e.g. "current_time" will not be evaluated at the time of insert but when you created the object....

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.