This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have just started wondering how is this scenario possible:
We have two entities:
- an unidirectional one-to-many relation between A and B. Now JPA/ORM knows that the listOfBs property is a relation with entity B and can create a join table and fetch the Bs. How does it know it? The generic B parameter is not stored in the bytecode. So everything it gets is a raw @OneToMany, a raw java.util.List and a name listOfBs. None of these tell anything (to me) that the other side of the relation is a B entity/B class. Does anyone know it works? I'm so curious but can't think of anything else.
Not really. The public List<B> getListOfBs() signature provides info on the objects it is holding at compile time, but then it is stripped in the bytecode (the .class file) to public List getListOfBs(), where the information on the B type disappears. And the container gets only the .class files. So how does it know about the B type?
And to just to be precise - it's targetEntity, not mappedBy.
Joined: Feb 18, 2008
Ah ha! I found out that although the generic type of an object cannot be determined during runtime, the generic types of methods, method parameters and fields can. This was helpful: Jakob Jenkov - Java Reflection: Generics.