It requires far less database-activity if you do:
- Retrieve TeamMember from database with XPath-constraint [YourModule.ProjectHours_TeamMember = $ProjectHours].
- Aggregate list, Count.
If count = 0, you can commit, else don't.
You’re retrieving ProjectHours by association. Maybe the new ProjectHours is therefore also (always) in that list. Try retrieving from database. The new one is not yet committed, else you could include an xpath constraint [id != $ProjectHours] to retrieve a list without the object you’re checking.