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.
posted 11 years ago
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.