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.
Joined: Sep 10, 2008
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);")
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 ]
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?
Joined: Sep 10, 2008
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'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.
Joined: Sep 10, 2008
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?
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.