This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How Do Foriegn Key Objects in JPA get Initialised?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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

e.g.



EntityB




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
 
reply
    Bookmark Topic Watch Topic
  • New Topic