aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate mapping to a view Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate mapping to a view" Watch "Hibernate mapping to a view" New topic
Author

Hibernate mapping to a view

laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
Can you map to a view in the same way as a table? What happens if the view does not contain a primary key? When I do a select statement based on a person's id I don't get all the results in my result list. My result size is correct but it only returns the first record. Is this what happens when mapping with hibernate in this circumstances?

Any Suggestions? Should I be looking at composite-ids would this fix it?

Thanks
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2433
    
  28

If you don't have PK, which column is the ID? You have to make sure that the field(s) that you have marked as Id in the entity uniquely identify the record, and are non-nullable.
laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
Yeah, that's the problem. While there are no null values all the fields in the view are nullable. But, theoretically, if I do a select query on this read-only view based on a person's id I should get a list of records for the user. However, when I run the query below I get the correct number of records but they are outputting the same exact record repeatedly instead of distinct records.

I have been doing a search on this for the past few days and I'm running out of ideas. There are no static variables in my domain class, running the sql stmt gives me the correct results when running it against the database, so I'm left wondering if this is a hibernate mapping problem. I'm scraping the bottom of the bucket on this one and I'm trying to leave no stone unturned but I'm not finding anything that stands out. Below if my query and mapping (I tried using id and composite id without any luck)

Please help

Thanks




Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2433
    
  28

If personid is not unique, you will not get all the records. The ID field has to uniquely identify the record. It doesn't have to be the PK (although making PK the Id field guarantees uniqueness), but it does have to be unique.

What you need to do is identify the business keys of your underlying tables, and make sure that the business keys of the underlying tables are included in the view. The combination of the business keys of the underlying tables will be the business key of the view. You need to make the business key columns as the ID.

laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
Well does it make it easier that it's only one table(view) that I'm working with?

Can I fake this through the hibernate mapping? Or do I need to modify the actual table, because I don't have permissions to the database to modify it.

Is it possible for me to use the composite key attribute to group personid and requirement since it creates a unique key? Is that what you are meaning?

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2433
    
  28

Yeah if personid and requirement is unique then that should be your id.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate mapping to a view