Hello guys
(sorry for my grammar)
i am little new with spring(wotking with Hibernate), but no with Hibernate
i am working with this , Tomcat 5.5.9, mysql 4.1.13-standard,Hibernate3 and spring-framework-2.0-rc2-with-dependencies
in linux FC5
i am making a test with spring/hibernate and works no with the obvious results,
for example
this 2 methods in a class ADO
Code:
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class PersonaAdo extends HibernateDaoSupport {
public void insertarPersona(PersonaHibernate persona){
System.out.println("insertarPersona 0: ");
System.out.println("insertarPersona 1: "+persona.toString());
getHibernateTemplate().save(persona);
System.out.println("insertarPersona 2: "+persona.toString());
}
public void getPersona(String idCodigo){
PersonaHibernate ph = null;
ph = (PersonaHibernate) getHibernateTemplate().load(PersonaHibernate.class,idCodigo);
System.out.println("insertarPersona 0: "+ph.toString());
}
}
where PersonaHibernate is a simple pojo for Hibernate with 2 fields and toString() method overwrite
this is my db/hibernate configuration
Code:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" >
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url" >
<value>jdbc:mysql://localhost:3306/springhibernate</value>
</property>
<property name="username" >
<value>someuser</value>
</property>
<property name="password" >
<value>iamalazypassword</value>
</property>
</bean>
<!--
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/hibernate/PersonaHibernate.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- SQL dialect -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<!-- JDBC connection pool (use the built-in) -->
<prop key="hibernate.connection.pool_size" >1</prop>
<!-- Enable Hibernate's automatic session context management -->
<prop key="hibernate.current_session_context_class" >thread</prop>
<!-- Disable the second-level cache -->
<prop key="hibernate.cache.provider_class" >org.hibernate.cache.NoCacheProvider</prop>
<!-- Echo all executed SQL to stdout -->
<prop key="hibernate.show_sql" >true</prop>
<!-- Drop and re-create the database schema on startup -->
<prop key="hibernate.hbm2ddl.auto" >create-drop</prop>
<prop key="hibernate.c3p0.min_size" >1</prop>
<prop key="hibernate.c3p0.max_size" >5</prop>
<prop key="hibernate.c3p0.timeout" >300</prop>
<prop key="hibernate.c3p0.max_statements" >50</prop>
<prop key="hibernate.c3p0.idle_test_preriod" >3000</prop>
</props>
</property>
</bean>
<bean id="idPersonaH" class="com.hibernate.PersonaHibernate">
<property name="idcodigo" >
<value>001abc</value>
</property>
<property name="nombres" >
<value>Prueba</value>
</property>
</bean>
<!-- ADO -->
<bean id="idPersonaADO" class="com.hibernate.PersonaAdo" >
<property name="sessionFactory" >
<ref local="sessionFactory"/>
</property>
</bean>
and i am calling 2 methods in this way
Code:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class MyTestClass {
public MyTestClass(){
}
public void pruebas(){
try{
ApplicationContext ctx = new FileSystemXmlApplicationContext("bin/applicationContext.xml");
PersonaHibernate perh = (PersonaHibernate) ctx.getBean("idPersonaH");
PersonaAdo pado = (PersonaAdo) ctx.getBean("idPersonaADO");
pado.insertarPersona(perh);
pado.getPersona("001abc");
}
catch(Exception e){
System.out.println("EXCPETION"+e.getMessage());
System.out.println("Stack");
e.printStackTrace();
System.out.println("CLASE"+e.getClass());
}
}
public static void main(String[] args){
MyTestClass myTestClass = new MyTestClass();
myTestClass.pruebas();
}
}
if i only call pado.insertarPersona(perh); i dont have any problems (and with my jdbc tool, i can see that Hibernate make the insertion)
, but if i call the 2 methods (i want only make the insertion and get the result inserted from the db with Hibernate of course), i recieve this error message
Code:
[java] (hbm2ddl.SchemaExport 194 ) schema export complete
[java] insertarPersona 0:
[java] insertarPersona 1: PersonaHibernate:
[java] Codigo001abc
[java] nombresPrueba
[java] (xml.XmlBeanDefinitionReader 330 ) Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
[java] (support.SQLErrorCodesFactory 126 ) SQLErrorCodes loaded: [DB2, HSQL, MS-SQL, MySQL, Oracle, Informix, PostgreSQL, Sybase]
[java] Hibernate: insert into Persona (nombres, idCodigo) values (?, ?)
[java] insertarPersona 2: PersonaHibernate:
[java] Codigo001abc
[java] nombresPrueba
[java] (hibernate.LazyInitializationException 19 ) could not initialize proxy - the owning Session was closed
[java] org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
[java] at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:60)
[java] org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
[java] at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:60)
[java] at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
[java] at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:160)
[java] at com.hibernate.PersonaHibernate$$EnhancerByCGLIB$$9b769117.toString(<generated>)
[java] at com.hibernate.PersonaAdo.getPersona(Unknown Source)
[java] at com.MyTestClass.pruebas(Unknown Source)
[java] at com.MyTestClass.main(Unknown Source)
[java] at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
[java] at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:160)
[java] at com.hibernate.PersonaHibernate$$EnhancerByCGLIB$$9b769117.toString(<generated>)
[java] at com.hibernate.PersonaAdo.getPersona(Unknown Source)
[java] at com.MyTestClass.pruebas(Unknown Source)
[java] at com.MyTestClass.main(Unknown Source)
[java] EXCPETIONcould not initialize proxy - the owning Session was closed
[java] Stack
[java] CLASEclass org.hibernate.LazyInitializationException
BUILD SUCCESSFUL
Total time: 7 seconds
so please how i can fix that???
i saw in the Spring documentation
http://static.springframework.org/sprin ... -hibernate
and my spring xml configuration seems be right
Note: this database test, consist in one table, so no relationship yet, and for the future , i used to work with lazy
attribute with value true
thanks for advance guys