Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

hbm without primary key

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.

Posts: 10336
Hibernate Eclipse IDE 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.

author and cow tipper
Posts: 5000
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
Won't you be my neighbor? - Fred Rogers. tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic