Question

I have some hibernate code and I want my code run in 1 transaction let me explain in code

public void changeBranch(Branch branch) throws DatabaseException {
//some code
            humanDao.update(he);
            superBranchUsername = branch.getFatherUsername();
            int superBranchId = branchDao.getBranchIdByUserName(superBranchUsername);
            BranchEntity superBranch = branchDao.load(superBranchId);
            BranchEntity be = new BranchEntity();
            setBranchEntity(be, he, pkId, bname, confirmed, level, studentCount, uname, superBranch);
            branchDao.update(be);   // update kardane jadvale Branch va Set kardane Human motenazer be on
//some code
}

Both humanDao.update(he); and branchDao.update(be); run in transaction handle by My GenericDAO that humanDao and branchDao are inherited from it. but I want this block of code (wrote above) to also run in a transaction!! How can I get to Hibernate to do this?

Was it helpful?

Solution 3

I find how should I fix it if I new session in changeBranch(Branch branch) and pass this session as a parameter to my DAO my problem solved

OTHER TIPS

DAOs should not handle transactions for exactly the reason you've discovered: they can't know when they're part of a larger transaction.

If you were using Spring declarative transactions, you'd have a service layer that would create the transaction context for both DAOs and deal with everything. I would recommend doing something like that.

UPDATE: I added a link to Spring.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top