-->
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.  [ 2 posts ] 
Author Message
 Post subject: Programatically Creating Tables With Columns having length
PostPosted: Wed Jul 25, 2007 4:17 pm 
Newbie

Joined: Wed Jul 25, 2007 4:10 pm
Posts: 4
Hello I am trying to progromatically create tables using the API.

So far I have managed to create tables OK. However I cannot manage to create table where some of the columns have length e.g varchar(25)...

I am using:

Configuration configForMysql = getConfigForMysql5();
Mappings mappings = configForMysql.createMappings();
Table table = mappings.addTable( null, null, "name", null, false );
Column column = new Column( "id" );
SimpleValue simpleValue = new SimpleValue( table );
simpleValue.setTypeName( "int" );
Properties properties = new Properties();
properties.setProperty( "not-null", "true" );
simpleValue.setTypeParameters( properties );
column.setValue( simpleValue );

// column.setPrecision( 4 );
// column.setLength( 5 );
// column.setScale( 5 );
table.addColumn( column );

Connection connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/hbm2ddl", "username", "password" );
SchemaExport export = new SchemaExport( configForMysql, connection );
export.setOutputFile( "C:/make.sql" );
export.execute( true, true, false, false );


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 9:00 am 
Regular
Regular

Joined: Sat Jan 22, 2005 6:57 pm
Posts: 50
Location: Chicago
Well, first I will ask the obvious. Why are you creating tables yourself? Are you using the mapping files and the hibernate configuration file? If so, there is a property you can set in the hibernate mapping file that looks like this:

Code:
   <property name="hibernate.hbm2ddl.auto">update</property>


Just make sure it is set to update and hibernate will create it all for you. In addition, when defining your properties in your mapping files, be sure to set the "length" property of your mapped object's property.

Now, if you really, really want to do this manually (crazy in my book) then you can set the Column.setLength(n) property in your example code in this post.

If this helps, 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.  [ 2 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.