-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Create/Alter and Drop DDL generation - hbm2ddl help needed
PostPosted: Fri Mar 28, 2008 12:06 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Hi,
I use hbm2ddl for ddl generation. Is there a way to say create/alter a table while generating the ddl. Example, I have table A. If I make changes to the hbm for table A and use hbm2ddl, I should get a ddl that says create/alter table A. I need this because table A may have valid business data. If get only the create DDL, I have to drop the table to execute the create table DDL. This can be avoided if I get an alter ddl also. Also, I want to know if we can get the drop table DDL along with the create DDLs.

Thanks in advance for any help!

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 31, 2008 6:10 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Hi,
Does anyone have any idea on how to solve the issue I mentioned in the post earlier???? Can anyone tell me how to control the properties of the generated DDLs when using hbm2ddl tool. Even a small hint could help me solve the problem. I need drop and alter DDLs to be generated along with the create DDLs. Help Please!

_________________
Sukirtha


Top
 Profile  
 
 Post subject: Pleaase Help
PostPosted: Mon Mar 31, 2008 7:51 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Ok I know how to generate the drop DDL using hbm2ddl tool. I just added drop="yes" in the hbm2ddl tag of the ant script.

Code:
<hbm2ddl export="false" outputfilename="application.ddl" drop="yes" >
</hbm2ddl>


But I am still stuck with the alter DDLs. Does hibernate provide a way to generate alter DDLs for tables that are already present in the schema? Or should I add it as a feature request to JIRA? I guess this feature is vital in many cases. We cannot create a table when all we did was make a minor change to the table design. Any suggestions???? Pleeease Help!

_________________
Sukirtha


Last edited by Sukirtha on Mon Mar 31, 2008 7:54 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 31, 2008 7:52 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Ok I know how to generate the drop DDL using hbm2ddl tool. I just added drop="yes" in the hbm2ddl tag of the ant script.

Code:
<hbm2ddl export="false" outputfilename="application.ddl" drop="yes">
</hbm2ddl>


But I am still stuck with the alter DDLs. Does hibernate provide a way to generate alter DDLs for tables that are already present in the schema? Or should I add it as a feature request to JIRA? I guess this feature is vital in many cases. We cannot create a table when all we did was make a minor change to the table design. Any suggestions???? Pleeease Help!

_________________
Sukirtha


Last edited by Sukirtha on Mon Mar 31, 2008 7:54 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 31, 2008 7:52 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Ok I know how to generate the drop DDL using hbm2ddl tool. I just added drop="yes" in the hbm2ddl tag of the ant script.

Code:
<hbm2ddl export="false" outputfilename="application.ddl" drop="yes">
</hbm2ddl>


