-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: When persistent object's changes are saved to database?
PostPosted: Mon Apr 06, 2015 4:59 pm 
Newbie

Joined: Mon Apr 06, 2015 3:54 pm
Posts: 1
I am using the latest 4.3.8 version of Hibernate.

I have the following JUinit test working code but some behaviors I cannot understand.

By reading the manual, the changes of a persistent object will be saved to database after the flush(). But I look at the database table, after the Step 7. the changes, "updatePassword2" and the new permission, are not saved to the database table.

The Step 7 actually closes the session and the persistent object is changed to detached object.

Step 8 is another session and call get() to retrieve the row. But to my surprise, Step 6's changes are written to database in this get() call but not the Step 7 flush() and close().

Note, I disable/enable code step by step and query the database from outside to get above result.


EDIT:
In Step 8, if I disable the get() statement, just leave the Transaction there, it will write the changes into database. Cannot understand. How do I know when the modified persistent object or detached object will be written to database?


Code:
//Step 1: create transient object
            Session session = HibernateDBUtil.getSession();
            String pwd = "password";
            UserAccount newUser = new UserAccount("testUpdate001", //String username,
                    pwd,//String password,
                    false,//boolean isDeleted,
                    false, //boolean isLocked,
                    new Date(),//Date lastPwdModify,
                    (short)0, //short loginFailedCount,
                    false, //boolean isTempPwd,
                    "Test", //String firstName,
                    "Unit", //String lastName,
                    new Date(), //Date userAccountCreateDateTime,
                    new Date() //Date userAccountUpdateDateTime
                    );

            //Step 2: save to database
            Transaction tx = session.beginTransaction();
            long newId = ((Number)session.save(newUser)).longValue();
            session.flush();
            tx.commit();

            //Step 3: get back and get the persistent object
            tx = session.beginTransaction();//to avoid dirty read
            UserAccount loadedObj = (UserAccount)session.get(UserAccount.class, newId);
            tx.commit();
            Assert.assertNotNull(loadedObj);
            Assert.assertEquals(loadedObj.getPassword(), pwd);
            Assert.assertEquals(loadedObj.getUserPermissions().size(), 0);

            //Step 4: change the persistent object
            Set<UserPermission> permissions = new HashSet();
            permissions.add(RuntimeDataStore.getUserPermission(UserPermissionEnum.Operator));
            permissions.add(RuntimeDataStore.getUserPermission(UserPermissionEnum.Manager));
            newUser.setUserPermissions(permissions);
            String newPwd = "updatedPassword";
            newUser.setPassword(newPwd);

            //Step 4: update the changes onto database
            tx = session.beginTransaction();
            session.saveOrUpdate(newUser);
            session.flush();
            tx.commit();

            //Step 5: get back the persistent object
            tx = session.beginTransaction();//to avoid dirty read
            loadedObj = (UserAccount)session.get(UserAccount.class, newId);
            tx.commit();

            Assert.assertEquals(loadedObj.getPassword(), newPwd);
            Assert.assertEquals(loadedObj.getUserPermissions().size(), 2);

            //Step 6: modify the persistent object again
            loadedObj.getUserPermissions().add(RuntimeDataStore.getUserPermission(UserPermissionEnum.TechnicalLeader));
            String newPwd2 = "updatedPassword2";
            loadedObj.setPassword(newPwd2);

            //Step 7: flush and close the session to make above change onto database
            session.flush();
            session.close();

            //Step 8: start another session and get the data from database
            session = HibernateDBUtil.getSession();
            tx = session.beginTransaction();
            loadedObj = (UserAccount)session.get(UserAccount.class, newId);
            tx.commit();

            Assert.assertEquals(loadedObj.getPassword(), newPwd2);
            Assert.assertEquals(loadedObj.getUserPermissions().size(), 3);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.