This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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 ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How Do Foriegn Key Objects in JPA get Initialised?

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I am just wondering whether someone will be kind enough to explain how forieign key objects in JPA get initialiased. I'm new to JPA as you can probably guess. Anyway, from my understanding, lets say an Entity A has ManyToOne birectional relationship with an Entity B and Entity A is the owning side of the relationship, then Entity A will have a foreign key object that references Enitity B and vice versa. That bit is quite easy to understand. The bit I am having a few problems understing is this, If i have persisted an An intance of Entity A, and at a later date, i retrieve the instance that had been persisted, how does the forieign key object get initialiased? Please have a look at code below. Please not that I have omitted some of the default get and set methods. Lets also assume that EntityB has the is also correctly mapped to EntityA



So now in my Session bean, I want to retrieve an an instance of EntiityA which has been persisted to the datastore previously, how does the ForiegnKey(instance of EntityB) get initialised?

This might seem trivial but I dont understanstand how the intance of entityB gets initilialised. Does the annotation @JoinColumn(name ="ForeignKeyColumn"), automatically retriieve an instance of EntityB? It seems to me that from the code above, getForiegnKeyEntityA_References() will always return null because, at no point does an instance of EntityB EXPLICITY gets initialised. I ask this because I have seen a lot of websites and tutorials including the Sun tutorial that provide similar codes to the above and non of them seem to explain how the foriegn key(instance of EntityB) is initialised when an instance of EntityA is retrieved from the datasource. Any Sort of explanation/better code to help me undertand this will be greatly appreciated.

Thanks All
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic