views:

171

answers:

3

hi every one, 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?

+1  A: 

Please see: Chapter 11. Transactions and Concurrency

Mitch Wheat
I want quick Answer man , I myself know can read from this site
Am1rr3zA
It will take you 5-10 minutes at that link to work it out!!
Mitch Wheat
+2  A: 

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.

duffymo
Tanx but I don't know what is Spring declarative transactions exactly,i must read about this method.
Am1rr3zA
I'm curious - do you think it's ok for the DAO to (eagerly) flush the hibernate session when it completes it's portion of work, or should it wait for the transaction to end?
matt b
I'd wait for the transaction to end.
duffymo
A: 

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

Am1rr3zA