aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate 3 Annotations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate 3 Annotations" Watch "Hibernate 3 Annotations" New topic
Author

Hibernate 3 Annotations

Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
I am trying to create a One to Many Relationship between two tables Orders and OrderLineItems. I am not sure what is the mistake i am doing. Can anybody tell me what is the problem with the following code.

The Scenario says One Orders can have more then one OrderLineItems

Order Class:


OrderLineItems Class:


Where ORD_ID is the name of the column in the Database ?

Any links or pointers are appreciated.

The following line is showing me an error in the jdeveloper IDE. It says "Type or variable orders not found"
@OneToMany(mappedBy=�order�)

Thanks,
Rahul


Rahul Juneja
ThoughtClicks - http://techlabs.thoughtclicks.com
Matt Gaunt
Ranch Hand

Joined: Sep 05, 2003
Posts: 34
Hi Rahul,

First I would look at the relationship you have there. You have a situation where an Order can have 0 to many OrderItems. So obviously, you would like to see the OrderItems belonging to an order.

Does the same hold true for an OrderItem. When you have an OrderItem object, do you need to know the Order to which it belongs. As in, is there ever going to be a time when you have an OrderItem first and need to find the Order from which it came. I would suggest that this is unlikely.

The annotations, I tend to put above the declaration of the fields as I find it much easier to read as well. But that is a personal preference.

Here is a way you should be able to achieve what you are chasing:
In the Order class you would have as follows:



And in your OrderLineItem class, I wouldn't make any reference to your Order class as you should not have a situation by where you need to find the Order to which an OrderLineItem belongs. This is information you should always have as it should be the Order you retrieve/create first.

Note, I have suggested a lazy loading of the OrderLineItems. This means that if you read the Order from the database, the OrderLineItems will not be read unless you initialise the orderItemList before closing the selection transaction. A simple order.getOrderItemList().size(); is enough to initialise it.

Hope this helps.

Regards

Matt Gaunt
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Matt,

I tried executing the code you gave but i am not sure if it was perfect as i had the following. Also this does make sense as per the specs.

--------------------------------------------------------------------------
Operation failed with error:

Exception Description: @OneToMany for attribute name [orderLines] in entity class [class Orders] should not have @JoinColumn(s) specified. In the case where the @OneToMany is not mapped by another entity (that is, it is the owning side and is uni-directional), it should specify (optional through defaulting) a @JoinTable.
--------------------------------------------------------------------------

Any help in this is highly appreciated.

Thanks,
Rahul
Matt Gaunt
Ranch Hand

Joined: Sep 05, 2003
Posts: 34
Hi Rahul,

Could you please include your Order class as well as the DDL for your Order table?

Regards

Matt Gaunt
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Matt,

To my understing, The following code should have the DB column name.
@JoinColumn(name = "order")
and you referred as the instance of the orders class.

Orders Class:


Order Table:
desc orders
Name Null? Type
-------------------------------- ------ -------------------------
ID No NUMBER(10) --- > Primary key col.
Type Yes VARCHAR2(255)

ORDERLINE Table
Name Null? Type
-------------------------------- ------ -------------------------
ID Yes NUMBER(10) --- > Primary key col.
ORD_ID Yes NUMBER(10) ----> Foreign key col.

Appreciate all your help.

Thanks,
Rahul
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Matt,

I have also tried the following code for Unidirectional relationship and getting the below exception.

Order Class :


Thanks,
Rahul
Matt Gaunt
Ranch Hand

Joined: Sep 05, 2003
Posts: 34
I think I see the problem. The code should read



The value for the join column should be the foreign key field in the joined table.

In your case, you have an Order table which can have many OrderLine items. In your OrderLine table, you have a foreign key back to the Order table. The foreign key field is called ord_id. It is this field which should be put as the value of the name attribute for the @JoinColumn annotation.

Regards

Matt Gaunt
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Thanks for you help Dude. But i had some very basic problem as i was declaring the annotation in a mixed manner. I mean was declaring some annotations on variables and some on methods and that is fixed now.

Thanks for you help.

Thanks,
Rahul
Matt Gaunt
Ranch Hand

Joined: Sep 05, 2003
Posts: 34
No worries. I'm glad you sorted it out and is all working now.

Regards

Matt Gaunt
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Indeed, it's an important point to ensure that you are consistent in your annotations. Either they are all on methods, or they are all on properties, but doing it on both will definitely cause problems.

-Cameron McKenzie
 
jQuery in Action, 2nd edition
 
subject: Hibernate 3 Annotations