-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: is hibernate.id required?
PostPosted: Thu Apr 01, 2004 3:30 pm 
Newbie

Joined: Thu Jan 29, 2004 2:51 pm
Posts: 9
The documentation says so. However there are times where I don't have a primary key for a table (especially a database view.)

I have a few database views that join together data from several tables. The database views do not require a primary key column (or a unique column.) There is no business need for me to add one.

I have mapped a java class to the view for Hibernate. However I have not defined an "id" property because the database view does not have a primary key.

All I would like to do is have Hibernate return to me a List of my java objects when I query against the view. I don't want Hibernate to store them or compare them in the object cache (in fact I want to bypass the object cache entirely.)

If I don't specify the hibernate.id property I can't get this to work. However if I modify the database view and add a column to it that is unique, and then declare the corresponding java bean property to be an "id", then this does work.

However I'm irritated by this because the database view doesn't require the this extra column and I don't want it in there.

I've tried finding documentation or forum topics on this to no avail. Can I accomlish my task using Hibernate without modifying my view?

Thanks, Steve


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 01, 2004 3:37 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Not really, as Hibernate assumes primary keys. In fact, a database is broken if it doesn't declare a primary key for a table/view. In some circumstances this might be considered "good", for example when _all_ of the columns _are_ the (composite) primary key. You might get away with a mapping like that.

I think you have hit a fundamental issue with today's SQL database products: Views, "null" and constraints. Notice that you probably can't even update your view! There is no real reason why this should not be possible.

What is worse, most database systems even allow you to update a table/view when there is no declared primary key, that means you can insert two rows with the same values! Now, this is not really anywhere close to the Set Theory of the relational model, so I leave it up to you to discover the problems that arise from this. ;)

I'd add a primary key to the view to ensure data integrity.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 01, 2004 9:12 pm 
Newbie

Joined: Thu Jan 29, 2004 2:51 pm
Posts: 9
I'm not a database maven but I do know that it can be acceptable to define a Set of data that doesn't have 1 property on the object/tuple that declares uniqueness. But you are right in that Set Theory mandates that one identify whether some object or tuple is "equal to" or "not equal to" another object or tuple.

For my database view, there is some combination of columns that gives uniqueness. But frankly I don't want to bother writing a composite key because I'll be defining 7 columns (out of 13) that comprise uniqueness.

Practically speaking, Hibernate is great for 80% of our data access needs. However I do want a tool that given 1) a Java bean and 2) a ResultSet - it can create an instance of that Java bean and set the properties on that bean given the data in the ResultSet. Hibernate is a full-blown O/R tool that has loads of great features but I'm not sure if Hibernate can do exactly what I said above. Sometimes I don't have a primary key, sometimes I want to completely bypass the object cache (can this be done on a persister-by-persister basis???). This is a read-only view so I don't intend to insert/update into the view. I guess I could write my own CustomPersister but I read elsewhere on the forums that that is a daunting task.

BTW, I added the primary key from the driver join table and everything works fine (and the more I think about it I don't think it's a bad solution either.)

Thanks, Steve


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 01, 2004 9:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
I do know that it can be acceptable to define a Set of data that doesn't have 1 property on the object/tuple that declares uniqueness.


Of course. That is what <compoisite-id> is for.

Quote:
But frankly I don't want to bother writing a composite key because I'll be defining 7 columns (out of 13) that comprise uniqueness.


Well, it doesn't matter what the DB schema says. As long as you wrap the property mappings in a <composite-id> tag, it will work.

I simply do not see the problem here. Its trivial to do this in Hibernate.

Quote:
However I do want a tool that given 1) a Java bean and 2) a ResultSet - it can create an instance of that Java bean and set the properties on that bean given the data in the ResultSet.


If that is all you need, then iBATIS is a great solution.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 12:36 pm 
Newbie

Joined: Wed Sep 03, 2003 10:40 am
Posts: 6
Quote:
However I do want a tool that given 1) a Java bean and 2) a ResultSet - it can create an instance of that Java bean and set the properties on that bean given the data in the ResultSet.


Checkout Kodo. It is a full O/R solution like Hibernate. But it does have the feature you ask for.

_________________
Dave Ford
Smart Soft - The Developer Training Company
http://www.smart-soft.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 12:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
posting a commercial on a 3 year old posting is funny ;)

...for completeness hibernate 3 has the feature too.

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.