Forums Register Login

Designing in Hibernate

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
any idea?
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1575 times.
Similar Threads
Generic
dependency injection using concrete classes
Set not initialised on lazy loading
Doubt About Generic
Generic
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:26:30.