Two Laptop Bag*
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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

Entity class for a View (logical table)

Matthew Vahedi
Greenhorn

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

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

Now..

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.

Questions:

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.

Cheers,
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

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: http://www.objectdb.com/java/jpa/query/jpql/select#Result_Classes_Constructor_Expressions_


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: http://aspose.com/file-tools
 
subject: Entity class for a View (logical table)
 
Similar Threads
how do I get one to many relation when only one column is there in table if many
Primary Key question
entity bean for a table with No Primary key : Please Help,,,, URGENT
Mapping JPA Entity with a View
needs clerification from chapter 6 Entity Bean Synchronization