-->
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: Not loading Set with unidirectional non-lazy set association
PostPosted: Thu Apr 20, 2006 3:10 pm 
Newbie

Joined: Thu Apr 20, 2006 2:12 pm
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

I have a unidirectional,non-lazy, one-to-many association between the class ServiceStatus to the class ScheduledOutage. The association is declared as a set within ServiceStatus.

I do not want a bidirectional association because I want to be able to store the child entity (ScheduledOutage) without having retrieved the parent entity (ServiceStatus). However, I have tried using a bidirectional association and I get the exact same problem.

The following operations work:
1. Storing a ServiceStatus entity
2. Storing a ScheduledOutage with a foreign key to an existing ServiceStatus entity
3. Loading the values of the ServiceStatus entity.

However, the following operations do not work:
1. Retrieving a ServiceStatus with no ScheduledOutages never sets an empty set into my loaded ServiceStatus entity
2. Retrieving a ServiceStatus with a ScheduledOutage does not assign anything to the set either.

The query that gets executed for the operations that dont work does not take into consideration of the non-lazy set assocation when retrieving the parent object. You can see the query that gets executed in the generated SQL section.

Thanks :)

Hibernate version:
Hibernate 3.1.3

Mapping documents:

Code:
<hibernate-mapping default-lazy="false" default-cascade="none">

    <class name="somePackage.ServiceStatus" table="SERVICE_STATUS">
       
        <id name="id" type="long">
            <column name="SERVICE_STATUS_ID"/>
            <generator class="native"/>
        </id>
       
        <!--  Other attribute mappings go here  -->
        ...
         
        <set name="scheduledOutages" cascade="all-delete-orphan" inverse="true">
           <key column="SERVICE_STATUS_ID" foreign-key="FK_SERVICE_STATUS_ID"/>           
           <one-to-many class="somePackage.ScheduledOutage"/>     
        </set>

    </class>
     
    <class name="somePackage.ScheduledOutage" table="SCHEDULED_OUTAGE">
       
        <id name="id" type="long">
            <column name="SCHEDULED_OUTAGE_ID"/>
            <generator class="native"/>
        </id>
      
      <property name="serviceStatusId" type="long" index="SERVICE_STATUS_ID_IDX">
            <column name="SERVICE_STATUS_ID" not-null="true" />
        </property>

        <!--  Other attribute mappings go here  -->
        ...
               
    </class> 
       
    <!-- Retrieving a services status using the service's unique handle -->     
    <query name="FIND_SERVICE_STATUS_BY_HANDLE">
       <![CDATA[
          select ss from ServiceStatus ss where ss.serviceHandle = :serviceHandle
        ]]>
    </query>
   
</hibernate-mapping>




Entity Java classes:

ServiceStatus (parent):

Code:
public class ServiceStatus implements Serializable, IScheduledOutage {

   private static final long serialVersionUID = -7847990753910263013L;
   
   private Long id;
   private Set<IScheduledOutage> scheduledOutages;
   
   // Some more attributes here
   ...

   // Getters and setters go here
   ...
   
   public boolean equals(final Object other) {
      
      boolean equals = false;
   
      if(this == other) {
         equals = true;
      } else if ((other == null) || !(other instanceof IServiceStatus)) {
         equals = false;
      } else {
         
         IServiceStatus castOther = (IServiceStatus) other;

         EqualsBuilder builder = new EqualsBuilder();

         // Attributes used for business key go here
         ...
         
         equals = builder.isEquals();         
      }
      
      return equals;      
   }

   public int hashCode() {
      
      HashCodeBuilder builder = new HashCodeBuilder();
      
      // Attributes for hashcode from business key go here
      ...
      
      return builder.toHashCode();
   }
}


ScheduledOutage (child):

Code:
public class ScheduledOutage implements IScheduledOutage, Serializable {

   private static final long serialVersionUID = 629186884530944597L;
   private Long id;   
   // Foreign key attribute
   private Long serviceStatusId;
   
   // Some more attributes here
   ...

   // Getters and setters go here
   ...
      
   public boolean equals(final Object other) {
      
      boolean equals = false;
   
      if(this == other) {
         equals = true;
      } else if ((other == null) || !(other instanceof IScheduledOutage)) {
         equals = false;
      } else {
         
         IScheduledOutage castOther = (IScheduledOutage) other;

         EqualsBuilder builder = new EqualsBuilder();
         
         // Attributes used for business key go here
         ...
         
         equals = builder.isEquals();         
      }
         return equals;      
   }

   public int hashCode() {
      
      HashCodeBuilder builder = new HashCodeBuilder();
      
      // Attributes for hashcode from business key go here
      ...
      
      return builder.toHashCode();
   }   
}




The generated SQL (show_sql=true):

The following SQL gets generated when I perform the following actions:
1. Store a ServiceStatus entity
2. Store a ScheduledOutage entity associated with the above entity
3. Retrieve the ServiceStatus entity

The last query that gets executed does not include anything about the
child entity table.

Code:
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into SERVICE_STATUS (SERVICE_HANDLE, SERVICE_NAME, HEALTHY, LAST_UPDATE, SERVICE_STATUS_ID) values (?, ?, ?, ?, ?)
Hibernate: insert into SCHEDULED_OUTAGE (SERVICE_STATUS_ID, OWNER_ID, REASON, START_TIME, END_TIME, SCHEDULED_OUTAGE_ID) values (?, ?, ?, ?, ?, ?)
Hibernate: select servicesta0_.SERVICE_STATUS_ID as SERVICE1_0_, servicesta0_.SERVICE_HANDLE as SERVICE2_0_, servicesta0_.SERVICE_NAME as SERVICE3_0_, servicesta0_.HEALTHY as HEALTHY0_, servicesta0_.LAST_UPDATE as LAST5_0_ from SERVICE_STATUS servicesta0_ where servicesta0_.SERVICE_HANDLE=?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 3:17 pm 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
By default lazy is true in Hibernate 3. Add explicitly lazy=false to the Set of scheduledOutages as follows:

Code:
<set name="scheduledOutages" cascade="all-delete-orphan" inverse="true" lazy="false">
           <key column="SERVICE_STATUS_ID" foreign-key="FK_SERVICE_STATUS_ID"/>           
           <one-to-many class="somePackage.ScheduledOutage"/>     
        </set>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 3:22 pm 
Newbie

Joined: Thu Apr 20, 2006 2:12 pm
Posts: 2
I already have the following in my mapping file which will make lazy to default to false:

<hibernate-mapping default-lazy="false" default-cascade="none">

Just to confirm, I added lazy="false" on the set tag and did not solve the problem.


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.