-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate with spring
PostPosted: Tue Jan 11, 2005 2:04 am 
Newbie

Joined: Thu Jan 06, 2005 7:27 am
Posts: 2
Location: India
I am using hibernate with spring. I am new to this... and have no idea whats wrong. I am faced with two problems :

When i try to explicity update the database I get the exception shown below.

If i dont use the update method and when i change any attribute in the persistent object , that change is not getting reflected in the database. I have extended HibernateDaoSupport and use the getHibernateTemplate() method in it.

public User getUser(String username)
{
User user=null;
try
{
user = (User)getHibernateTemplate().createQuery("from User user where user.username like '"+username+"'").uniqueResult();
user.setEmail("abcd@yahoo.com");
getHibernateTemplate().update(user);
}
catch (Exception e)
{
e.printStackTrace();
}
return user;
}

Hibernate version:
Hibernate 2.1.7

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="hibernate.entities">
<class name="User" table="user_details" lazy="true">
<id name="userid" type="string">
<column name="user_id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="firstname"/>
<property name="lastname"/>
<property name="sex"/>
<property name="username"/>
<property name="rating"/>
<property name="blocked"/>
<property name="created" type="java.util.Date"/>
<property name="email"/>
<many-to-one name="address" column="addressid" class="hibernate.entities.Address"/>
<set name="currentselling">
<key column="sellerid"/>
<one-to-many class="hibernate.entities.Auction"/>
</set>

<set name="currentbids">
<key column="bidderid"/>
<one-to-many class="hibernate.entities.Bid"/>
</set>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

net.sf.hibernate.LazyInitializationException: Illegally attempted to associate a proxy with two open Sessions
at net.sf.hibernate.proxy.LazyInitializer.setSession(LazyInitializer.java:152)
at net.sf.hibernate.impl.SessionImpl.reassociateProxy(SessionImpl.java:1026)
at net.sf.hibernate.impl.SessionImpl.reassociateIfUninitializedProxy(SessionImpl.java:975)
at net.sf.hibernate.impl.SessionImpl.update(SessionImpl.java:1343)
at hibernate.actions.AuctionTransactionHibernate.getUser(AuctionTransactionHibernate.java:235)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.springframework.aop.framework.AopProxyUtils.invokeJoinpointUsingReflection(AopProxyUtils.java:59)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:138)
at $Proxy0.getUser(Unknown Source)
at hibernate.actions.TempRunner.<init>(TempRunner.java:33)
at hibernate.actions.TempRunner.main(TempRunner.java:25)

Name and version of the database you are using:
Oracel iSQL plus

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 6:55 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
This problem actually belongs on the Spring forum, but here goes:

It sounds to me like you're somehow getting more than one session open and proxied objects are being updated or locked in both sessions. Perhaps each call to getHibernateTemplate() is creating a new Hibernate instance?

One possible cause is that your spring session factory bean or transaction manager bean is marked as singleton="false" in your spring configuration. Verify that your session factory and transaction manager are not marked that way. If they are then you could be getting a new instance of the hibernate configuration on each method call.

If this isn't the problem try posting on the Spring Framework forums. This sounds like more of a Spring problem to me.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 11:39 pm 
Regular
Regular

Joined: Tue Sep 28, 2004 6:34 pm
Posts: 50
instead of:
Quote:
Code:
public User getUser(String username)
{
User user=null;
try
{
user = (User)getHibernateTemplate().createQuery("from User user where user.username like '"+username+"'").uniqueResult();
user.setEmail("abcd@yahoo.com");
getHibernateTemplate().update(user);
}
catch (Exception e)
{
e.printStackTrace();
}
return user;
}



use:

Code:
public User getUser(final String username)
{
User user= (User) hibernateTemplate.execute(
                    new HibernateCallback() {
                        public Object doInHibernate(Session session) throws HibernateException {
   User user = session.createQuery("from User user where user.username like '"+username+"'").uniqueResult();
    user.setEmail("abcd@yahoo.com");
    session.update(user);
     return user;
                         }
              }
       );
  return user;
}


Haven't test it - but you should get an idea.

Lukasz[/quote]


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

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.