-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to map table without primary key
PostPosted: Fri Dec 29, 2006 12:42 pm 
Regular
Regular

Joined: Mon Aug 07, 2006 6:22 pm
Posts: 67
I have an Oracle table with the following columns:

MODEL_VERSION NUMBER(6) NOT NULL,
QKEY VARCHAR2(40) NOT NULL,
DISCRIM VARCHAR2(40) NOT NULL,
WEIGHT NUMBER(10,2),
VALUE NUMBER(10,2),
RATING NUMBER(2) NOT NULL,
MOD_TIME TIMESTAMP

Conceptually, the primary key is a composite, with MODEL_VERSION, QKEY, and VALUE. Unfortunately, I have to allow for VALUE being null. In each group of unique MODEL_VERSION and QKEY, there will only ever be one row with a null VALUE column. I don't know of any way to restrict that through plain database constraints.

It also doesn't work if I set the primary key to include those three columns, because the insert fails if VALUE is null.

So, I figured that since I couldn't specify the complete primary key, it would be better to specify no primary key at all. Oracle was fine with this, both in the DDL and with inserts. However, I realized that I don't understand how to specify a mapping in Hibernate for a table that doesn't have a primary key. As a result, I created a single synthetic "OID" column. Since this table is initialized statically in a dbinit script, I have to hardcode a unique OID for each row (ok, I do have a way to generate unique values in the script).

I'd prefer to avoid that annoyance, however. Is it practical in Hibernate to map a class to a table that doesn't have a primary key?


Top
 Profile  
 
 Post subject: Re: How to map table without primary key
PostPosted: Fri Dec 29, 2006 6:02 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
dkarr wrote:
I'd prefer to avoid that annoyance, however. Is it practical in Hibernate to map a class to a table that doesn't have a primary key?

Well I don't think it is. Hibernate can do a lot of things to cope with legacy schema, but working without primary key is a bit too much I guess. I don't even thing the dtd allow not to specify an <id> or <composite-id>, does it?

Defining an additional column is exactly what si recommended anyway. The best practices recommends not to use composite ids when possible (even if Hibernate supports it very well), since it's more difficult to use that a simple identifier column.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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