K
Konstantin Denerz
Hallo,
ich bekomme folgende Exception beim Laden eines Objektes mit Hibernate(Spring):
Ich benutze Spring und Hibernate.
Ich habe ein Objekt person, das ein Attribut contacts vom Typ Set<Contact> hat.
Dieses person Objekt will ich nun speichern und dann wieder laden. Speichern funktioniert problemlos. Wenn ich dann aber die Kontakte der Person laden will(Lazy Loading) krieg ich die Exception.
Das person-Objekt:
Mapping zu Person:
Das contact Objekt:
Mapping des Contacts:
Hier die Serviceklassen:
Und die Spring Konfiguration:
Die Main-Methode:
Hat jemand eine Idee warum das Lazy Loading nicht funktioniert?
Gruß Konstantin
ich bekomme folgende Exception beim Laden eines Objektes mit Hibernate(Spring):
Java:
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: de.tutorials.training.springLazyLoading.domain.Person.contacts, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163)
at de.tutorials.training.springLazyLoading.domain.Main.main(Main.java:28)
Ich benutze Spring und Hibernate.
Ich habe ein Objekt person, das ein Attribut contacts vom Typ Set<Contact> hat.
Dieses person Objekt will ich nun speichern und dann wieder laden. Speichern funktioniert problemlos. Wenn ich dann aber die Kontakte der Person laden will(Lazy Loading) krieg ich die Exception.
Das person-Objekt:
Java:
package de.tutorials.training.springLazyLoading.domain;
import java.util.Set;
public class Person {
private long id;
private String firstName;
private String lastName;
private Set<Contact> contacts;
public Set<Contact> getContacts() {
return contacts;
}
public void setContacts(Set<Contact> contacts) {
this.contacts = contacts;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public long getId() {
return id;
}
private void setId(long id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
XML:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="de.tutorials.training.springLazyLoading.domain">
<class name="Person">
<id name="id" unsaved-value="0"><generator class="native"></generator> </id>
<property name="firstName" type="string"></property>
<property name="lastName" type="string"></property>
<set name="contacts" cascade="all" lazy="true">
<key column="person_id"></key>
<one-to-many class="Contact"/>
</set>
</class>
</hibernate-mapping>
Java:
public class Contact {
public Contact(String type, String value) {
super();
this.type = type;
this.value = value;
}
public Contact() {
super();
// TODO Auto-generated constructor stub
}
private long id;
private String type;
private String value;
public long getId() {
return id;
}
private void setId(long id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Mapping des Contacts:
XML:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="de.tutorials.training.springLazyLoading.domain">
<class name="Contact">
<id name="id" unsaved-value="0">
<generator class="native"></generator>
</id>
<property name="type" type="string"></property>
<property name="value" type="string"></property>
</class>
</hibernate-mapping>
Hier die Serviceklassen:
Java:
//PersonAdministrationService
package de.tutorials.training.springLazyLoading.service;
import de.tutorials.training.springLazyLoading.domain.Person;
import de.tutorials.training.springLazyLoading.service.dao.IPersonDao;
public class PersonAdministrationService {
private IPersonDao personDao;
public IPersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(IPersonDao personDao) {
this.personDao = personDao;
}
public void savePerson(Person person){
this.getPersonDao().makePersistent(person);
}
public Person getPersonById(long id){
return this.getPersonDao().getPersonById(id);
}
}
// IPersonDao
package de.tutorials.training.springLazyLoading.service.dao;
import de.tutorials.training.springLazyLoading.domain.Person;
public interface IPersonDao {
void makePersistent(Person person);
Person getPersonById(long id);
}
//PersonDaoHibernateImpl
package de.tutorials.training.springLazyLoading.service.dao.internal;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import de.tutorials.training.springLazyLoading.domain.Person;
import de.tutorials.training.springLazyLoading.service.dao.IPersonDao;
public class PersonDaoHibernateImpl extends HibernateDaoSupport implements IPersonDao {
public Person getPersonById(long id) {
return (Person) this.getHibernateTemplate().get(Person.class, id);
}
public void makePersistent(Person person) {
this.getHibernateTemplate().save(person);
}
}
Und die Spring Konfiguration:
XML:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean name="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="conf/jdbcOracle.properties"/>
</bean>
<bean name="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean name="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"></property>
<!-- O/R-Mappings -->
<property name="mappingResources">
<list>
<value>de/tutorials/training/springLazyLoading/domain/Contact.hbm.xml</value>
<value>de/tutorials/training/springLazyLoading/domain/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.hbm2ddl.auto" >create-drop</prop>
</props>
</property>
</bean>
<bean name="personDao" class="de.tutorials.training.springLazyLoading.service.dao.internal.PersonDaoHibernateImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean name="myPersonAdministrationServiceTarget" class="de.tutorials.training.springLazyLoading.service.PersonAdministrationService">
<property name="personDao" ref="personDao"></property>
</bean>
<bean name="myTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean id="myPersonAdministrationService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="myTransactionManager"/>
<property name="target" ref="myPersonAdministrationServiceTarget" />
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
Die Main-Methode:
Java:
package de.tutorials.training.springLazyLoading.domain;
import java.util.HashSet;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import de.tutorials.training.springLazyLoading.service.PersonAdministrationService;
public class Main {
public static void main(String[] args){
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("conf/applicationContext.xml");
PersonAdministrationService personAdministrationService= (PersonAdministrationService) applicationContext.getBean("myPersonAdministrationService");
Person person =new Person();
person.setFirstName("Konstantin");
person.setLastName("Denerz");
Set<Contact> contacs = new HashSet<Contact>();
contacs.add(new Contact("email_privat","kde@tutorials.de"));
person.setContacts(contacs);
personAdministrationService.savePerson(person);
System.out.println(personAdministrationService.getPersonById(1).getFirstName());
System.out.println(personAdministrationService.getPersonById(1).getContacts().iterator().next().getValue());
}
}
Gruß Konstantin
Zuletzt bearbeitet von einem Moderator: