Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hbm without primary key

 
Ankita Jha
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ranchers,

I have generated a view using hibernate.Which basically dosent have an Primary key.
This returns me a list which is type casted to a Collection.
The problem is my id column in hbm has got duplicate values.where ever there is a duplicate value,it displays the first occurance of that id.

Pasting below my hbm file. My POJO and Dao Class




This is how my database output looks like




Pasting below my controller code.





Can any one please explian why it happens and how it happens.


And i cannot have a primary key for the view as it used group function.

Any help will be appreciated.

Thanks.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any entity in a relational database without a primary key is invalid. Entities can only participate in relationships (and hence be relational data) if they can be uniquely identified, and without a primary key they can't be uniquely identified. This is why Hibernate requires all objects to have an identifier property mapped to a valid primary key. There are partial work arounds for legacy data sets (such as mapping the entire contents of the row as a composite key) but the best fix is to change your data model to use a primary key.


And i cannot have a primary key for the view as it used group function.

Why does using a group by function mean it can't have a primary key? Define an auto generated surrogate key in your view and the problem goes away. This does however highlight one of many issues mapping views in this way. Unless your view is an updatable view (which it can't be if it uses a group by clause) mapping it as a distinct class in Hibernate doesn't make much sense. If you can create, update or delete these objects why do you need all the baggage of mapping it as a manageable object, which also runs the risk of hiding from other developers maintaining your code that the obejct is not updatable? Views make more sense if they are mapped as named queries.

 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul is indeed correct. The problem is solved by defining some type of primary key. It's always best to spend some time thinking about how to produce a non-intrusive, unique primary key when you are mapping a grouping or a view.

-Cameron McKenzie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic