File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes A general question about Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "A general question about Hibernate" Watch "A general question about Hibernate" New topic
Author

A general question about Hibernate

John Farrel
Ranch Hand

Joined: May 24, 2010
Posts: 84

I am considering using Hibernate for a project, but I need to know something about its capabilities.

If you use java objects to create a schema, how smart is the schema update?
e.g if I change the name of a field in an object, will the table column name change? Or will the old column get dropped and a new unpopulated column be created?
Assuming annotations are used.

J.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

First off never use schema generation/update in production, that was never its intent. It is a development tool only. That said you can set the column name and the field names can be changed without affecting the column.



I can rename myField to whatever I want and the column name will remain the same.


[How To Ask Questions][Read before you PM me]
John Farrel
Ranch Hand

Joined: May 24, 2010
Posts: 84

I guess the question is what's best practice for use of Hibernate;

Create the database and generate classes from it?
Create the classes and generate the db schema?

My gut feel is to generate classes from db.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

I guess there are people from different camps on this. Personally I believe that one of the major advantages of using an ORM is the ability to focus on the Domain (object) relationships and not have to have those objects reflective of the relational (database) model. Therefore I typically start with creating my objects in a way that makes sense for my application. I can basically ignore the database at this stage. When I have that set up I put the relationships in and then if this is a new database, generate the schema and make tweaks as necessary.

For example if I have a person object and the person object contains an Address object I do not need to worry about whether the address details live in the same table as the other person properties or in its own 'ADDRESS' table. I can account for that with my mappings later. Also often times the database already exists. In this case the trick is usually mapping the existing relational model to my domain model. Not the other way around or you lose one of the biggest advantages of an ORM imo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A general question about Hibernate