-->
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.  [ 2 posts ] 
Author Message
 Post subject: Newbie problem with object identifiers after update
PostPosted: Wed Jan 27, 2010 11:33 am 
Newbie

Joined: Fri Jan 22, 2010 11:24 am
Posts: 1
Hello, I am new at Hibernate and I need help with this problem:

I have the Service entity, which contains a Set<ServicePeriod>. Both entities are mapped to corresponding tables.
I do the following:
1) Retrieve Service by id
2) Add a new ServicePeriod to it (bidirectional association)
3) Perform an update on the Service object retrieved in (1)

After these operations, I can see the new ServicePeriod has been written correctly to database, but the field servicePeriodId (in Java object) is still null. I need this field not to be null, so I can perform other tasks using that value.

I am using SQL Server 2005.
These are the classes

Code:
public class Service {

   private String serviceCode;
   private Set<ServicePeriod> servicePeriods;
   // other fields, constructors, getters and setters...
   
   public void addServicePeriod(ServicePeriod servicePeriod){
      if (this.servicePeriods == null){
         this.servicePeriods = new HashSet<ServicePeriod>();
      }
      this.servicePeriods.add(servicePeriod);
      servicePeriod.setServiceCode(this);
   }
}

public class ServicePeriod {
   private Integer servicePeriodId;
   private Service serviceCode;
   
   // other fields, constructors, getters and setters
}

And these are the HBM mappings:
Code:
<class name="mypackage.Service" table="service">
   <id name="serviceCode" type="string">
      <column name="service_code" length="5" />
      <generator class="assigned" />
   </id>
   <!-- other properties... -->       
   <set name="servicePeriods" inverse="true" cascade="all-delete-orphan">
      <key><column name="service_code" /></key>
      <one-to-many class="mypackage.ServicePeriod" />
   </set>
</class>

<class name="mypackage.ServicePeriod" table="service_period">
   <id name="servicePeriodId" type="int">
      <column name="service_period_id" />
      <generator class="native" />
   </id>
   <many-to-one name="serviceCode" class="mypackage.Service">
      <column name="service_code" length="5" not-null="true" />
   </many-to-one>
   <!-- other properties... -->
</class>

I will appreciate your help. Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Newbie problem with object identifiers after update
PostPosted: Thu Jan 28, 2010 4:52 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Have you checked that the ServicePeriod.setServicePeriodId() method really updates the field?


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