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).
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?
[ September 24, 2008: Message edited by: subhashchandra medhiassam ]
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 ]
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?
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.
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.
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?
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
<!-- Persistent classes -->
<!-- <mapping class="Schema1" /> -->
<!-- Mapping files -->
<mapping resource="Schema1.class" />
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.