File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes hbm without primary key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hbm without primary key" Watch "hbm without primary key" New topic
Author

hbm without primary key

Ankita Jha
Ranch Hand

Joined: Sep 15, 2008
Posts: 55
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

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hbm without primary key