-->
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: Composite key with null value for one column
PostPosted: Mon Jul 03, 2006 2:21 am 
Newbie

Joined: Thu Jun 29, 2006 3:51 am
Posts: 7
Hi,

I have table with composite primary key and I created following mapping for the table.As it is possible to insert a null value for any column in composite key as long as the combination of all columns is Unique, I have record in teh table which has null value for V_CHAR2 column ( which is part of composite key ) . when I execute a query on this entity I get null values for the records which are having null value of V_CHAR2 column. What's wrong in my mapping and implementation..

Please help ...
Thanks in advance



Hibernate version: 3.1

Mapping documents:
<hibernate-mapping package="com.test.testPK">
<class name="TESTCOMPPK" table="TESTCOMPPK">
<composite-id class="TESTCOMPPKPK" mapped="true" >
<key-property name="V_CHAR" column="V_CHAR" type="java.lang.String" />
<key-property name="V_NUM" column="V_NUM" type="java.math.BigDecimal" />
<key-property name="V_CHAR2" column="V_CHAR2" type="java.lang.String" />
<key-property name="V_NUM2" column="V_NUM2" type="java.math.BigDecimal" />
</composite-id>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():


Session session = sessionFactory.openSession();
Query query = session.createQuery("from TESTCOMPPK" );
dataList = query.list();
for (Iterator iter2 = dataList.iterator(); iter2.hasNext();)
{
Object element = (Object) iter2.next();
System.out.println(" ---> " + element);
}
session.close();



Name and version of the database you are using: Oracle

The generated SQL (show_sql=true):

select testcomppk0_.V_CHAR as V1_0_, testcomppk0_.V_NUM as V2_0_, testcomppk0_.V_CHAR2 as V3_0_, testcomppk0_.V_NUM2 as V4_0_ from TESTCOMPPK testcomppk0_




Output of the query is :

---> com.test.testPK.TESTCOMPPK@1385660
---> com.test.testPK.TESTCOMPPK@161dfb5
---> com.test.testPK.TESTCOMPPK@a613f8
---> com.test.testPK.TESTCOMPPK@16921fd
---> com.test.testPK.TESTCOMPPK@136a43c
---> null
---> com.test.testPK.TESTCOMPPK@3411a
---> com.test.testPK.TESTCOMPPK@1a7508a
---> com.test.testPK.TESTCOMPPK@198cb3d
---> com.test.testPK.TESTCOMPPK@472d48
---> com.test.testPK.TESTCOMPPK@2bc3f5
---> null
---> com.test.testPK.TESTCOMPPK@1acd47
---> com.test.testPK.TESTCOMPPK@19b04e2


note ( 6th record and 12th record are coming null bacause one column as null value in the database )


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 04, 2006 6:36 am 
Newbie

Joined: Thu Jun 29, 2006 3:51 am
Posts: 7
Hi , while checking the implementation of Object hydration I found following problem..

In ComponentType.hydrate() after getting the value from given resultset there is condition which checks if value is null and field is a Key field, return null , which is not correct in case of composite key.
If the field is part of composite key that should be handled seprately.

Follwoing is the code in ComponentType.hydrate() :

public Object hydrate(
final ResultSet rs,
final String[] names,
final SessionImplementor session,
final Object owner)
throws HibernateException, SQLException {

int begin = 0;
boolean notNull = false;
Object[] values = new Object[propertySpan];
for ( int i = 0; i < propertySpan; i++ ) {
int length = propertyTypes[i].getColumnSpan( session.getFactory() );
String[] range = ArrayHelper.slice( names, begin, length ); //cache this
Object val = propertyTypes[i].hydrate( rs, range, session, owner );
if ( val == null ) {
if (isKey) return null; //different nullability rules for pk/fk
}
else {
notNull = true;
}
values[i] = val;
begin += length;
}

return notNull ? values : null;
}


Highlighted code is causing problem in my code.. Can anyone sugegst ,wht should I do in this case .Is there any hooks so that I can put my own implementation there.

Thanks and Regards,
Prasoon


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 5:50 am 
Newbie

Joined: Thu Jun 29, 2006 3:51 am
Posts: 7
no reply :( ??


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 9:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
a primary key cannot be null (neither fully or partial)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 9:14 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Well, I don't know if there is a solution in fact. IMO it doesn't seem normal that one of the column of a primary composite key could be null...

Did you put a primary key constraint of the columns in your db? I just tried with Oracle 10G, it is impossible (null primary key)...

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 7:01 pm 
Newbie

Joined: Fri Jan 05, 2007 6:43 pm
Posts: 1
I'm having this same problem with a legacy database whose structure I cannot change. The table in question does not have a primary key, but does have a unique composite index which can be mapped as a key. In this case, one of the fields in the key CAN be null.

I understand that tables without primary keys are evil and all that jazz, but do any posters have suggestions for working around this that don't involve changing the table or the data in it? I refuse to believe that there is NO way to handle this situation in something as powerful and flexible as Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 06, 2007 5:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
sorry to disapoint you but null in primary key is not supported - primarily because doing join's and comparisons will require alot of painfullly stupid code that is just not needed anywhere else.....think about it and you will see (e.g. how do you do a *correct* join with such a table)

_________________
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.  [ 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.