Hi!
I've created my first very simple desktop application using Spring and Hibernate. I think that everything I've done well but this application throws a LazyLoadException :(
Hibernate version:
Hibernate 3.0.5
applicationContext.xml
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="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/uk</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>/net/universekingdom/Ship.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<bean id="hibernateTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"></ref></property>
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager"><ref local="hibernateTxManager"/></property>
</bean>
<bean id="shipDao" class="net.universekingdom.ShipDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory"></ref>
</property>
</bean>
<bean id="shipsListService" class="net.universekingdom.ShipsListServiceImpl">
<constructor-arg type="net.universekingdom.ShipDAO">
<ref local="shipDao" />
</constructor-arg>
<constructor-arg type="org.springframework.transaction.support.TransactionTemplate">
<ref local="transactionTemplate" />
</constructor-arg>
</bean>
</beans>
Ship.hbm.xmlCode:
<hibernate-mapping
>
<class
name="net.universekingdom.Ship"
table="SHIPS"
>
<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="native">
</generator>
</id>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="name"
length="64"
not-null="true"
/>
</class>
</hibernate-mapping>
Ship.javaCode:
package net.universekingdom;
/**
* @hibernate.class table = "SHIPS"
*
*/
public class Ship {
public Ship() {
name = "";
}
private String name;
/**
* @hibernate.property column = "name" length = "64" not-null = "true"
* @return
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private Long id;
/**
* @hibernate.id column = "id" generator-class = "native"
* @return
*/
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
ShipListServiceImpl.javaCode:
package net.universekingdom;
import java.util.List;
import java.util.Vector;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
public class ShipsListServiceImpl extends ShipsListService {
private TransactionTemplate txTemplate;
public ShipsListServiceImpl(ShipDAO shipDao, TransactionTemplate txTemplate) {
super(shipDao);
this.txTemplate = txTemplate;
}
public List getEnemyShips() {
List l = (List) txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
Ship s = new Ship();
s.setName("Jacek");
shipDAO.insert(s);
Ship ship = shipDAO.find("Jacek");
List w = new Vector();
w.add(ship);
return w;
}
});
return l;
}
}
Code which is executed in main methodCode:
InputStreamResource resource = new InputStreamResource(MyTest.class
.getResourceAsStream("applicationContext.xml"));
BeanFactory beanFactory = new XmlBeanFactory(resource);
ShipsListService b = (ShipsListService) beanFactory
.getBean("shipsListService");
System.out.println(b.getEnemyShips().get(0));
When I run this code I get the following exception:
Full stack trace of any exception that occurs:Code:
Exception in thread "main" 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:80)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:133)
at net.universekingdom.Ship$$EnhancerByCGLIB$$e195f361.toString(<generated>)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at MyTest.main(MyTest.java:14)
What I'm doing wrong? What is incorrect?