• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Hibernate 3 Annotations

 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt,

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

Order Class :


Thanks,
Rahul
 
Matt Gaunt
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No worries. I'm glad you sorted it out and is all working now.

Regards

Matt Gaunt
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic