G
Gast
Gast
Hallo,
bekomme beim Aufbau einer JSP eine LazyInitializationException.
Genauer getFirstName bei CustomerImpl (siehe unten)
Umgebung:
Spring 2.5.3 (MVC, ORM, etc.)
JPA mit Hibernate
Tomcat 6.14
Allgemeine Anmerkung:
Mir ist bewusst das normalerweise eine Serviceschicht etc. benutzt wird... Aber es sollte ein einfaches Bsp. sein...
Der Controller der Seite:
Die CustomerEntity:
web.xml
application.xml
spring-mvc-servlet.xml
bekomme beim Aufbau einer JSP eine LazyInitializationException.
Genauer getFirstName bei CustomerImpl (siehe unten)
Umgebung:
Spring 2.5.3 (MVC, ORM, etc.)
JPA mit Hibernate
Tomcat 6.14
Allgemeine Anmerkung:
Mir ist bewusst das normalerweise eine Serviceschicht etc. benutzt wird... Aber es sollte ein einfaches Bsp. sein...
Der Controller der Seite:
Code:
@Controller
@RequestMapping( "/simple.form" )
@Transactional
public class CustomerEditorController
{
@PersistenceContext
private EntityManager _entityManager;
@ModelAttribute( "customer" )
public CustomerImpl initView( @RequestParam( "customerId" )String customerId )
{
CustomerImpl customer;
if( customerId != null && !customerId.isEmpty() )
{
customer = _entityManager.find( CustomerImpl.class, Long.valueOf( customerId ) );
}
else
{
customer = new CustomerImpl();
}
return customer;
}
@RequestMapping( method = RequestMethod.GET )
public String setupForm()
{
return "CustomerEditor";
}
@RequestMapping( method = RequestMethod.POST )
public String processSubmit( @ModelAttribute( "customer" )CustomerImpl customer )
{
return "testResult";
}
}
Die CustomerEntity:
Code:
@Entity
@Table( name = "Customer", schema = "businessplan" )
public class CustomerImpl
{
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "primaryKeySequence" )
@SequenceGenerator( name = "primaryKeySequence", sequenceName = "primaryKey_SEQ", allocationSize = 100 )
@Column( name = "Customer_PK", insertable = false, updatable = false )
private long _primaryKey;
@Column( name = "InsertDate" )
@Temporal( value = TemporalType.TIMESTAMP )
private Date _InsertDate;
@Column( name = "UpdateDate" )
@Temporal( value = TemporalType.TIMESTAMP )
private Date _UpdateDate;
@Column( name = "Version" )
@Version
private Integer _Version = 1;
@PrePersist
private void __jpaPrePersist()
{
_InsertDate = new Date();
_UpdateDate = new Date();
}
@PreUpdate
private void __jpaPreUpdate()
{
_UpdateDate = new Date();
}
@OneToMany( mappedBy = "_customer", targetEntity = AddressImpl.class, fetch = FetchType.LAZY )
private Set<AddressImpl> _addresses;
@Column( name = "firstname", length = 256 )
private String _firstName;
@Column( name = "lastname", length = 256 )
private String _lastName;
public CustomerImpl()
{
_addresses = new HashSet<AddressImpl>();
}
public void addAddress( AddressImpl address )
{
_addresses.add( address );
address.setCustomer( this );
}
public String getFirstName()
{
_addresses.size(); // Test ob OEMIVF Funktioniert !!! --> Es funktioniert nicht !!!
return _firstName;
}
public void setFirstName( String firstName )
{
_firstName = firstName;
}
public String getLastName()
{
return _lastName;
}
public void setLastName( String lastName )
{
_lastName = lastName;
}
}
web.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application.xml</param-value>
</context-param>
<filter>
<filter-name>sessionFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
[url]http://www.springframework.org/schema/beans[/url]
[url]http://www.springframework.org/schema/beans/spring-beans-2.5.xsd[/url]
[url]http://www.springframework.org/schema/context[/url]
[url]http://www.springframework.org/schema/context/spring-context-2.5.xsd[/url]
[url]http://www.springframework.org/schema/tx[/url]
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="com" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<tx:annotation-driven/>
<context:property-placeholder location="classpath:datasource.properties"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource"/>
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="businessplan"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${dataSource.driverClassName}" />
<property name="url" value="${dataSource.url}" />
<property name="username" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
</bean>
</beans>
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
[url]http://www.springframework.org/schema/beans[/url]
[url]http://www.springframework.org/schema/beans/spring-beans-2.5.xsd[/url]
[url]http://www.springframework.org/schema/context[/url]
[url]http://www.springframework.org/schema/context/spring-context-2.5.xsd[/url]
[url]http://www.springframework.org/schema/tx[/url]
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="com" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:annotation-config/>
<context:property-placeholder location="classpath:datasource.properties"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="businessplan"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${dataSource.driverClassName}" />
<property name="url" value="${dataSource.url}" />
<property name="username" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource"/>
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>