-->
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.  [ 10 posts ] 
Author Message
 Post subject: Null elements in composite key
PostPosted: Fri Jun 10, 2005 2:32 pm 
Beginner
Beginner

Joined: Wed May 25, 2005 7:49 pm
Posts: 25
Hi,

I have a composite key (Yes, I know, they're bad. But it's a legacy data model that I can't do anything about). Some of the key properties are null. Whenever Hibernate encounters this null key, it returns a null element. Is there any way to return the actual element, with a null property?

Thanks in advance,
Lee


Hibernate version: 3.0.3

Mapping documents:
<hibernate-mapping package="metrics.beans">
<class name="MetricsEntityKeyJB" table="FCT_HISTORY" mutable="false">
<composite-id>
<key-many-to-one name="metricsPeriod" column="PER_ID" />
<key-many-to-one name="employee" column="EMP_ID" />
<key-many-to-one name="metricsLocation" column="LOC_ID" />
</composite-id>
</class>
</hibernate-mapping>


Full stack trace of any exception that occurs: None

Name and version of the database you are using: Oracle 9i

The generated SQL (show_sql=true): select * from ( select metricsent0_.PER_ID as PER1_, metricsent0_.MKT_POS_ID as MKT2_, metricsent0_.LOC_ID as LOC3_ from ADMET_PROMO_PER_HISTORY metricsent0_, ( select per_id, per_level_name, case when (PER_LEVEL_NAME = 'ADVERTISING WK') then AD_WK_BEGIN_DT when (PER_LEVEL_NAME = 'SCANNING WK') then SCAN_WK_BEGIN_DT when (PER_LEVEL_NAME = 'PROMO PLAN PERIOD') then PROMO_PER_BEGIN_DT when (PER_LEVEL_NAME = 'CALENDAR MTH') then CAL_MNTH_BEGIN_DT when (PER_LEVEL_NAME = 'ADVERTISING YR') then CAL_YR_BEGIN_DT end as begin_date, case when (PER_LEVEL_NAME = 'ADVERTISING WK') then AD_WK_END_DT when (PER_LEVEL_NAME = 'SCANNING WK') then SCAN_WK_END_DT when (PER_LEVEL_NAME = 'PROMO PLAN PERIOD') then PROMO_PER_END_DT when (PER_LEVEL_NAME = 'CALENDAR MTH') then CAL_MNTH_END_DT when (PER_LEVEL_NAME = 'ADVERTISING YR') then CAL_YR_END_DT end as end_date from admet_period ) metricsper1_ where metricsent0_.PER_ID=metricsper1_.PER_ID and metricsper1_.PER_LEVEL_NAME=? and (metricsper1_.BEGIN_DATE between ? and ?) and (metricsper1_.END_DATE between ? and ?) and (metricsent0_.LOC_ID is null) ) where rownum <= ?

Debug level Hibernate log excerpt:
13:27:38,060 DEBUG Loader:388 - processing result set
13:27:38,060 DEBUG Loader:393 - result set row: 0
13:27:38,060 DEBUG IntegerType:86 - returning '798' as column: PER1_
13:27:38,070 DEBUG IntegerType:86 - returning '29' as column: MKT2_
13:27:38,080 DEBUG IntegerType:80 - returning null as column: LOC3_
13:27:38,080 DEBUG Loader:795 - result row: null


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 2:35 pm 
Beginner
Beginner

Joined: Wed May 25, 2005 7:49 pm
Posts: 25
P.S. Sorry about putting everything in bold. I realize it's hard to read.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 6:42 pm 
Beginner
Beginner

Joined: Wed May 25, 2005 7:49 pm
Posts: 25
Ok, so I've figured out that composite-id elements cannot be null. But, the database does contain null elements. What's the best way around that? A custom UserType class?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 6:52 pm 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
Primary keys cannot have null values for any column if they are going to be true primary keys. You should be getting a ORA-01400: Cannot insert null... if the schema is configured correctly. I would think it is correct that Hibernate doesn't support this.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 7:02 pm 
Beginner
Beginner

Joined: Wed May 25, 2005 7:49 pm
Posts: 25
You're probably right. I was trying to use a composite id, but I guess I'll just use the rowid. There shouldn't be any problems there, right?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 7:08 pm 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
I haven't used ROWID through Hibernate, but I guess it would work. That's a question best left to the Hibernate developers. It isn't going to be portable across different database vendors, AFAIK. Do you know if the table even has a primary key? Could you add a surrogate key to the table that is generated from a sequence number and inserted by a trigger?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 7:13 pm 
Beginner
Beginner

Joined: Wed May 25, 2005 7:49 pm
Posts: 25
As far as I can tell, the table doesn't have a primary key on it. And unfortunately, the DBAs here are pretty territorial, so it might take an Act of Congress to get anything done. They don't allow triggers in any form, so I couldn't do that. I might be able to have the application generate it, but that's clearly not preferrable. I am trying for a surrogate key, though.

Thanks for all your help, by the way. I really appreciate you taking the time to help me out.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 7:28 pm 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
No prob...

Ouch! That worries me that the DBAs wouldn't let you add a PRIMARY KEY?!? to a table! You can tell if you have a primary key by querying the user_constraints view.

Code:
SELECT *
  FROM user_constraints
WHERE constraint_type = 'P' AND table_name = 'FCT_HISTORY';


Check out:

http://asktom.oracle.com/pls/ask/f?p=49 ... 0828234131

I'm not sure, personally, if you can do what you want to do without a PK. That's best answered by the Hibernate developers.

BTW, Download a trial of Toad for Oracle for its autoformat capabilities... e.g.

Code:
SELECT *
  FROM (SELECT metricsent0_.per_id AS per1_, metricsent0_.mkt_pos_id AS mkt2_,
               metricsent0_.loc_id AS loc3_
          FROM admet_promo_per_history metricsent0_,
               (SELECT per_id, per_level_name,
                       CASE
                          WHEN (per_level_name = 'ADVERTISING WK'
                               )
                             THEN ad_wk_begin_dt
                          WHEN (per_level_name = 'SCANNING WK')
                             THEN scan_wk_begin_dt
                          WHEN (per_level_name = 'PROMO PLAN PERIOD')
                             THEN promo_per_begin_dt
                          WHEN (per_level_name = 'CALENDAR MTH')
                             THEN cal_mnth_begin_dt
                          WHEN (per_level_name = 'ADVERTISING YR')
                             THEN cal_yr_begin_dt
                       END AS begin_date,
                       CASE
                          WHEN (per_level_name = 'ADVERTISING WK'
                               )
                             THEN ad_wk_end_dt
                          WHEN (per_level_name = 'SCANNING WK')
                             THEN scan_wk_end_dt
                          WHEN (per_level_name = 'PROMO PLAN PERIOD')
                             THEN promo_per_end_dt
                          WHEN (per_level_name = 'CALENDAR MTH')
                             THEN cal_mnth_end_dt
                          WHEN (per_level_name = 'ADVERTISING YR')
                             THEN cal_yr_end_dt
                       END AS end_date
                  FROM admet_period) metricsper1_
         WHERE metricsent0_.per_id = metricsper1_.per_id
           AND metricsper1_.per_level_name = ?
           AND (metricsper1_.begin_date BETWEEN ? AND ?)
           AND (metricsper1_.end_date BETWEEN ? AND ?)
           AND (metricsent0_.loc_id IS NULL))
WHERE ROWNUM <= ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 7:37 pm 
Beginner
Beginner

Joined: Wed May 25, 2005 7:49 pm
Posts: 25
lightc wrote:
No prob...

Ouch! That worries me that the DBAs wouldn't let you add a PRIMARY KEY?!? to a table! You can tell if you have a primary key by querying the user_constraints view.

I agree completely. Unfortunately, it's a little above my pay grade to correct their follies. I ran the query and came up with no results, so I'm assuming, no primary key.

lightc wrote:


Great article. Lots of good information!

lightc wrote:
BTW, Download a trial of Toad for Oracle for its autoformat capabilities... e.g.

Thanks...I'd been using DBVisualizer because it also supports AS/400, which they use here as well, but I'll give TOAD a try.

Thanks for all of your help. I really do appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 8:00 pm 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
Thanks for mentioning DBVisualizer. I hadn't tried it before, but I just downloaded it, and it looks really good. Toad is similar, but it has more features. (It also costs more.)

There's a free version of Toad, by the way.

http://www.toadsoft.com/downld.html

I'm not sure if it has the formatter, though.


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