-->
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.  [ 6 posts ] 
Author Message
 Post subject: Data Insert/Update not persist
PostPosted: Mon Feb 02, 2009 4:20 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.
I used the code from MyEclipse:
PersistenceLayer persistenceLayer = (PersistenceLayer) beanFactory
.getBean("persistenceLayer");
persistenceLayer.addUser(user);

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 4:29 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Please provide code of addUser() and your hibernate-configuration.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 2:11 pm 
Newbie

Joined: Sun Feb 01, 2009 6:29 pm
Posts: 15
Test2 userLoadedFromDB;
PersistenceLayer persistenceLayer = (PersistenceLayer) beanFactory
.getBean("PersistenceLayer");

Test2 test2 = new Test2();
test2.setId(new Integer(33));
test2.setName("Adi");
/* 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());
/* Update the user */
userLoadedFromDB.setName("Gill");
userLoadedFromDB.setId(new Integer(14));
persistenceLayer.updateTest2DAO(userLoadedFromDB);


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- 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">titi</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="pool_size">12</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>

</hibernate-configuration>

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/b ... ns-2.5.xsd">


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"
value="file:src/hibernate.cfg.xml">
</property>
<property name="annotatedClasses">
<list>
<value>snir.test.hibernatespring.Test2</value></list>
</property></bean>
<bean id="Test2DAO" class="snir.test.hibernatespring.Test2DAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="PersistenceLayer"
class="snir.test.hibernatespring.PersistenceLayer" abstract="false"
lazy-init="default" autowire="default" dependency-check="default">
<property name="test2DAO">
<ref bean="Test2DAO" />
</property>
</bean></beans>
[/u]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2009 3:51 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
hmm, I can't see anything wrong. Try to select all users using a HQL-Query. Do you get results?

The only thing that seems weird to me is, that you have "hbm2ddl.auto" twice in your config. remove one of these lines.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2009 5:46 am 
Newbie

Joined: Sun Feb 01, 2009 6:29 pm
Posts: 15
Hi
When I do select all I got it all but the database hase no change.
and I so that it add a user with incremental id (not the id that I gave it).
There is somthing thet I need to change in the mySql configuratio?

the twice "hbm2ddl.auto" its becuase I didn't noetic that its there.

This is the completely code:

package snir.test.hibernatespring;

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: Re: Data Insert/Update not persist
PostPosted: Wed Nov 25, 2009 5:58 am 
Newbie

Joined: Wed Nov 25, 2009 5:34 am
Posts: 4
Hi there,

I've had the same problem with you when I tried to save data in Hibernate by calling the save function in the DAO class.
Then, I've studied and I've found that every transient variable will not be committed automatically. That's the reason!

So we need to commit our codes manually.

Take a look at this:

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class UpdateExample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Session sess = null;
try {
SessionFactory fact = new Configuration()
.configure().buildSessionFactory();
sess = fact.openSession();
Transaction tr = sess.beginTransaction();
StudentClass student = new StudentClass();
student.setName("viet");
student.setAge("22");
sess.update(student);
tr.commit();
sess.close();
System.out.println("Update successfully!");
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}

After I've included transaction at the begin and the end of the code.
My program run well.

I think your problem is the same here! Try it.


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