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 Designing in 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 "Designing in Hibernate" Watch "Designing in Hibernate" New topic
Author

Designing in Hibernate

fu bace
Greenhorn

Joined: Oct 16, 2009
Posts: 24
Hi. Suppose i have two classes. Don't worry on syntaxes.

Public class Basket{
Customer c;
}

Public class Customer{
Basket b;
}

Do i need to store the Customer attribute in Basket object or should i only use customer_id instead of customer object.

is this a design issue? what are the advanteges and disadvanteges if i hold customer object in basket object?
fu bace
Greenhorn

Joined: Oct 16, 2009
Posts: 24
any idea?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

One of the purposes of Hibernate is to try and remove (or at least lessen) the need to think in terms of primary and foreign keys. I'm assuming in terms of your domain model a Basket has a Customer and viceversa? You have to consider if there is any reason for your domain model to know about how this association is implemented.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
fu bace
Greenhorn

Joined: Oct 16, 2009
Posts: 24
Paul thanks for your reply. here is my scenario. I have 4 classes. Customer, Order, LineItem and Product.

Customer.hbm.xml



Order.hbm.xml


LineItem.hbm.xml


Product.hbm.xml




After this i see my Orders table with 3 columns. id,customer,customer_id which looks very stupid. I am new to hibernate. Before hibernate, i hold onyle the order_id,customer_id in orders table which looks better than the above.

When i first read hibernate. I saw some hql's like " from Book where book.chapters.sub_chapters = "blabla" ". I said this is wonderfull. If i want to this on my old way i had to use joins. My question is this. If i use bidirection, at the end i have a table stupid like this but i can access the customer by doing this simple query.
from Orders where orders.customer.id = 5

Sorry for my nasty englih. i wonder you understand what i meant.

and one last question



i am executing this code many time but i only see one row with name "jit" there. i am expecting multiple rows with name "jit". is there something that i dont know about sessions?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

After this i see my Orders table with 3 columns. id,customer,customer_id


Why do you see three columns? Is Hibernate generating your tables?

Anyway, in the Java side just think of Java objects, not FK and PKs like said before.

Your mapping should be simple too. Do not have any int customer_id fk properties in your Java objects.

When mapping OneToMany or ManyToOne, I always include @JoinColumn so that if Hibernate is generating your tables that it puts a FK field into the Child table, instead of creating a Join Table between the Parent and Child tables.

I recommend reading a book on Hibernate to fully understand the ORM design and how things work.

Good Luck

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
fu bace
Greenhorn

Joined: Oct 16, 2009
Posts: 24
I understood why i had 3 columns.

in order.hbm.xml

i defined a mapping like this
<many-to-one name="customer" class="Customer"/>

which should be

<many-to-one name="customer_id" class="Customer"/>

the name must be exactly same as in the customer.hbm.xml

also i understood why i should use bidirectional associations.

if i want to set a customer to an order by the order domain, i must also make an association to customer class.

the only problem i had now is why only one row is being inserted althoug i insert the record many times in main function?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

What database are you using? Is it Hypersonic or some other in memory database? Because with these in memory databases, each time your run your app it will recreate the database, and when the app ends, the database in memory goes away.

Mark
fu bace
Greenhorn

Joined: Oct 16, 2009
Posts: 24
I am using mysql. I think the problem is not with mysql because i am also using the spring framework hibernatetemplate but i am not facing a problem like this.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

fu bace wrote:I am using mysql. I think the problem is not with mysql because i am also using the spring framework hibernatetemplate but i am not facing a problem like this.


What version of Hibernate are you using? If you are using Hibernate 3.x there really isn't any need for HibernateTemplate in Spring. The reason why you would use it is if you were using the old way of Sessions where you had code like session.beginTransaction, and had a try/catch and commit and rollback calls. But with Hibernate 3, you let the TransactionManager handle commit and rollback and all you need is

sessionFactory.getCurrentSession();
session.someAction();

And that is all.

Mark
fu bace
Greenhorn

Joined: Oct 16, 2009
Posts: 24
I am using hibernate-3.x. the code above is not a part of my spring project. i am using it just to see what happens if i run this query. something like a test class. Thanks for your help Mark. I should work on ORM a little much to understand how the things are working together.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Designing in Hibernate