• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How does JPA/ORM know the target entity?

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this to me says It is a List of Bs

public List getListOfBs()

List is typing the Collection to only hold instances of B. If it was just List, then you would have to include the attribute "mappedby" in your @OneToMany annotations.

Mark
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic