-->
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.  [ 3 posts ] 
Author Message
 Post subject: How do I prevent Hibernate from creating an empty wrapper?
PostPosted: Fri Dec 15, 2006 8:06 pm 
Newbie

Joined: Fri Dec 15, 2006 7:43 pm
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

3.2

Mapping documents:

...
<component name="SomeList" class="org.example.SomeList">
<array name="SomeId" table="SomeTable">
<key column="ParentId" not-null="true" />
<list-index column="ArrayIndex" />
<element type="org.example.SomeIdType"
column="SomeId" not-null="true" unique="true" />
</array>
</component>
...

Name and version of the database you are using:

MySQL 5


Relevant Java code:

protected void validateSomeId(org.example.SomeId[] param){
if ((param != null) && (param.length < 1)){
throw new java.lang.RuntimeException();
}
}

public void setSomeId(org.example.SomeId[] param){
validateSomeId(param);
this.localSomeId=param;
}


Can someone help me create the proper mapping file to prevent Hibernate from initializing an empty array and adding it to my class?

I am trying to use Hibernate with Axis2 and one of the restrictions in my web service schema is that there needs to be one or more SomeId elements in the request. I used WSDL2Java to create my code and it added the validate method to make sure this restriction is met. However, when initializing the SomeList class, Hibernate tries to populate it with an empty wrapper and an exception is thrown.

Why does Hibernate initialize two arrays, one empty and one with the values from my database? Can't it just tell there is valid data and skip the first step?

Thanks for your help!

Yours,
Jason

P.S. I tried to use the access=”field” method instead, but it turns out Hibernate is first passing in the good array, but then it overwrites it with the empty one!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 8:26 pm 
Regular
Regular

Joined: Thu Jul 29, 2004 11:55 pm
Posts: 75
Not sure off hand why hibernate is creating the default empty array, but it might be using that for xxx.toArray(array[]). One way to get around your exception (and it is not elegant), would be to have another private method for hibernate to get/set the array that doesn't validate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 2:13 pm 
Newbie

Joined: Fri Dec 15, 2006 7:43 pm
Posts: 2
SOLVED!!!!!!

It turns out to be an issue with the hashcode of the primary key.

The primary key is a UUID that I generate as soon as I receive the webservice request, but it is stored in a org.apache.axis2.databinding.types.HexBinary class (the response type expected by the web service caller). It turns out, while it appears HexBinary implements the HashCode function, it just calls java.lang.object.hashcode! Since the default java implementation is based on the memory location, the returned hashcode changes across different sessions for the same UUID. As a result, hibernate generated two arrays, one for the original session and one for the new session!


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