Hello,
I have a short problem concerning the delete function on a webbased application.
Example code:
Code:
...
User user = new User( 1);
Machine machine = new Machine( 5);
UserMachine userMachine = new UserMachine( user, machine);
deleteObject( userMachine)
...
function deleteObject( Object object)
{
// Simplified
Session session = InitSessionFactory.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
session.delete( object);
transaction.commit();
}
...
Now this works fine and all, but see the current situation:
On the website there is a handy shortcut link to quickly delete the coupling between a user and a machine.
This is done by creating the user & machine objects with the appropriate user id and machine id, these two objects are put into another object (the many-to-many relationship table, why this is manually done is because the many-to-many table has some extra columns other then the two foreign keys ) and sending this to the delete function above (as you can see in the example code).
This works fine, the entry inside the UserMachine table is deleted right.
But now when you refresh the page (with the same user id and machine id), hibernate does not give an exception.
I have checked the log files and it does not execute the delete query at all (even though i call it).
So it seems hibernate checks something locally so it knows it's already deleted and thus not executing the query.
But I want it to throw an exception so I can block off someone F5'ing with the exception other then creating extra code to retrieve the values first and check if they match.
Is there anything wrong with my configuration or something else? (caching is not enabled, as far as i know)
Thank you
edit2:
When I delete a user, and I F5 that after that, I will get an unexpected row count (from hibernate), which I can catch and act appropriate on.
This is how I want it to work with userMachine as well.
So it seems there is a mapping mistake or something that i am doing wrong.
edit1:
Smaller made hibernate mapping files of usermachine user and machine:
User:
Code:
<class name="User" table="users">
<id name="userId" column="users_id" type="int">
<generator class="sequence">
<param name="sequence">users_users_id_seq</param>
</generator>
</id>
<property name="userName" column="users_name" type="string" />
<property name="userPassword" column="users_password" type="string" />
<!-- more properties -->
<set name="userMachines" cascade="all" lazy="false" inverse="true" >
<key column="users_id" />
<one-to-many class="UserMachine" />
</set>
</class>
Machine:
Code:
<class name="Machine" table="machine">
<id name="machineId" column="machine_id" type="int">
<generator class="sequence">
<param name="sequence">machine_machine_id_seq</param>
</generator>
</id>
<property name="machineSerialNumber" column="machine_serialnumber" type="string" />
<!-- more properties -->
<set name="userMachines" cascade="all" lazy="false" inverse="true" >
<key column="machine_id" />
<one-to-many class="UserMachine" />
</set>
</class>
UserMachine:
Code:
<class name="UserMachine" table="usersmachine">
<composite-id>
<key-many-to-one name="user" class="User" column="users_id" lazy="false" />
<key-many-to-one name="machine" class="Machine" column="machine_id" lazy="false" />
</composite-id>
<!-- properties -->
</class>