This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
Paul thanks for your reply. here is my scenario. I have 4 classes. Customer, Order, LineItem and Product.
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?
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.
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.
Joined: Oct 16, 2009
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.
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
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.