Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate mapping to a view

 
laura mccord
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah if personid and requirement is unique then that should be your id.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic