Hi,
I'm using HIbernate 3 with Spring... my Spring's XML is it:
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>hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="eventoDAO" class="br.gov.pa.tj.evento.dao.EventoDAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="eventoFacade" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="eventoFacadeTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="inserir*">PROPAGATION_REQUIRED</prop>
<prop key="alterar*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="eventoFacadeTarget" class="br.gov.pa.tj.evento.facade.EventoFacadeImpl">
<property name="eventoDAO">
<ref local="eventoDAO"/>
</property>
</bean>
</beans>
With this, my method insert functions:
Code:
public class EventoFacadeImplTest extends TestCase {
private ApplicationContext ctx;
private EventoFacade efacade;
private Evento evento;
protected void setUp() throws Exception {
String[] paths = { "spring-hibernate.xml" };
ctx = new ClassPathXmlApplicationContext(paths);
efacade = (EventoFacade) ctx.getBean("eventoFacade");
}
protected void tearDown() throws Exception {
evento = null;
efacade = null;
}
public void testInserir() throws Exception{
evento = new Evento();
evento.setComponente("componente");
evento.setDescricao("descricao");
evento.setIcone("icone");
evento = (Evento) efacade.inserir(evento);
assertTrue(evento.getId()!=null);
}
But in my method update occur an exception LazyInitializationException:
Method update:
Code:
public void testUpdate() throws Exception {
evento = (Evento) efacade.selecionar(new Integer(4));
evento.setComponente("novo componente");
evento = (Evento) efacade.alterar(evento);
}
Exception:
Code:
09/08/2005 10:32:06 org.hibernate.LazyInitializationException <init>
SEVERE: could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at br.gov.pa.tj.evento.classe.Evento$$EnhancerByCGLIB$$a0abedcd.setComponente(<generated>)
at br.gov.pa.tj.evento.testes.EventoFacadeImplTest.testUpdate(EventoFacadeImplTest.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
My Bean:
Code:
package br.gov.pa.tj.evento.classe;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
/**
* @hibernate.class
* table = "ARREVENTO"
* */
public class Evento implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String descricao;
private String componente;
private String icone;
/**
* @hibernate.property
* */
public String getComponente() {
return componente;
}
/**
* @hibernate.property
* column = "DEEVENTO"
* */
public String getDescricao() {
return descricao;
}
/**
* @hibernate.property
* */
public String getIcone() {
return icone;
}
/**
* @hibernate.id
* generator-class = "native"
* column = "CDEVENTO"
* */
public Integer getId() {
return id;
}
public void setComponente(String componente) {
this.componente = componente;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public void setIcone(String icone) {
this.icone = icone;
}
public void setId(Integer id) {
this.id = id;
}
public boolean equals(Object o) {
if (!(o instanceof Evento)) {
return false;
}
Evento rhs = (Evento) o;
return new EqualsBuilder().appendSuper(super.equals(o)).append(id,
rhs.id).isEquals();
}
}
Someone can help me??