This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes how to add a table in hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "how to add a table in hibernate" Watch "how to add a table in hibernate" New topic
Author

how to add a table in hibernate

Manu ravi
Greenhorn

Joined: Aug 15, 2012
Posts: 16
Hi,
In my current task, we are adding a new table to the database to implement a new functionality. We are using hibernate+spring for the project.
SO my question is since we already have hibernate files and java files for all the tables(i am not sure about the terminology i mean pojo files for each table), should I just create a new java file and the related hibernate file by myself?

please tell me if I am on the right track. any help would be appreciated.
Thanks
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

Yes,

If your hibernate setting is create-drop or update (I think.. been a while), then the schema is auto generated and created for you.

NOW! In production, you should never turn this on as you could lose all your data when the table is dropped and created.

What I do is have the table(s) created in a separate DB, generate the DDL, execute it on the target DB (just to be safe).

WP
Manu ravi
Greenhorn

Joined: Aug 15, 2012
Posts: 16
I am still in local server. So I can just go ahead and create my java and hibernate mappings??

I have one more question. They are using middlegen plugin. So will it create mappings for this single new table or does it create it for all the tables in the database?
Manu ravi
Greenhorn

Joined: Aug 15, 2012
Posts: 16
oh and we are updating. not create and drop
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

I don't know middlegen.

Again, I prefer having control and analyzing the generated tables before using/deploying.

The choice is yours.

WP
Manu ravi
Greenhorn

Joined: Aug 15, 2012
Posts: 16
William P O'Sullivan wrote:

Again, I prefer having control and analyzing the generated tables before using/deploying.





I didn't get you. What do you mean by "analyzing the generated tables"?

Sorry for repeatedly asking the same question, so when you say you generate the ddl for the tables you mean the newly added tables right? how do you generate ddl?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

What William is referring to is using the hbm-ddl-auto property to generate the tables FROM the objects. Hibernate also has the facilities to generate the java objects from the hbm files. However I would say the best approach is forget all the generation stuff and just create your domain objects (pojos) and the hbm mapping files yourself.

It looks like the middlegen plugin is something that generates all of this for you from the tables. In this case you would add your new tables and generate the mappings and pojos from the database. I typically don't trust these kind of tools and prefer to do it myself. However you may have to be consistent with what others are doing. I would talk to your team.


[How To Ask Questions][Read before you PM me]
Manu ravi
Greenhorn

Joined: Aug 15, 2012
Posts: 16
thanks. That clarifies my doubt. I think i will write the pojos and hbm files myself
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

Exactly what Bill said.

You can also design the table(s) first, then generate the POJOs from it/them.

why are you still using hbm.xml? Annotations are the way to go now.

WP
Manu ravi
Greenhorn

Joined: Aug 15, 2012
Posts: 16
well we are using hibernate 2.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to add a table in hibernate
 
Similar Threads
QuerySyntaxException: "Tablename is not mapped"
Hibernate many-to-many mapping
hibernate doubt
hibernate mapping files for enumerated strings.
cannot create hibernate project.