aspose file tools*
The moose likes Object Relational Mapping and the fly likes How does JPA/ORM know the target entity? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How does JPA/ORM know the target entity?" Watch "How does JPA/ORM know the target entity?" New topic
Author

How does JPA/ORM know the target entity?

Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128
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.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128
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

Joined: Feb 18, 2008
Posts: 128
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.
 
 
subject: How does JPA/ORM know the target entity?