Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to persist an object in Hibernate

 
mvPrasad Regula
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an class Person which has an Address object in it. How should I persist the Address object using hibernate. The code is as below
 
Rob Spoor
Sheriff
Pie
Posts: 20753
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our ORM forum where Hibernate is discussed.
 
Pradeep Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you need to tell hibernate the DB details that you would be persisting the object into. So obviously you need to write the configuration details in xml. You specify the driver, username, password and other details so that hibernate call talk to the DB.

The next step would be to write an xml mapping file for each of your persistent classes. In your case the person and the address. Typicallly you would be writing that in Person.hbm.xml and Address.hbm.xml files although you can write it in a single file.

You then include these two xmls in the hibernate configuration xml so that hibernate can map the properties to the columns ands the class to the tables and associate these two entities.

Hibernate loads the configuration a startup time and scans through the xml files. The hbm2ddl is used to trigger the schema update on the db.

Hope this was useful :-).
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mvPrasad, please don't post the same question twice. I already answered it here, and I almost answered it again here.

Here's my previous answer. I'm going to throw your previous post on the compost heap.

************
C'mon, ya gotta try harder than that!!! What have you tried? What problems are you running into? Do you have Hibernate configured?

Here's a little tutorial on one to many relationship:

Mapping One to Many Relationships with Hibernate

That should point you in the right direction.

************
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic