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.