• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA mapping

 
mark I thomas
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If it is a simple select from one table called Student {name, id, address, age}, then in "Student" POJO I can define




Now, the chanllenge is --- If I define "Student" class using such naming to suit this "Student" table, how can I use this "Student" class to suit any other native SQL or store procedure ? For example, name is called "name" in "Student" table, but if there is another table called
Library_User { userName, ..., ...} where "userName" is the same thing as "name" and can be used as join key. If that's the case, then I can NOT do things like



because the mismatch between column name and class' attribute name. Furthermore, if it is store procedure it can be more different. So it is hard to define a POJO's attribute name unless you are sure this POJO is ONLY going to match one table or SQL.

Another way to handle this is




i.e. don't specify and POJO class in the "createNativeQuery". Then use Object[] to do the mapping. Do I make sense here ? Any idea to share when you handle the scenario ?
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally you would map each table to a different class, so I'm not sure I understand your goal.

You may want to look into ResultSetMappings, these allow for different native query results to be mapped into objects.

http://en.wikibooks.org/wiki/Java_Persistence/Querying#Result_Set_Mapping

You could always just get the Object[] back and map the array into objects yourself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic