• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Designing in Hibernate

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any idea?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
fu bace
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
fu bace
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic