Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Automatic Schema Generation

 
subhashchandra medhiassam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Could anybody enlighten me on what is automatic schema generation using hibernate? Your replies would be highly appreciated. I would also like to know how to implement automatic schema generation. It would be nice if somebody would provide some example.

Thanks,
Subhash
 
subhashchandra medhiassam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have gone through some tutorial available in the net on automatically updating a schema.(web page)

I tried to implement whatever is given in that tutorial. Here goes the description.

I have a class Schema1 which is a representation for the table schema1 in MYSQL.

Table schema1 has columns id(int type),description(varchar type) and country_code(int type).

Table schema1:
Column Data-type
------ ---------
id int
description varchar
country_code int

Class Scema1.java looks like the following:




Now, i have another class UpdateSchema.java. It looks like the following:



I have exactly followed the code given in the tutorial.

What I am unable to understand is where from does the "execute" method arise?(In "new UpdateSchema(myConfiguration).execute(true,true);")

Could anybody give me some idea about this code?

Thanks,
Subhash
[ September 24, 2008: Message edited by: subhashchandra medhiassam ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Hibernate documentation is always the best place to start (especially as the tutorial you link to is three years old, uses Hibernate v2 and demonstrates a technique that is unneccesary). Have a read of the official documentation and this should get you going.

Typically you should not have to programatically enable dynamic DDL - its a property you set in your configuration and Hibernate will do the work for you. Read the official documentation, all should become clear.
[ September 24, 2008: Message edited by: Paul Sturrock ]
 
subhashchandra medhiassam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,
I have tried updating the schema in another way.

I have the same table schema1. I want to add another column "addition" of type varchar to this table.

So,I have added one extra property to my mapping file. My .hbm.xml file now looks like this:



I have changed my POJO class Schema1.java which now looks like the following:



Now, my i have another class UpdateSchema.java which looks like the following:



When i execute this program, i don't get any error output. It executes properly. But the table schema1 doesn't get changed. It remains the same.

Could anybody kindly tell me why? What's the remedy for this?

Thanks,
Subhash
 
subhashchandra medhiassam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
You are telling that the official Hibernate documentation is the best place to start. Well, i went through that document. What's given there is that we should include the following two lines of code in our program:



But the method execute which is available takes two arguments of type boolean and not one?


What should i do now? Kindly help.

Thanks,
Subhash
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What's given there is that we should include the following two lines of code in our program:

Not sure where you found that bit of information - its not mentioned in the "Automatic schema generation" section of the docs. Where did you see it?

The Hibernate tools API included a SchemaUpdate type - but I don't see why you should need to use it. Hibernate includes a tool generate DDL based on your mapping files. Have a read about the hibernate.hbm2ddl.auto property and its effects.
 
subhashchandra medhiassam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay,
I got the following in the official documentation on hibernate.hbm2ddl.auto:

"Automatically validate or export schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.
eg. validate | update | create | create-drop"


So, accordingly, i have changed my third class UpdateSchema.java. It now looks like this:


But when i run it, it is throwing the following message:



What could be the reason? Kindly do let me know what i should do?

Thanks,
Subhash
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can only get that error mapping classes directly in you hibernate.cfg.xml file. Can you post that?
 
subhashchandra medhiassam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, i have tried to map my bean class in the hibernate.cfg.xml. It looks like the following now:



Kindly help.

Thanks,
Subhash
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<mapping class="Schema1" />

There is your problem. You are mapping a class, not a resource. If you map a class you are relying on annotations not mapping files.
 
subhashchandra medhiassam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then what should i do? Map the class as a resource?

Thanks in advance,
Subhash
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Map the resource (since the resource maps the class)
 
subhashchandra medhiassam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which resource do you mean? Could you be a bit clearer?

Thanks,
Subhash
 
subhashchandra medhiassam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when i am mapping Schema1 as a resource and then running the program, i am getting the following message:



What should i do? Kindly advise.

Thanks,
Subhash
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would appear you have saved your hbm.xml file in an encoding other than UTF-8. Make sure you save the file as UTF-8.
 
subhashchandra medhiassam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,
My hibernate.cfg.xml looks like the following now:



And, i have saved it as UTF-8. when i run my program, however, i am getting the following message:



What should i do? Kindly help.

Thanks,
Subhash
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">

com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">

jdbc:mysql://localhost/hibernatetutorial</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Persistent classes -->
<!-- <mapping class="Schema1" /> -->
<!-- Mapping files -->
<mapping resource="Schema1.hbm.xml"/>
<mapping resource="Schema1.class" />
</session-factory>
</hibernate-configuration>

Why are you mapping both the mapping file and the class? This may be frustrating to hear again, but I really strongly recommend you stop what you are doing and take a few moments to read the documentation. This is basic configuration; make sure you understand this concept before pushing on to more involved topics (otherwise you'll just have a horrible time trying to get this working).


And, i have saved it as UTF-8. when i run my program, however, i am getting the following message:

Ah. I was too quick to suggest its your mapping file - the non UTF-8 thing is the class file. Sort out your configuration and this error will go away.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic