| Author |
Message |
|
|
Post subject: hibernate spring tutorial on latest myeclipse no data
Posted: Jan 09, 2008 - 06:21 PM
|
|
Veteran Member


Joined: Apr 07, 2006
Posts: 5
|
|
i'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 |
|
|
| |
|
|
|
 |
|
|
Post subject: RE: hibernate spring tutorial on latest myeclipse no data
Posted: Jan 10, 2008 - 05:19 PM
|
|
Moderator


Joined: Jan 06, 2004
Posts: 23310
|
|
Peter,
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. |
_________________ Riyad
MyEclipse Support
|
| |
|
|
|
 |
|
|
Post subject: RE: hibernate spring tutorial on latest myeclipse no data
Posted: Jan 10, 2008 - 05:40 PM
|
|
Veteran Member


Joined: Apr 07, 2006
Posts: 5
|
|
thanks 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 |
|
|
| |
|
|
|
 |
|
|
Post subject: RE: hibernate spring tutorial on latest myeclipse no data
Posted: Jan 10, 2008 - 06:58 PM
|
|
Moderator


Joined: Jan 06, 2004
Posts: 23310
|
|
Peter,
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. |
_________________ Riyad
MyEclipse Support
|
| |
|
|
|
 |
|
|
Post subject: RE: hibernate spring tutorial on latest myeclipse no data
Posted: Jan 10, 2008 - 07:13 PM
|
|
Veteran Member


Joined: Apr 07, 2006
Posts: 5
|
|
i 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 |
|
|
| |
|
|
|
 |
|
|
Post subject: RE: hibernate spring tutorial on latest myeclipse no data
Posted: Jan 11, 2008 - 02:58 PM
|
|
Registered Member


Joined: Apr 18, 2007
Posts: 5662
|
|
In the UserDAO.java, replace the save, delete and merge methods with the following piece of code. It should get things working:-
| Code: |
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;
}
}
|
|
_________________ Nipun
MyEclipse Support
|
| |
|
|
|
 |
|
|
Post subject: RE: hibernate spring tutorial on latest myeclipse no data
Posted: Jan 11, 2008 - 03:06 PM
|
|
Veteran Member


Joined: Apr 07, 2006
Posts: 5
|
|
| thanks 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. |
|
|
| |
|
|
|
 |
|
|
Post subject: RE: hibernate spring tutorial on latest myeclipse no data
Posted: Jan 11, 2008 - 05:45 PM
|
|
Registered Member


Joined: Apr 18, 2007
Posts: 5662
|
|
| Your Welcome. |
_________________ Nipun
MyEclipse Support
|
| |
|
|
|
 |
|
|
Post subject: Update the tutorial
Posted: Jan 17, 2008 - 06:47 PM
|
|
Joined: Nov 05, 2007
Posts: 13
|
|
Hi,
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 |
|
|
| |
|
|
|
 |
|
|
Post subject: RE: Update the tutorial
Posted: Jan 17, 2008 - 07:23 PM
|
|
Moderator


Joined: Jan 06, 2004
Posts: 23310
|
|
Paras,
It's actually on our TODO list to refresh the tutorial project and put it into the Examples On-Demand repository directly for others. |
_________________ Riyad
MyEclipse Support
|
| |
|
|
|
 |
|
|
Post subject: RE: Update the tutorial
Posted: Jan 18, 2008 - 04:24 PM
|
|
Joined: Nov 05, 2007
Posts: 13
|
|
|
|
|
 |
|
|
Post subject: Spring's transaction management in Hibernate
Posted: Jan 18, 2008 - 04:48 PM
|
|
Joined: Nov 05, 2007
Posts: 13
|
|
By 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 finish
2) UserDAO should implement some interface. Define a new Interface
| Code: |
package com.myeclipse.hibernatespring;
public interface IUser {
}
|
3) Change class signature of UserDAO to
| Code: |
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
| 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
| Code: |
PersistenceLayer persistenceLayer = (PersistenceLayer) beanFactory
.getBean("persistenceLayer");
|
you should write
| Code: |
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.
| Code: |
persistenceLayer.deleteUser(user);
|
You can see entries in the databases now.
Cheers,
Paras |
_________________ Thanks,
Paras
http://parasjain01.wordpress.com/
Last edited by parasjain01 on Jan 21, 2008 - 10:04 PM; edited 2 times in total
|
| |
|
|
|
 |
|
|
Post subject: RE: Spring
Posted: Jan 18, 2008 - 05:45 PM
|
|
Moderator


Joined: Jan 06, 2004
Posts: 23310
|
|
Paras,
That was very nice of you to post that followup for folks. Thanks for doing that. |
_________________ Riyad
MyEclipse Support
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Jan 23, 2008 - 08:33 PM
|
|
Registered Member


Joined: Nov 07, 2005
Posts: 13
|
|
Thanx 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. |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Mar 21, 2008 - 06:39 AM
|
|
Joined: Mar 21, 2008
Posts: 1
|
|
Paras,
thanks for doing that. |
_________________ ---------------------------------------------------
cn boy
thanks.
|
| |
|
|
|
 |
|
|