-->
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: Optional components and a one-to-many relations
PostPosted: Fri Nov 21, 2008 12:27 pm 
Newbie

Joined: Wed Oct 08, 2008 1:54 am
Posts: 14
I'm fighting with a problem with an optional component I added to my class which has a one-to-many relation inside to another table. The problem is that the component member object is never null and always at least has an empty collection of referenced instances of the mentioned second object.
Simplified the setup is as follows:

Code:
<class name="Person" table="persons" > 
    <id name="id" column="id" type="int" access="field">
        <generator class="increment" />
    </id>
   
    <component name="healthCare" >
        <property name="state" column="health_state" type="String" />
       
        <property name="insuranceNumber" column="insurance_number" type="long" />
       
        <list name="doctorVisists" >
            <key column="person_id" />
            <list-index column="queue_index" base="0"/>
            <one-to-many class="DoctorVisit" />
        </list>
    </component>
</class>


The main Problem with that is that the healthCare component also has some primitive types (insuranceNumber in the sample above) and reading the just saved Person object which had no healthCare Object set results in an "IllegalArgumentException: Can not set long field insurance_number n to null value". Of course I could simply change all members of the HealthCare object from primitives to objects but somehow this does not feel right.

Does someone know about a simple work-around so that empty collections do not prevent components from being null?

I'm using Hibernate 3.4.1.GA but switching to another version is easyly done.


Top
 Profile  
 
 Post subject: Re: Optional components and a one-to-many relations
PostPosted: Wed Jul 28, 2010 7:10 am 
Regular
Regular

Joined: Thu Aug 28, 2003 2:42 pm
Posts: 77
Location: The Netherlands
Did you find a solution for this?
I am having the same problem :(


Top
 Profile  
 
 Post subject: Re: Optional components and a one-to-many relations
PostPosted: Thu May 02, 2013 5:23 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
I'm also having this problem. Any solution?


Top
 Profile  
 
 Post subject: Re: Optional components and a one-to-many relations
PostPosted: Tue May 07, 2013 1:29 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
The problem is in ComponentType.hydrate. It's checking all the properties for nullness and gives no special treatment to collections. Arguably, it should.

Since my collection is just a small set of strings, I'm working around it for now by exposing a separate string property that reads from and writes to the collection using a JSON array string. I mapped that property instead.


Last edited by rhasselbaum on Tue May 07, 2013 1:31 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Optional components and a one-to-many relations
PostPosted: Tue May 07, 2013 1:31 pm 
Regular
Regular

Joined: Thu Aug 28, 2003 2:42 pm
Posts: 77
Location: The Netherlands
rhasselbaum wrote:
The problem is in ComponentType.hydrate. It's checking all the properties for nullness and gives no special treatment to collections. Arguably, it should..

Could you please put it in the hibernate issue tracker as Feature request?


Top
 Profile  
 
 Post subject: Re: Optional components and a one-to-many relations
PostPosted: Tue May 07, 2013 1:58 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
edbras wrote:
Could you please put it in the hibernate issue tracker as Feature request?


It wouldn't work for lazy-loaded collections. You'd need to query the collection tables in order to tell if the collections are empty or not. I'm not sure it's worth it.


Top
 Profile  
 
 Post subject: Re: Optional components and a one-to-many relations
PostPosted: Wed May 08, 2013 7:26 pm 
Beginner
Beginner

Joined: Sat Sep 24, 2005 11:04 pm
Posts: 21
Not to downplay what may be a legitimate bug here... but I can't help but wonder why you're modeling your classes in this way. If insuranceNumber and state are specific to an individual (due to the fact that the columns exist in the "persons" table) then why would you place these in the component class? If these properties are specific to an instance of HealthCare then I would expect them to exist in a joined table ("healthcare" or whatever) with "doctorVisits" having a foreign key reference to "healthcare" (rather than "persons").

If your requirements are simply to display a list of "doctorVisits" for an individual (assuming they have only one insurance provider), then you might want to move the state and insuranceNumber properties to Person and try something along the lines of:
Code:
<class name="Person" table="persons" > 
  <id name="id" column="id" type="int" access="field">
    <generator class="increment" />
  </id>
  <property name="state" column="health_state" type="String" />
  <property name="insuranceNumber" column="insurance_number" type="long" />
  <list name="doctorVisits">
    <key column="person_id" />
    <list-index column="queue_index" base="0"/>
    <one-to-many class="DoctorVisit" />
  </list>
</class>

If the reason for structuring your classes like this is to allow for an HQL query involving state and insuranceNumber from DoctorVisit, then add a bidirectional association from DoctorVisit to person and reference the properties as doctorVisit.person.insuranceNumber, doctorVisit.person.state, etc.

Code:
<class name="DoctorVisit" table="doctor_visits"> 
  <id name="id" column="id" type="int" access="field">
    <generator class="increment" />
  </id>
  <many-to-one name="person" column="person_id" not-null="true"/>
  ...
</class>

And by the way... if insurance_number is a nullable column in your table then you should most definitely declare this as a Long rather than a primitive (considering null cannot be assigned to a primitive).


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.