This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Entity class for a View (logical table) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Entity class for a View (logical table)" Watch "Entity class for a View (logical table)" New topic

Entity class for a View (logical table)

Matthew Vahedi

Joined: Jan 05, 2012
Posts: 17
Hi everybody,

Thanks for answering my previous question, those are great answers and helped a lot.


I have got a View that fetches it's result from different tables. There is no primary key in this View. I'm using Hibernate JPA for the rest of my Oracle tables.


1- Can I create an Entity class for this View? ( What should I do for the lack of primary key? )

2- Where can I place the SQL ( not JPQL ) command that creates this View? My guess is inside DAO class but not sure how and what API must be used?

Thanks in advance.

Dieter Quickfend

Joined: Aug 06, 2010
Posts: 543

There are several options which you can use, among which:

- Add (a) column(s) to your view which is eligible as a PK, and use that as a PK for your entity. It doesn't have to be a PK in the database.

- define a composite primary key in your entities. You can define a primary key composed of all columns in your entity if completely duplicate entities do not exist. If you can find a number of columns which will never have the same value more than once, create your composite key. It doesn't have to be a PK in the database.

- If you don't need your entity to be managed, you can use Result Classes Constructor Expression. I think this is probably the best solution for your problem, but I am not in a position to decide whether or not you need your entities managed. This case has the lowest footprint, of course. See here:

Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
I agree. Here's the link:
subject: Entity class for a View (logical table)
It's not a secret anymore!