Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using a optimistic strategy when updating entities by setting "<version />" element in hbm.xml s. It works fine when I update a single entity. But this strategy fails when dealing with this scenario:

public class SalesPlan {
    //omitted fields
    private Resource resource;
    private DateRange dateRange;
}


public class Resource {
    //omitted fields
    private int version = 1;
}

and there is a constraint: A Resource should not have SalesPlan s with overlapped dateRange.For example:

Given there is a Resource named "Hippoom resort"

And it has a SalesPlan ranging from Nov 1, 2013 to Nov 2, 2013

When I want to add a SalesPlan ranging from Nov 2, 2013 to Nov 2, 2013

Then It should fail for overlapped date range

I have to implement this in Java because database unique key does not work in this "range" case.The code looks like this:

@Transactional
@Override
public SalesPlan handle(CreateSalesPlanCommand command) {
    Resource resource = resourceRepository.findBy(command.getResourceId());
    SalesPlan salesPlan = //omitted init codes

    DuplicateSalesPlanSpecification spec = aDuplicateSpec();

    if (spec.isSatisfiedBy(salesPlan)) {
        throw new DuplicateSalesPlanException(salesPlan);
    }

    salesPlanRepository.store(salesPlan);
    resourceRepository.store(salesPlan.getResource());
    return salesPlan;
}

I fetch all existed SalesPlan s from the database in DuplicateSalesPlanSpecification to check if the new SalesPlan breaks the constraint.I want to update the Resource in the last step (check the version number in Resource) in case of concurrent operations. But I note there is no update sql because the Resource is not dirty.

The Hibernate version is 3.6.10.FINAL.Is there any possibilities I can fix this?

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.