-->
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.  [ 3 posts ] 
Author Message
 Post subject: Initializing Table Data
PostPosted: Wed Jan 25, 2006 12:08 pm 
Newbie

Joined: Wed Jan 25, 2006 11:45 am
Posts: 4
Hi,

I'm using hibernate (annotations) to auto create the structure for the SQL tables in my project. I would however, like to know the best practice for auto initializing a table with some default data.

More specifically I will have a table "Authorities":

|AUTHORITIES |
|ID | ROLE |
----------------------
| 0 | ROLE_XYZ|
| 1 | ROLE_123|

etc...

Users of the system would then associate with this table in a Many-To-One relationship.


Now enough of the boring background story. What I'd like to understand, is the method I should use to have a few ROLE (varchar) entries auto populated into the DB as the table is created.

Ideas I've considered thus far are:
ANT scripts
Alternately, using Enums.

If anyone has an idea on the best way to do this, please let me know.

Thanks in advance.


[b]Hibernate version:3.1.1


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 3:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Hibernate's schema-export will recognize a classpath resource named 'import.sql' as a classpath resource when it is run. That file can contain any arbitrary sql statements you want it to, which are then sent to the database after schema generation has completed.

Another solution is to use AuxliaryDatabaseObjects to populate the table data. In the mapping files, these are represented by <database-object/> elements. Not sure if that feature is available via annotations yet...


Top
 Profile  
 
 Post subject: Cool
PostPosted: Wed Jan 25, 2006 7:38 pm 
Newbie

Joined: Wed Jan 25, 2006 11:45 am
Posts: 4
Thanks Steve, the "import.sql" option I overlooked should work. And you are correct, the second solution is not availalbe in annotations.

This task to to me seems really common, so I was surprised I could not find more posts or reference material describing how to go about doing this. While the 'import.sql' option is certainly appreciated, it unfortuantly couples "initialization" files with "configuration" files. Ahhh well.


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