-->
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.  [ 7 posts ] 
Author Message
 Post subject: hibernate.id vs normalized databases
PostPosted: Sun Apr 08, 2007 3:34 pm 
Beginner
Beginner

Joined: Tue Nov 22, 2005 6:55 am
Posts: 41
Hi,

I am about to start writing my master's project and I have first solid dilema.

I have a choice
1. create as normalized db as it is possible, using multiple primary keys and complitated hibernate composite keys. The advantage - well designed database.
2. create java classes extending base model with hibernate.id and set it as primary key in each table, even many-to-many junction tables (or maybe here i could make an exception). The disadvantage - not good database - i.e. class:
Worker
hibernate.id id
personalID - unique personal data, one in the country for one person,
otherNumber - unique personal data, one in the country for one person,
I NEED TO HAVE TWO fields which normally would be primary keys - if I had only personalID, my problem would be simple resolved - put unique constraint to avoid data repetition. But there will be tables with >=2 id fields apart from hibernate.id (third one). To the bes of my knowledge I can't set composite unique constraints on a table. in POSTGRE SQL 8

then i would have possibility to put into my db the same worker many times, which is an error. Nex disadventage would be that befor insert of such a worker i would hav to check in my code if he hasn't been already inserted.

But i've found in your tutorial http://www.hibernate.org/hib_docs/v3/re ... rial-intro :

Quote:
The id property holds a unique identifier value for a particular event. All persistent entity classes (there are less important dependent classes as well) will need such an identifier property if we want to use the full feature set of Hibernate. In fact, most applications (esp. web applications) need to distinguish objects by identifier, so you should consider this a feature rather than a limitation. However, we usually don't manipulate the identity of an object, hence the setter method should be private. Only Hibernate will assign identifiers when an object is saved. You can see that Hibernate can access public, private, and protected accessor methods, as well as (public, private, protected) fields directly. The choice is up to you and you can match it to fit your application design.



So could u please tell me why should I follow it? I am not experienced enough to outnumber the disadvantages that i've mentioned.
Could u point them to me so that i could use these hints in my project ?
I have to justify resigning from normilized db and putting some objectId in each table just for hibernate..

I also want to use HibernateTemplate :
protected BaseModel readModel(Class clazz, Long id) {
return (BaseModel) getHibernateTemplate().load(clazz,id);
}
but I could always make a select query.


Top
 Profile  
 
 Post subject: any experienced hibernatist :P ?
PostPosted: Tue Apr 10, 2007 4:06 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 6:55 am
Posts: 41
is there any experienced hibernatist that could answer :P ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 10, 2007 5:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
your db will still be normalized if you use surrogate id's.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 10, 2007 8:58 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
But you'll still be better off (IMO) by having a single primary key on each table if your are designing from scratch. If they serve no other purpose that as a row identifier, it will still make your ORM life easier in the long run. For instance (as I believe you mentioned), it allows you to extends your POJOs from a single base class with an identifier, and lemme tell you, there is a lot of common functionality that you would want to incorporate in a base class. This also allows you to write generic DAO methods, using the BaseModel (or whatever) class to perform common operations (persist, delete, etc).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 10, 2007 9:10 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You do not need a base class to implement generic DAOs. You need generics: http://www.hibernate.org/328.html

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 10, 2007 9:59 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I certainly already use that methodology for DAOs, and highly suggest it! But I also derive all of my model classes from BaseModel, which allows me to use <T extends BaseModel> in the generics of the DAOs, which then allows me to implement more common functionality (e.g. all my model also have 4 auditing columns, create_by, create_date, etc). So, for example, I can write a findByCreatedDate() function in my BaseDao and BaseHibernateDao classes.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 10, 2007 11:19 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
herbatniczek wrote:
I NEED TO HAVE TWO fields which normally would be primary keys - if I had only personalID, my problem would be simple resolved - put unique constraint to avoid data repetition. But there will be tables with >=2 id fields apart from hibernate.id (third one). To the bes of my knowledge I can't set composite unique constraints on a table. in POSTGRE SQL 8


PostgreSQL certainly does allow multi-column unique constraints. From the docs (http://www.postgresql.org/docs/8.1/static/ddl-constraints.html):

Quote:
If a unique constraint refers to a group of columns, the columns are listed separated by commas:

CREATE TABLE example (
a integer,
b integer,
c integer,
UNIQUE (a, c)
);


Hibernate has a <natural-id> you can use to identify your unique columns
(http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#mapping-declaration-naturalid)
(http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#query-criteria-naturalid)

_________________
nathan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.