Hello,
i work with these two tables :
model (*modelId, acquisitionCost);
mobile (*mobilId , *modelId);
Here are my mapping files :
Code:
<hibernate-mapping package="market.test.hibernate">
<class name="Model" table="model" catalog="market">
<id name="modelId" type="java.lang.Integer">
<column name="modelId" />
<generator class="identity" />
</id>
<property name="acquisitionCost" type="java.lang.Float">
<column name="acquisitionCost" precision="12" scale="0" />
</property>
<set name="mobiles" inverse="true" lazy="true" table="mobile" fetch="select">
<key>
<column name="modelId" not-null="true" />
</key>
<one-to-many class="Mobile" />
</set>
</class>
</hibernate-mapping>
and
Code:
<hibernate-mapping package="market.test.hibernate">
<class name="Mobile" table="mobile" catalog="market">
<id name="mobileId" type="int">
<column name="mobileId" />
<generator class="increment" />
</id>
<many-to-one name="model" class="Model" fetch="select">
<column name="modelId" not-null="false" />
</many-to-one>
<property name="configId" type="string">
<column name="configId" length="45" />
</property>
<set name="loadcapacities" inverse="true" lazy="true" table="loadcapacity" fetch="select">
<key>
<column name="mobileId" not-null="true" />
</key>
<one-to-many class="Loadcapacity" />
</set>
</class>
</hibernate-mapping>
I have an entry in my table model(modelId=1, acquisitionCost=3000),
and two in the table mobile (mobilId=1,modelId=null et mobilId=2, modelId=null).
I want to update mobile table by setting the field modelId of the two entries with the value 1.
This is my code :
Code:
private void addMobileToModel(int mobileId, int modelId) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Mobile mobile = (Mobile) session.load(Mobile.class, mobileId);
Model model = (Model) session.load(Model.class, modelId);
model.getMobiles().add(mobile);
session.getTransaction().commit();
}
When I call this code, there is no error but my Data base is not updated.
Does anyone has an idea ?
Thanks.