But I am still stuck with the alter DDLs. Does hibernate provide a way to generate alter DDLs for tables that are already present in the schema? Or should I add it as a feature request to JIRA? I guess this feature is vital in many cases. We cannot create a table when all we did was make a minor change to the table design. Any suggestions???? Pleeease Help!

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 31, 2008 7:55 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Sorry for multiple posts :(

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 02, 2008 3:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
schemaupdate.

but it is not something I would use automatically against production systems.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 02, 2008 7:14 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Hi Max,
Thanks for the reply. I got a sample of how to use schemaupdate from the this URL http://www.hibernate.org/385.html?cmd=comphist&histnode=2801 as well as from the hibernate tools reference guide 3.2.0 beta 10.

Following is my ant script:

Code:
<hibernatetool destdir="C:/DDL">
           <configuration propertyfile="hibernate.properties">
   <fileset dir="C:/HBM" id="id">
            <include name="application.hbm.xml"/>
   </fileset>
         </configuration>
         <hbm2ddl outputfilename="application.ddl" export="false" update="true"/>
        </hibernatetool>


This is what I get in my console.
Code:
     [echo] Starting Build...
     [echo] Performing hbm2ddl..
[hibernatetool] Executing Hibernate Tool with a Standard Configuration
[hibernatetool] 1. task: hbm2ddl (Generates database schema)
     [echo] DDL generated...

BUILD SUCCESSFUL


However, I do not get the DDL generated nor is my schema getting updated automatically. What am I missing? I want the ddl to be stored in a file. I do not want an automatic export of the schema. I guess the update is not working in my case. If i remove update = "true" ddls are getting generated

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 02, 2008 7:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
argh..we still have the bug of schemaupdate not generating output correctly ;(

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 02, 2008 8:03 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Oh! So it's a bug? Are u sure? I am somehow still suspicious of my script only. If possible please try for yourself once and let me know. Meanwhile, I will check my configurations again.

Thanks for your help anyway!

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 02, 2008 11:53 pm 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
I checked all the configurations. No luck. Its a bug in schemaupdate.

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 03, 2008 9:17 am 
Newbie

Joined: Tue Mar 25, 2008 4:14 am
Posts: 7
Location: Israel
You can use this mapping:

create an hbm file as shown bellow:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<database-object>
<create>
CREATE OR REPLACE TRIGGER "USER_UPDATE_TRIGGER"
AFTER UPDATE ON IQ_USER
REFERENCING OLD AS oldrow NEW AS newrow
FOR EACH ROW
BEGIN
update shard_local_user slu
set slu.user_name = :newrow.user_name,
slu.display_name = :newrow.display_name,
slu.biography = :newrow.biography
where slu.iq_user_id = :newrow.iq_user_id;
END;
</create>
<drop>
</drop>
</database-object>
</hibernate-mapping>

whatever is in <create> tag will work...

_________________
Best regards,
Michael Gantman


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 04, 2008 4:27 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Thanks for the reply. But can you please explain what is happening here? I am bit weak at the sql query writing side... ;) And is this the only way to do it? Also is it portable to all types of databases. Is this the ideal way to do things? I want the generated ddls to be portable. Thats top priority in my scenario.

_________________
Sukirtha


Top
 Profile  
 
 Post subject: The way to do it
PostPosted: Sun Apr 06, 2008 3:11 am 
Newbie

Joined: Tue Mar 25, 2008 4:14 am
Posts: 7
Location: Israel
I am not sure if this is the only way to do it. When I struggled with hbm2ddl I found that this was working alternative. However, I also ran into some problems with it. Some of my statements worked just fine. I had no problems creating several DB VIEWs in that way. But when I tried to create some triggers I only could create one. If I tried to create more then one it just failed no matter what I did. I tried to separate my statements into separate files and tried all kind of other things but nothing helped. As for portability - since as you already noticed you have to write SQL yourself portability is totally up to you. If the SQL you write is portable then it will work on other DBs if your SQL is DB specific then no. If you are not good enough with writting SQLs consult your DBA. As far as Hibernate supporting <database-object> tag - I do believe it is DB independed. As far as my problem that I described above I had to resort to not so pretty solution. I created a standalone java program that loads up current Hibernate.cfg file and adds hbm2ddl property hardcoded. (I removed that property from my cfg file). Then I open and close hibernate session. That creates my db structure. Then I run a method in which I use jdbc to create some extra db objects. You can of course use the same to alter the objects. Here is a sample of my code:

package <package_name>;

import ...;
.....


public class SetupDb {

/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException,
IOException {

Configuration cfg = new Configuration();
Properties props = new Properties();
props.load(new FileReader(<FULL_PROPERTIES_FILE_NAME>);
cfg.setProperties(props);
cfg.setProperty("hibernate.hbm2ddl.auto", "create");
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session sess = sf.openSession();
sess.close();
createTriggers(props);
}

private static void createTriggers(Properties props) {
String sqlStatement1 = "...";
String sqlStatement2 = "...";
String sqlStatement3 = "...";

try {
OracleDataSource ods = new OracleDataSource();
ods.setURL(props.getProperty("hibernate.connection.url"));
ods.setUser(props.getProperty("hibernate.connection.username"));
ods.setPassword(props.getProperty("hibernate.connection.password"));
Statement stmt = ods.getConnection().createStatement();
stmt.execute(sqlStatement1);
stmt.execute(sqlStatement2);
stmt.execute(sqlStatement3);
} catch (SQLException se) {
se.printStackTrace();
}

}

}

_________________
Best regards,
Michael Gantman


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 1:55 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Thanks Michael. Just one more doubt. This trigger that you have shared... What does it do? Sorry for the dumb question. As mentioned earlier, I am not very familiar with SQL. I need something that will help me generate alter DDLs if I have done any changes to the hbm file. Do you have a trigger for that? Can you please share that with me if you have? I guess I am still not clear of how this trigger thing works. The database-object tag creates/drops/alters something in a database schema (mentioned in the configuration file) is it? Or can I also use it to save the DDLs (create/alter/drop DDls) in some target file and use those DDLs later in whichever schema I want? Please let me know if I am not clear! :)

_________________
Sukirtha


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.