-->
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.  [ 12 posts ] 
Author Message
 Post subject: Database Is not updated
PostPosted: Wed Feb 04, 2009 7:40 am 
Newbie

Joined: Sun Feb 01, 2009 6:29 pm
Posts: 15
Hello
I'm using Spring and Hibernate v.3.2 with MySql. when I'm doing update or insert it looked ok and the data is saved but not in the database it saved just in cash.
Do I need to change sothing in mySql configuration fil?

-------- hibernate-configuration ------------

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/games
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">
MySql Connector 5.1.7
</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="pool_size">12</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>

</hibernate-configuration>


----- BusinessLogic Code ---------

import java.util.Iterator;
import java.util.List;

import org.hibernate.Transaction;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class BusinessLogic {
public static void main(String[] args) {

/* 2. Load the Spring bean configuration and create a bean factory */
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
"applicationContext.xml"));

System.out.println(beanFactory);
/* 3. Create instance of PersistenceLayer */

/* 5. Confirm that our user was saved */
Test2 userLoadedFromDB;
try {
PersistenceLayer persistenceLayer = (PersistenceLayer) beanFactory
.getBean("PersistenceLayer");

Test2 test2 = new Test2();
test2.setId(new Integer(33));
test2.setName("Adidas");
/* 4. Save the new user to the database */

persistenceLayer.addTest2DAO(test2);


userLoadedFromDB = persistenceLayer.findTest2DAO(new Integer(11));
System.out.println("User Loaded from DB Name="+ userLoadedFromDB.getName());
/* 6. Update the user */
userLoadedFromDB.setName("Gillgoolina");
userLoadedFromDB.setId(new Integer(14));
persistenceLayer.updateTest2DAO(userLoadedFromDB);

/* 7. Confirm that the update worked */
List all = persistenceLayer.getAll();
for(Iterator i=all.iterator(); i.hasNext();){
Test2 t2 = (Test2)i.next();
System.out.println(t2.getId()+ " name: " + t2.getName());
}

/* 8. Delete the user */
// persistenceLayer.deleteUser(user);
} catch (BeansException e) {
System.out.println("BeansException: "+e.getMessage());
//e.printStackTrace();
}

}
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 8:09 am 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
What does the code within PersistenceLayer look like?

It's difficult to tell what's going on when we can't see how you're interacting with Hibernate.

--
Stephen Souness


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 8:16 am 
Newbie

Joined: Sun Feb 01, 2009 6:29 pm
Posts: 15
The PersistenceLayer code

package snir.test.hibernatespring;

import java.util.List;

public class PersistenceLayer {
private Test2DAO test2DAO;

public Test2DAO getTest2DAO() {
return test2DAO;
}

public void setTest2DAO(Test2DAO test2DAO) {
this.test2DAO = test2DAO;
}

public void addTest2DAO(Test2 test2) {
test2DAO.save(test2);
}

public Test2 findTest2DAO(Integer id) {
return test2DAO.findById(id);
}

public void updateTest2DAO(Test2 test2) {
test2DAO.merge(test2);
}

public void deleteTest2DAO(Test2 test2) {
test2DAO.delete(test2);
}

public List getAll(){
return test2DAO.findAll();
}
}



Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 8:55 am 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
Okay, still no sign of any Hibernate code, what's happening in the Test2DAO.save and Test2DAO.merge methods?

I suspect that the changes are not being flushed to the database.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 9:20 am 
Newbie

Joined: Sun Feb 01, 2009 6:29 pm
Posts: 15
The code of class Test2DAO

package snir.test.hibernatespring;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
* A data access object (DAO) providing persistence and search support for Test2
* entities. Transaction control of the save(), update() and delete() operations
* can directly support Spring container-managed transactions or they can be
* augmented to handle user-managed Spring transactions. Each of these methods
* provides additional information for how to configure it for the desired type
* of transaction control.
*
* @see snir.test.hibernatespring.Test2
* @author MyEclipse Persistence Tools
*/

public class Test2DAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(Test2DAO.class);
// property constants
public static final String NAME = "name";

protected void initDao() {
// do nothing

}

public void save(Test2 transientInstance) {
log.debug("saving Test2 instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

public void delete(Test2 persistentInstance) {
log.debug("deleting Test2 instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}

public Test2 findById(java.lang.Integer id) {
log.debug("getting Test2 instance with id: " + id);
try {
Test2 instance = (Test2) getHibernateTemplate().get(
"snir.test.hibernatespring.Test2", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}

public List<Test2> findByExample(Test2 instance) {
log.debug("finding Test2 instance by example");
try {
List<Test2> results = (List<Test2>) getHibernateTemplate()
.findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

public List findByProperty(String propertyName, Object value) {
log.debug("finding Test2 instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Test2 as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}

public List<Test2> findByName(Object name) {
return findByProperty(NAME, name);
}

public List findAll() {
log.debug("finding all Test2 instances");
try {
String queryString = "from Test2";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

public Test2 merge(Test2 detachedInstance) {
log.debug("merging Test2 instance");
try {
Test2 result = (Test2) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public void attachDirty(Test2 instance) {
log.debug("attaching dirty Test2 instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void attachClean(Test2 instance) {
log.debug("attaching clean Test2 instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public static Test2DAO getFromApplicationContext(ApplicationContext ctx) {
return (Test2DAO) ctx.getBean("Test2DAO");
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 10:49 am 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
I just saw Marten's response on the Spring forum - he's right.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 11:15 am 
Newbie

Joined: Sun Feb 01, 2009 6:29 pm
Posts: 15
Ok Tanks I will try to do this in PersistenceLayer.addTest2DAO()

addTest2DAO(){
Session session = HibernateSessionFactory .getSession();
Transaction tx = session.beginTransaction();
tx.begin();
session.persist(test2);

test2DAO.save(test2);
session.flush();
tx.commit();
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 6:51 pm 
Newbie

Joined: Sun Feb 01, 2009 6:29 pm
Posts: 15
I have don this:

Code:
    public void addTest2DAO(Test2 test2) {
       Session session = HibernateSessionFactory.getSession();
       Transaction tx = session.beginTransaction();
       try {                  
         test2DAO.save(test2); 
         session.persist(test2);
         session.flush();
         tx.commit();
         session.close();
      } catch (HibernateException e) {
         tx.rollback();
         e.printStackTrace();
      }
    }

But I got that:

Code:
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        .
        .
        .


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 9:28 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Hi,

Quote:
userLoadedFromDB = persistenceLayer.findTest2DAO(new Integer(11));
System.out.println("User Loaded from DB Name="+ userLoadedFromDB.getName());
/* 6. Update the user */
userLoadedFromDB.setName("Gillgoolina");
userLoadedFromDB.setId(new Integer(14));
persistenceLayer.updateTest2DAO(userLoadedFromDB);


Here you are updating the identifier of an instance. As far as I know, this is not an allowed operation.

- Litty


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 11:04 am 
Newbie

Joined: Sun Feb 01, 2009 6:29 pm
Posts: 15
Tanks I fixed it its all working now


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 11:43 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Hw did u fix it?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 08, 2009 3:12 am 
Newbie

Joined: Sun Feb 01, 2009 6:29 pm
Posts: 15
Yes thenk you


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.