Hello Guys!
I´m having the tradicional problem of LazyLoadingException in my Swing application.
Let me explain how I´m working.
I have a DAO interface, called GenericDAO, and one implementation in Hibernate, called Hibernate3BasedDAO. In my Spring configuration, I have this:
Code:
<bean id="genericDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<bean class="com.mypackage.genericdao.impl.Hibernate3BasedDAO">
<property name="sessionFactory">
<ref bean="xtoSessionFactory"/>
</property>
</bean>
</property>
<property name="proxyInterfaces">
<value>com.mypackage.genericdao.GenericDao</value>
</property>
</bean>
So, when I did in my code
springFactory.getBean("genericDAO"), I get one instance of Hibernate3BasedDAO.
Then, I have one interface of service, called AttributeService, and his implementatiom AttributeServiceImpl. In my Spring configuration, I have this:
Code:
<bean id="attributeService"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<bean class="com.mypackage.xto.attributebuilder.business.impl.AttributeServiceImpl">
<property name="dao">
<ref bean="genericDAO"/>
</property>
</bean>
</property>
<property name="proxyInterfaces">
<value>com.mypackage.xto.attributebuilder.business.AttributeService</value>
</property>
</bean>
So, when I did in my code
springFactory.getBean("attributeService"), I get one instance of AttributeServiceImpl, with an instance of HibernateBased3DAO.
In the AttributeService interface, I have a method called findAllAttributes, in the implementation, I do the follow:
Code:
public List<Attribute> fildAllAttributes() {
List<Attribute> result;
result = dao.findByQuery("findAllAttributesQuery", Attribute.class);
return result;
}
What happened in this method? I just get all Attributes from my database.
Ok... for now, we are ok... we did some Unit tests and everthing works fine.
Then... we started to do our Swing screens, and we want a screen that list all Attributes in a JTable, so, we have something like this:
Code:
public class ListAttributesFrame extends JFrame {
public void ListAttributesFrame() {
// .. Swing initialization stuff
createGui();
}
private void createGui() {
AttributeService service;
List<Attribute> attributes;
TableModel tableModel;
JTable table;
// Get the attributeService
service = springFactory.getBean("attributeService");
// get all attributes
attributes = service.findAllAttributes();
// create the tableModel
tableModel = new AttributeTableModel(attributes); // this is my customized table model to show the attributes
// create the table and put in the container
JTable table = new JTable(tableModel);
this.getContentPane().add(table);
}
}
When we run this class, what happened? Wow!! He have a list of all attributes in a table! Great!
Now I want to show in a dialog, some properties of my attributes, so, we did this:
Code:
public class ListAttributesFrame extends JFrame {
public void ListAttributesFrame() {
// .. Swing initialization stuff
createGui();
}
private void createGui() {
AttributeService service;
List<Attribute> attributes;
final TableModel tableModel;
final JTable table;
// Get the attributeService
service = springFactory.getBean("attributeService");
// get all attributes
attributes = service.findAllAttributes();
// create the tableModel
tableModel = new AttributeTableModel(attributes); // this is my customized table model to show the attributes
// create the table and put in the container
table = new JTable(tableModel);
this.getContentPane().add(table);
// Button to show the attribute properties
JButton button = new JButton("Show Details");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Attribute attribute;
int row = table.getSelectedRow();
if (row > -1) {
attribute = tableModel.getAttribute(row);
JOptionPane.showMessageDialog(null, "The attribute arguments size is: "+attribute.getArguments().size());
}
}
});
this.getContentPane().add(button);
}
}
Well... looking the code, we guess that we are Ok, right?
Wrong!
When I hit in the button "Show Details", I have the famous
LazyLoadException, because the session has been closed in the attribute service, because the session just stay open when the transaction is open, when the transaction closes, the session are closed too.
Now, my question:
Which is the best way to handle this Error in my application? Any ideas?
PS: I´m using Hibernate 3.0.5, Spring 1.2.2 and Java 1.5
Thanks in advanced!
Paulo Sigrist