This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

JPA mapping

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 ?
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.

You could always just get the Object[] back and map the array into objects yourself.
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!