How Do Foriegn Key Objects in JPA get Initialised?
posted 7 years ago
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.