LazyInitializationException

Status
Nicht offen für weitere Antworten.
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:
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>
application.xml
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>
spring-mvc-servlet.xml
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>
 
M

maki

Gast
Was ist die genaue frage?

Wenn die Session endet, können die lazy proxies nicht mehr von Hibernate nachgeladen werden und man bekommt diese Exception.
 
S

Starter

Gast
klar, dass sollte aber doch eigentlich durch:

<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>

verhindert werden oder ?

Sprich die Frage ist, die ich zugegeben irgendwie vergessen habe mit zu posten:

Wieso kommt die Exception trotz des Filters ?

Habe ich irgendetwas nicht richtig konfiguriert ?

Funktioniert der OpenEntityManagerInViewFilter nicht mit "MVC" etc. ? oder ...

Zusammenfassung:

Bitte einfach mal draufgucken und schauen, ob irgendwelche schwerwiegenden Konfigprobleme auffallen oder oder ...

Danke im voraus

Gast
 

byte

Top Contributor
Versuchs mal mit einem org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.
 
S

Starter

Gast
Hab das Problem mittlerweile gefunden.

Es war ein "einfaches" Copy&Paste Problem.

Ich hatte vergessen den EntityManager aus der "spring-mvc-servlet.xml" herauszunehmen, dadurch war er zweimal vorhanden...

:meld: Manchmal sieht man den Wald vor lauter Baeumen nicht...
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
B Problem mit org.hibernate.LazyInitializationException Data Tier 11

Ähnliche Java Themen

Neue Themen


Oben