RESOLVED - issue SWF-332: Test Lazy Loading behavior from views with Hibernate and JPA conversational persistence implementation

http://opensource.atlassian.com/projects/spring/browse/SWF-332
This commit is contained in:
Ben Hale
2007-08-13 15:50:58 +00:00
parent 658e040612
commit 17bb793f8c
7 changed files with 135 additions and 9 deletions

View File

@@ -22,6 +22,7 @@ import javax.sql.DataSource;
import junit.framework.TestCase;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
@@ -228,6 +229,22 @@ public class HibernateFlowExecutionListenerTests extends TestCase {
assertSessionNotBound();
}
public void testLazyInitializedCollection() {
MockRequestContext context = new MockRequestContext();
MockFlowSession flowSession = new MockFlowSession();
flowSession.getDefinitionInternal().getAttributeMap().put("persistenceContext", "true");
listener.sessionCreated(context, flowSession);
context.setActiveSession(flowSession);
assertSessionBound();
TestBean bean = (TestBean) hibernateTemplate.get(TestBean.class, Long.valueOf(0));
assertFalse("addresses should not be initialized", Hibernate.isInitialized(bean.getAddresses()));
listener.paused(context, ViewSelection.NULL_VIEW);
assertFalse("addresses should not be initialized", Hibernate.isInitialized(bean.getAddresses()));
Hibernate.initialize(bean.getAddresses());
assertTrue("addresses should be initialized", Hibernate.isInitialized(bean.getAddresses()));
}
private DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
@@ -241,10 +258,19 @@ public class HibernateFlowExecutionListenerTests extends TestCase {
Connection connection = null;
try {
connection = dataSource.getConnection();
connection.createStatement().execute("drop table T_ADDRESS if exists;");
connection.createStatement().execute("drop table T_BEAN if exists;");
connection.createStatement().execute(
"create table T_BEAN (ID integer primary key, NAME varchar(50) not null);");
connection.createStatement().execute(
"create table T_ADDRESS (ID integer primary key, BEAN_ID integer, VALUE varchar(50) not null);");
connection
.createStatement()
.execute(
"alter table T_ADDRESS add constraint FK_BEAN_ADDRESS foreign key (BEAN_ID) references T_BEAN(ID) on delete cascade");
connection.createStatement().execute("insert into T_BEAN (ID, NAME) values (0, 'Ben Hale');");
connection.createStatement().execute(
"insert into T_ADDRESS (ID, BEAN_ID, VALUE) values (0, 0, 'Melbourne')");
} catch (SQLException e) {
throw new RuntimeException("SQL exception occurred acquiring connection", e);
} finally {
@@ -260,8 +286,9 @@ public class HibernateFlowExecutionListenerTests extends TestCase {
private SessionFactory getSessionFactory(DataSource dataSource) throws Exception {
LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setMappingLocations(new Resource[] { new ClassPathResource(
"org/springframework/webflow/support/persistence/TestBean.hbm.xml") });
factory.setMappingLocations(new Resource[] {
new ClassPathResource("org/springframework/webflow/support/persistence/TestBean.hbm.xml"),
new ClassPathResource("org/springframework/webflow/support/persistence/TestAddress.hbm.xml") });
factory.afterPropertiesSet();
return (SessionFactory) factory.getObject();
}

View File

@@ -8,6 +8,7 @@ import javax.sql.DataSource;
import junit.framework.TestCase;
import org.hibernate.Hibernate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTemplate;
@@ -108,6 +109,22 @@ public class JpaFlowExecutionListenerTests extends TestCase {
}
public void testLazilyInitalizedCollection() {
MockRequestContext context = new MockRequestContext();
MockFlowSession flowSession = new MockFlowSession();
flowSession.getDefinitionInternal().getAttributeMap().put("persistenceContext", "true");
jpaListener.sessionCreated(context, flowSession);
context.setActiveSession(flowSession);
assertSessionBound();
TestBean bean = (TestBean) jpaTemplate.getReference(TestBean.class, Long.valueOf(0));
assertFalse("addresses should not be initialized", Hibernate.isInitialized(bean.getAddresses()));
jpaListener.paused(context, ViewSelection.NULL_VIEW);
assertFalse("addresses should not be initialized", Hibernate.isInitialized(bean.getAddresses()));
Hibernate.initialize(bean.getAddresses());
assertTrue("addresses should be initialized", Hibernate.isInitialized(bean.getAddresses()));
}
private DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
@@ -121,10 +138,19 @@ public class JpaFlowExecutionListenerTests extends TestCase {
Connection connection = null;
try {
connection = dataSource.getConnection();
connection.createStatement().execute("drop table T_ADDRESS if exists;");
connection.createStatement().execute("drop table T_BEAN if exists;");
connection.createStatement().execute(
"create table T_BEAN (ID integer primary key, NAME varchar(50) not null);");
connection.createStatement().execute(
"create table T_ADDRESS (ID integer primary key, BEAN_ID integer, VALUE varchar(50) not null);");
connection
.createStatement()
.execute(
"alter table T_ADDRESS add constraint FK_BEAN_ADDRESS foreign key (BEAN_ID) references T_BEAN(ID) on delete cascade");
connection.createStatement().execute("insert into T_BEAN (ID, NAME) values (0, 'Ben Hale');");
connection.createStatement().execute(
"insert into T_ADDRESS (ID, BEAN_ID, VALUE) values (0, 0, 'Melbourne')");
} catch (SQLException e) {
throw new RuntimeException("SQL exception occurred acquiring connection", e);
} finally {

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.springframework.webflow.support.persistence" default-lazy="false">
<class name="TestAddress" table="T_ADDRESS">
<id name="entityId" column="ID">
<generator class="increment" />
</id>
<property name="value" column="VALUE" />
</class>
</hibernate-mapping>

View File

@@ -0,0 +1,25 @@
package org.springframework.webflow.support.persistence;
public class TestAddress {
private Long entityId;
private String value;
public Long getEntityId() {
return entityId;
}
public void setEntityId(Long entityId) {
this.entityId = entityId;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@@ -9,5 +9,9 @@
<generator class="increment" />
</id>
<property name="name" column="NAME" />
<set name="addresses" table="T_ADDRESS" lazy="true">
<key column="BEAN_ID"/>
<one-to-many class="TestAddress"/>
</set>
</class>
</hibernate-mapping>

View File

@@ -15,12 +15,17 @@
*/
package org.springframework.webflow.support.persistence;
import java.util.HashSet;
import java.util.Set;
public class TestBean {
private Long entityId;
private String name;
private Set addresses = new HashSet();
public TestBean() {
}
@@ -49,4 +54,12 @@ public class TestBean {
public String getName() {
return name;
}
public Set getAddresses() {
return addresses;
}
public void setAddresses(Set addresses) {
this.addresses = addresses;
}
}

View File

@@ -1,17 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
version="1.0">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
version="1.0">
<package>org.springframework.webflow.support.persistence</package>
<entity class="org.springframework.webflow.support.persistence.TestBean">
<table name="T_BEAN"/>
<entity class="TestBean">
<table name="T_BEAN" />
<attributes>
<id name="entityId">
<column name="ID"/>
<column name="ID" />
</id>
<basic name="name">
<column name="NAME"/>
<column name="NAME" />
</basic>
<one-to-many name="addresses" target-entity="TestAddress">
<join-column name="BEAN_ID"/>
</one-to-many>
</attributes>
</entity>
<entity class="TestAddress">
<table name="T_ADDRESS"/>
<attributes>
<id name="entityId">
<column name="ID" />
</id>
<basic name="value">
<column name="VALUE" />
</basic>
</attributes>
</entity>