- This topic has 23 replies, 9 voices, and was last updated 16 years, 9 months ago by TonyHH.
-
AuthorPosts
-
alderionMemberi’m not sure what exactly is up but the tutorial doesn’t actually save any data in the db. if i comment out the delete line of the business logic and then check the database, there aren’t any rows.
if i set the ‘connection.autocommit’ property to true in the hibernate.cfg.xml file, it does save data.
autocommit isn’t ideal. how do you get it to save data without autocommit? how do you get your hands on a transaction so you can commit “manually”?
thanks,
-peter
Riyad KallaMemberPeter,
The tutorial wasn’t written with transactions originally to help keep it simple, so your information is cached in the session until Hibernate decides to flush it. Using autocommit just makes sure Hibernate flushes it every time anyway.You can check the hibernate docs on using the transactions in hib to perform the commit yourself, I dug through some code on my end and didn’t have the version of that example *with* transactions in it to provide to you… sorry about that.
I pinged another developer on the team to see if he had it laying around, I knew we messed with it a while ago to provide as an example but I just don’t have access to it off the top of my head. Sorry about that.
alderionMemberthanks for the reply. i don’t think what you are saying, ‘…so your information is cached in the session until Hibernate decides to flush it..’, is true. the example is a simple main() class. why wouldn’t hibernate flush its session when the program ended. what i am saying is that if you try to use the example ‘as is’ it will not save data to the db. the only reason the example appears to work is because you delete the object as the last step. if you comment out the delete statement, no data ever gets saved to the db. i think that is a poor example.
i would really appreciate it if you could track down the transactional code, it would be very helpful.
thanks,
-peter
Riyad KallaMemberPeter,
I’ll try and dig up the transactions code for you by tommorow.As for the example, if instead of the delete, you put another bit of code right before it to query the DB to get the same entity back, does it return something? My guess is yes because it’s still all cached in the session. Hibernate likes caching stuff unless you tell it not to. You can try flushing the session before the exit.
alderionMemberi look forward to your reply tomorrow. thanks!
yes, all the operations in the example succeed because you are using the cache; however, as soon as you try to extend the example by creating a new main class and trying to read the data from there, you won’t get any data.
it seems to me any complete example should actually store things in the db. when i used previous version of myeclipse, i seem to recall that there was a hibernate utility generated from which you could request a transaction object, begin a transaction, do work and then commit the transaction. i don’t see anything similar in the current generated code.
at the very least, i think you should explicitly state that nothing will get to the db using this example unless you set the connection.autocommit property to true in hibernate.cfg.xml. at least that was my experience with mysql.
again, thanks for the help. i look forward to hearing from you on the transactional example.
-peter
Loyal WaterMemberIn the UserDAO.java, replace the save, delete and merge methods with the following piece of code. It should get things working:-
public void save(User transientInstance) { log.debug("saving User instance"); try { Session session = HibernateSessionFactory.getSession(); Transaction tx = session.beginTransaction(); tx.begin(); session.save(transientInstance); tx.commit(); log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } } public void delete(User persistentInstance) { log.debug("deleting User instance"); try { Session session = HibernateSessionFactory.getSession(); Transaction tx = session.beginTransaction(); tx.begin(); session.delete(persistentInstance); tx.commit(); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } public User merge(User detachedInstance) { log.debug("merging User instance"); try { Session session = HibernateSessionFactory.getSession(); Transaction tx = session.beginTransaction(); tx.begin(); User result = (User) session .merge(detachedInstance); tx.commit(); log.debug("merge successful"); return result; } catch (RuntimeException re) { log.error("merge failed", re); throw re; } }
alderionMemberthanks very much. seeing it now, it is very easy. i was trying to use the hibernate template, getting rid of it altogether was a bit of a mental block.
Loyal WaterMemberYour Welcome.
parasjain01MemberHi,
Thanks for the updated code. Can someone please update the tutorial for the same. So that next time if anyone tries the tutorial he should not be wondering why the data is not saved in the DB.Paras
Riyad KallaMemberParas,
It’s actually on our TODO list to refresh the tutorial project and put it into the Examples On-Demand repository directly for others.
parasjain01MemberThanks Riyad
parasjain01MemberBy the time MyEclipse tutorial is updated if someone wants to do the transaction management using spring. This is what you have to do.
1)
a) Go to project properties- Java Build Path -> Libraries Tab -> Select Add Library button.
b) Select MyEclipse Library and press next.
c) Select Spring 1.2 AOP libraries
d) Press finish2) UserDAO should implement some interface. Define a new Interface
package com.myeclipse.hibernatespring; public interface IUser { }
3) Change class signature of UserDAO to
public class UserDAO extends HibernateDaoSupport implements IUser{
4)applicationContext.xml has to be updated with new beans etc. Below is the entire applicationContext.xml code
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="file:src/hibernate.cfg.xml"> </property> </bean> <bean id="userDAOTarget" class="com.myeclipse.hibernatespring.UserDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="userDAOService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="transactionManager"/> <property name="transactionAttributes"> <props> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> </props> </property> <property name="target"><ref local="persistenceLayer"/></property> </bean> <bean id="persistenceLayer" class="com.myeclipse.hibernatespring.PersistenceLayer" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default"> <property name="userDAO"> <ref bean="userDAOTarget" /> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"><ref bean="sessionFactory"/></property> </bean> </beans>
5) Go to BusinessLogic.java. Instead of writing
PersistenceLayer persistenceLayer = (PersistenceLayer) beanFactory .getBean("persistenceLayer");
you should write
PersistenceLayer persistenceLayer = (PersistenceLayer) beanFactory .getBean("userDAOService");
6) After making this changes the code should run fine and it should automatically persist the persistent objects.
Try removing the following line of code in BusinessLogic class and run the program.
persistenceLayer.deleteUser(user);
You can see entries in the databases now.
Cheers,
Paras
Riyad KallaMemberParas,
That was very nice of you to post that followup for folks. Thanks for doing that.
DanMemberThanx for the info guys on doing with Spring and directly via Hibernate.
I would like to just ask if it would be possible to get another example that say shows adding 2 new users without actually committing until the 2nd user was added. I can see how I could do this with hibernate, but I have no clue how could accomplish this using Spring’s Session logic/config.
cnboyMemberThis message has not been recovered.
-
AuthorPosts