Hibernate & Spring Konfiguration/Struktur LazyInitializationException

christoph123

Grünschnabel
Hallo,

Ich arbeite derzeit an meinem Maturaprojekt. Es handelt sich um eine Hotelsoftware, welche mit dem Springframework und Hibernate arbeitet. Leider bekomme ich beim Laden einer lazy-Beziehung die LazyInitializationException.

Will ich zum Beispiel auf die Nationaltität eines Gasts zugreifen erhalte ich folgenden Fehler:
Code:
Client: d518b4aa53374c08:7bd64b0e:120422da103:-7fff is ready
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
	at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
	at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
	at at.openhotel.model.object.Nationality_$$_javassist_8.getName(Nationality_$$_javassist_8.java)

Die Objekte sehen so aus:

Guest
Code:
package at.openhotel.model.object;

// Generated 10.01.2009 14:15:38 by Hibernate Tools 3.2.4.CR1

import static javax.persistence.GenerationType.IDENTITY;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * this class represents a guest, it includes personal information referring to
 * the guest as well as the list of newsletters the guest got, if he is a regular
 * or on the black list and if he gets any price reduce.
 */
@Entity
@Table(name = "guest")
public class Guest implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	private Integer id;
	private Nationality nationality;
	private Zip zip;
	private String firstName;
	private String lastName;
	private String gender;
	private Date dateOfBirth;
	private String job;
	private String address;
	private boolean blacklist;
	private String email;
	private String phone;
	private String title;
	private boolean newsletter;
	private String comment;
	private Double priceReduction;
	private boolean isRegular;
	private String salutation;
	private boolean vegetarian;
	private List<Newsletter> newsletters = new ArrayList<Newsletter>(0);

	private List<Affinity> affinities = new ArrayList<Affinity>(0);
	private List<ContactPerson> contactpersons = new ArrayList<ContactPerson>(0);
	private List<Reservation> reservations = new ArrayList<Reservation>(0);
	
	/**
     * default constructor
     */
	public Guest() {
	}

    /**
     * not preferred constructor
     * @param nationality
     * @param zip
     * @param address
     */
	public Guest(Nationality nationality, Zip zip, String address) {
		this.nationality = nationality;
		this.zip = zip;
		this.address = address;
	}

    /**
     * preferred very tiny constructor
     * @param firstName first name
     * @param lastName last name
     * @param gender gender ("m"/"f")
     * @param email email address
     * @param title title
     * @param newsletter if the guest wants to get a newsletter from the hotel
     * @param comment space for comments
     */
    public Guest(String firstName, String lastName, String gender, String email, String title, boolean newsletter, String comment) {
        this.zip = zip;
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
        this.email = email;
        this.title = title;
        this.newsletter = newsletter;
        this.comment = comment;
    }

    /**
     * extremly detailed constructor
     * @param nationality nationality of the guest
     * @param zip zip code of the guests domicile
     * @param firstName first name
     * @param lastName last name
     * @param gender gender either m or f
     * @param dateOfBirth date of birth to determine the age
     * @param job job
     * @param address address
     * @param blacklist if the guest is welcomed or not
     * @param email email address
     * @param phone phone number
     * @param title title
     * @param newsletter if the guest wants to get a newsletter
     * @param comment space for comments
     * @param priceReduction amount of price reduction for the guest (optional, e.g. for regulars)
     * @param isRegular boolean if regular or not
     * @param salutation maybe important for sending letters.
     */
    public Guest(String firstName, String lastName, String gender, Date dateOfBirth, String job, String address, Nationality nationality, Zip zip, boolean blacklist, String email, String phone, String title, boolean newsletter, String comment, Double priceReduction, boolean isRegular, String salutation) {
        this.nationality = nationality;
        this.zip = zip;
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
        this.dateOfBirth = dateOfBirth;
        this.job = job;
        this.address = address;
        this.blacklist = blacklist;
        this.email = email;
        this.phone = phone;
        this.title = title;
        this.newsletter = newsletter;
        this.comment = comment;
        this.priceReduction = priceReduction;
        this.isRegular = isRegular;
        this.salutation = salutation;
    }

    /**
     * detailed preferred constructor
     * @param nationality nationality of the guest
     * @param zip zip code of the guests domicile
     * @param firstName first name
     * @param lastName last name
     * @param gender gender either "m" or "f"
     * @param address address-field e.g. Mainstreet 12/34
     * @param blacklist if the guest is welcomed or not
     * @param email email address
     * @param phone phone number as a String
     * @param title title of the guest
     * @param newsletter if the guest wants to get a newsletter
     * @param comment space for comments
     * @param priceReduction price reduction (optionally, e.g. for regulars)
     * @param isRegular if the guest is a regular
     * @param salutation salutation e.g. Mr., Ms.,...
     */
    public Guest(String firstName, String lastName, String gender, String address, Nationality nationality, Zip zip, boolean blacklist, String email, String phone, String title, boolean newsletter, String comment, Double priceReduction, boolean isRegular, String salutation) {
        this.nationality = nationality;
        this.zip = zip;
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
        this.address = address;
        this.blacklist = blacklist;
        this.email = email;
        this.phone = phone;
        this.title = title;
        this.newsletter = newsletter;
        this.comment = comment;
        this.priceReduction = priceReduction;
        this.isRegular = isRegular;
        this.salutation = salutation;
    }

    /**
     * to be used by Java Hibernate
     * @param nationality
     * @param zip
     * @param firstName
     * @param lastName
     * @param gender
     * @param dateOfBirth
     * @param job
     * @param address
     * @param blacklist
     * @param email
     * @param phone
     * @param title
     * @param newsletter
     * @param comment
     * @param priceReduction
     * @param isRegular
     * @param salutation
     * @param newsletters
     * @param affinities
     * @param contactpersons
     * @param reservations
     */
	public Guest(Nationality nationality, Zip zip, String firstName,
			String lastName, String gender, Date dateOfBirth, String job,
			String address, boolean blacklist, String email, String phone,
			String title, boolean newsletter, String comment,
			Double priceReduction, boolean isRegular, String salutation,
			boolean vegetarian,
			List<Newsletter> newsletters,
			List<Affinity> affinities, List<ContactPerson> contactpersons,
			List<Reservation> reservations) {
		this.nationality = nationality;
		this.zip = zip;
		this.firstName = firstName;
		this.lastName = lastName;
		this.gender = gender;
		this.dateOfBirth = dateOfBirth;
		this.job = job;
		this.address = address;
		this.blacklist = blacklist;
		this.email = email;
		this.phone = phone;
		this.title = title;
		this.newsletter = newsletter;
		this.comment = comment;
		this.priceReduction = priceReduction;
		this.isRegular = isRegular;
		this.salutation = salutation;
		this.newsletters = newsletters;
		this.vegetarian = vegetarian;
		this.affinities = affinities;
		this.contactpersons = contactpersons;
		this.reservations = reservations;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "nationalityID", nullable = true)
	public Nationality getNationality() {
		return this.nationality;
	}

	public void setNationality(Nationality nationality) {
		this.nationality = nationality;
	}

    /*public String getNationalityName() {
        if (this.nationality == null)
            return "?";
        return this.nationality.getName();
    }*/

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "zipID", nullable = true)
	public Zip getZip() {
		return this.zip;
	}

	public void setZip(Zip zip) {
		this.zip = zip;
	}

	@Column(name = "firstName", length = 30)
	public String getFirstName() {
		return this.firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	@Column(name = "lastName", length = 30)
	public String getLastName() {
		return this.lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Column(name = "gender", length = 1)
	public String getGender() {
		return this.gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "dateOfBirth", length = 10)
	public Date getDateOfBirth() {
		return this.dateOfBirth;
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	@Column(name = "job", length = 30)
	public String getJob() {
		return this.job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	@Column(name = "address", nullable = true, length = 65535)
	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Column(name = "blacklist")
	public boolean getBlacklist() {
		return this.blacklist;
	}

	public void setBlacklist(boolean blacklist) {
		this.blacklist = blacklist;
	}

	@Column(name = "email", length = 200)
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name = "phone", length = 30)
	public String getPhone() {
		return this.phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Column(name = "title", length = 30)
	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name = "newsletter")
	public boolean getNewsletter() {
		return this.newsletter;
	}

	public void setNewsletter(boolean newsletter) {
		this.newsletter = newsletter;
	}

	@Column(name = "comment", length = 65535)
	public String getComment() {
		return this.comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	@Column(name = "priceReduction", precision = 22, scale = 0)
	public Double getPriceReduction() {
		return this.priceReduction;
	}

	public void setPriceReduction(Double priceReduction) {
		this.priceReduction = priceReduction;
	}

	@Column(name = "isRegular")
	public boolean getIsRegular() {
		return this.isRegular;
	}

	public void setIsRegular(boolean isRegular) {
		this.isRegular = isRegular;
	}

	@Column(name = "salutation", length = 50)
	public String getSalutation() {
		return this.salutation;
	}

	public void setSalutation(String salutation) {
		this.salutation = salutation;
	}
	
	@Column(name = "vegetarian")
	public boolean getVegetarian() {
		return this.vegetarian;
	}

	public void setVegetarian(boolean vegetarian) {
		this.vegetarian = vegetarian;
	}
	

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinTable(name = "newsletter_has_guest", joinColumns = { @JoinColumn(name = "guestID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "newsletterID", nullable = false, updatable = false) })
	public List<Newsletter> getNewsletters() {
		return this.newsletters;
	}

	public void setNewsletters(List<Newsletter> newsletters) {
		this.newsletters = newsletters;
	}

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinTable(name = "guest_has_affinity", joinColumns = { @JoinColumn(name = "guestID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "affinityID", nullable = false, updatable = false) })
	public List<Affinity> getAffinities() {
		return this.affinities;
	}

	public void setAffinities(List<Affinity> affinities) {
		this.affinities = affinities;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "guest")
	public List<ContactPerson> getContactpersons() {
		return this.contactpersons;
	}

	public void setContactpersons(List<ContactPerson> contactpersons) {
		this.contactpersons = contactpersons;
	}
	
	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinTable(name = "reservation_has_guest", joinColumns = { @JoinColumn(name = "guestID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "reservationID", nullable = false, updatable = false) })
	public List<Reservation> getReservations() {
		return this.reservations;
	}

	public void setReservations(List<Reservation> reservations) {
		this.reservations = reservations;
	}
}

Nationality
Code:
package at.openhotel.model.object;

// Generated 10.01.2009 14:15:38 by Hibernate Tools 3.2.4.CR1

import static javax.persistence.GenerationType.IDENTITY;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * this class represents the nationaltity of guests, e.g. some guy from Austria=Austrian
 */
@Entity
@Table(name = "nationality")
public class Nationality implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	private Integer id;
	private Country country;
	private String name;
	private List<Guest> guests = new ArrayList<Guest>(0);

	public Nationality() {
	}

	public Nationality(Country country) {
		this.country = country;
	}

    /**
     * preferred constructor
     * @param country country the nationality is referring to.
     * @param name name of the nationality
     */
    public Nationality(Country country, String name) {
        this.country = country;
        this.name = name;
    }

    /**
     * to be used by Java Hibernate
     * @param country
     * @param name
     * @param guests
     */
	public Nationality(Country country, String name, List<Guest> guests) {
		this.country = country;
		this.name = name;
		this.guests = guests;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "countryID", nullable = false)
	public Country getCountry() {
		return this.country;
	}

	public void setCountry(Country country) {
		this.country = country;
	}

	@Column(name = "name", length = 30)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "nationality")
	public List<Guest> getGuests() {
		return this.guests;
	}

	public void setGuests(List<Guest> guests) {
		this.guests = guests;
	}
}

Dazu habe ich DAO Klassen, die mein GenericHibernateDAO implementieren.
GenericHibernateDAO
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package at.openhotel.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * @author Christoph
 */
public class GenericHibernateDAO<T, ID extends Serializable>  extends HibernateDaoSupport
implements GenericDAO <T, ID> 
{
    private Class <T> cEntityClass = null;

    @SuppressWarnings("unchecked")
	public GenericHibernateDAO ( )
    {
        this.cEntityClass = ( Class <T> )( ( ParameterizedType ) this.getClass ( ).getGenericSuperclass ( ) ).getActualTypeArguments ( )[0];
    }
    
    /**
     * ensures that the session is kept for lazy laoding
     */
    public void keepSessionOpen ( boolean keepSessionOpen ) {
    	if(keepSessionOpen) {
	    	getHibernateTemplate().setAllowCreate(false);
	    	SessionFactoryUtils.initDeferredClose(getHibernateTemplate().getSessionFactory());
    	}
    }
    @Override
    public List<T> loadAll ( boolean keepSessionOpen ) 
    {
    	keepSessionOpen(keepSessionOpen);
    	return getHibernateTemplate().loadAll(this.cEntityClass);
    }
    
	@Override
	public T loadByID(ID id, boolean keepSessionOpen) {
		keepSessionOpen(keepSessionOpen);
		return (T) getHibernateTemplate().get(this.cEntityClass, id);
	}

	@Override
	public void persist(T entity) {
		getHibernateTemplate().save(entity);
//		getHibernateTemplate().flush();
	}

	@Override
	public void update(T entity) {
		getHibernateTemplate().merge(entity);
		getHibernateTemplate().flush();
	}
	
	@Override
	public void remove(T entity) {
		getHibernateTemplate().delete(entity);
		getHibernateTemplate().flush();
	}

    @SuppressWarnings("unchecked")
	public List<T> findByCriteria (boolean keepSessionOpen, Criterion... cCriteriaList ) 
    {
    	keepSessionOpen(keepSessionOpen);
        Criteria cCritObj =  getHibernateSession().createCriteria ( this.cEntityClass );
        
        for ( Criterion c : cCriteriaList ) {
            cCritObj.add ( c );
        }
        
        List<T> lReturnValue = cCritObj.list ( );
        return lReturnValue;
    }
    
    @SuppressWarnings("unchecked")
	public List<T> findByDetachedCriteria ( DetachedCriteria detached, boolean keepSessionOpen ) 
    {
    	keepSessionOpen(keepSessionOpen);
        return getHibernateTemplate().findByCriteria(detached);
    }  
    
    private Session getHibernateSession() {
    	return (Session)getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                if(session !=null && session.isOpen())
                	return session;
				return null;
        	}
        });
    }
}

GuestDAO
Code:
package at.openhotel.dao;

import at.openhotel.model.object.Guest;


/**
 * @author Christoph
 */
public class GuestDAO extends GenericHibernateDAO <Guest, Integer> 
{
    
}

Bis jetzt habe ich das LazyProblem durch
SessionFactoryUtils.initDeferredClose(getHibernateTemplate().getSessionFactory());
gelöst, doch leider treten (ich vermute) durch die offenen Sessions im laufe des Programms neue Fehler auf.
 
Mein Service verfügt über einen quasi DAO-Manager (ist nicht sehr elegant gelöst - für jeden Typ gibt es eine if-Anweisung), diese Klasse beeinhaltet DAOs zu allen Objekten.

DAOCollection
Code:
public class DAOCollection {
	 private AffinityDAO affinityDao;
	 private AffinityCategoryDAO affinitycategoryDao;
	 private BrockerDAO brockerDao;
	 private CompanyDAO companyDao;
	 private ContactPersonDAO contactpersonDao;
	 private CountryDAO countryDao;
	 private DefectDAO defectDao;
	 private EventDAO eventDao;
	 private FeedbackDAO feedbackDao;
	 private FeedbackCategoryDAO feedbackcategoryDao;
	 private FeedbackCriteriumDAO feedbackcriteriumDao;
	 private FeedbackEvaluationDAO feedbackevaluationDao;
	 private FeedbackFormDAO feedbackformDao;
	 private GuestDAO guestDao;
	 private JourneyTypeDAO journeytypeDao;
	 private LocationDAO locationDao;
	 private NationalityDAO nationalityDao;
	 private NewsletterDAO newsletterDao;
	 private ReservationDAO reservationDao;
	 private RoomDAO roomDao;
	 private RoomCategoryDAO roomcategoryDao;
	 private RoomFacilityDAO roomfacilityDao;
	 private ZipDAO zipDao;
	 
	 private UserDAO userDao;
	 private ModuleDAO moduleDao;
	
	public DAOCollection() {
		
	}
	public List<?> loadAll(DAOType t, boolean keepSessionOpen) {
		if(t.equals(DAOType.Affinity)) {
			 return getAffinityDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.AffinityCategory)) {
			 return getAffinitycategoryDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Brocker)) {
			 return getBrockerDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Company)) {
			 return getCompanyDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.ContactPerson)) {
			 return getContactpersonDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Country)) {
			 return getCountryDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Defect)) {
			 return getDefectDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Event)) {
			 return getEventDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Feedback)) {
			 return getFeedbackDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.FeedbackCategory)) {
			 return getFeedbackcategoryDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.FeedbackCriterium)) {
			 return getFeedbackcriteriumDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.FeedbackEvaluation)) {
			 return getFeedbackevaluationDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.FeedbackForm)) {
			 return getFeedbackformDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Guest)) {
			 return getGuestDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.JourneyType)) {
			 return getJourneytypeDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Location)) {
			 return getLocationDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Nationality)) {
			 return getNationalityDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Newsletter)) {
			 return getNewsletterDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Reservation)) {
			 return getReservationDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Room)) {
			 return getRoomDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.RoomCategory)) {
			 return getRoomcategoryDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.RoomFacility)) {
			 return getRoomfacilityDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Zip)) {
			 return getZipDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.Module)) {
			 return getModuleDao().loadAll(keepSessionOpen);
		}
		if(t.equals(DAOType.User)) {
			 return getUserDao().loadAll(keepSessionOpen);
		}
		return null;
	}
	public Object loadByID(DAOType t, Integer id, boolean keepSessionOpen) {
		if(t.equals(DAOType.Affinity)) {
			 return getAffinityDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.AffinityCategory)) {
			 return getAffinitycategoryDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Brocker)) {
			 return getBrockerDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Company)) {
			 return getCompanyDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.ContactPerson)) {
			 return getContactpersonDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Country)) {
			 return getCountryDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Defect)) {
			 return getDefectDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Event)) {
			 return getEventDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Feedback)) {
			 return getFeedbackDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.FeedbackCategory)) {
			 return getFeedbackcategoryDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.FeedbackCriterium)) {
			 return getFeedbackcriteriumDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.FeedbackEvaluation)) {
			 return getFeedbackevaluationDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.FeedbackForm)) {
			 return getFeedbackformDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Guest)) {
			 return getGuestDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.JourneyType)) {
			 return getJourneytypeDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Location)) {
			 return getLocationDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Nationality)) {
			 return getNationalityDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Newsletter)) {
			 return getNewsletterDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Reservation)) {
			 return getReservationDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Room)) {
			 return getRoomDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.RoomCategory)) {
			 return getRoomcategoryDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.RoomFacility)) {
			 return getRoomfacilityDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Zip)) {
			 return getZipDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.Module)) {
			 return getModuleDao().loadByID(id, keepSessionOpen);
		}
		if(t.equals(DAOType.User)) {
			 return getUserDao().loadByID(id, keepSessionOpen);
		}
		return null;
	}
	
	public void persist(Object o) {
		
		if(o instanceof Affinity) {
			 getAffinityDao().persist((Affinity) o);
		}
		if(o instanceof AffinityCategory) {
			 getAffinitycategoryDao().persist((AffinityCategory) o);
		}
		if(o instanceof Brocker) {
			 getBrockerDao().persist((Brocker) o);
		}
		if(o instanceof Company) {
			 getCompanyDao().persist((Company) o);
		}
		if(o instanceof ContactPerson) {
			 getContactpersonDao().persist((ContactPerson) o);
		}
		if(o instanceof Country) {
			 getCountryDao().persist((Country) o);
		}
		if(o instanceof Defect) {
			 getDefectDao().persist((Defect) o);
		}
		if(o instanceof Event) {
			 getEventDao().persist((Event) o);
		}
		if(o instanceof Feedback) {
			 getFeedbackDao().persist((Feedback) o);
		}
		if(o instanceof FeedbackCategory) {
			 getFeedbackcategoryDao().persist((FeedbackCategory) o);
		}
		if(o instanceof FeedbackCriterium) {
			 getFeedbackcriteriumDao().persist((FeedbackCriterium) o);
		}
		if(o instanceof FeedbackEvaluation) {
			 getFeedbackevaluationDao().persist((FeedbackEvaluation) o);
		}
		if(o instanceof FeedbackForm) {
			 getFeedbackformDao().persist((FeedbackForm) o);
		}
		if(o instanceof Guest) {
			 getGuestDao().persist((Guest) o);
		}
		if(o instanceof JourneyType) {
			 getJourneytypeDao().persist((JourneyType) o);
		}
		if(o instanceof Location) {
			 getLocationDao().persist((Location) o);
		}
		if(o instanceof Nationality) {
			 getNationalityDao().persist((Nationality) o);
		}
		if(o instanceof Newsletter) {
			 getNewsletterDao().persist((Newsletter) o);
		}
		if(o instanceof Reservation) {
			 getReservationDao().persist((Reservation) o);
		}
		if(o instanceof Room) {
			 getRoomDao().persist((Room) o);
		}
		if(o instanceof RoomCategory) {
			 getRoomcategoryDao().persist((RoomCategory) o);
		}
		if(o instanceof RoomFacility) {
			 getRoomfacilityDao().persist((RoomFacility) o);
		}
		if(o instanceof Zip) {
			 getZipDao().persist((Zip) o);
		}
		if(o instanceof User) {
			 getUserDao().persist((User) o);
		}
		if(o instanceof Module) {
			 getModuleDao().persist((Module) o);
		}
		
	}
	
	public void remove(Object o) {
		if(o instanceof Affinity) {
			 getAffinityDao().remove((Affinity) o);
		}
		if(o instanceof AffinityCategory) {
			 getAffinitycategoryDao().remove((AffinityCategory) o);
		}
		if(o instanceof Brocker) {
			 getBrockerDao().remove((Brocker) o);
		}
		if(o instanceof Company) {
			 getCompanyDao().remove((Company) o);
		}
		if(o instanceof ContactPerson) {
			 getContactpersonDao().remove((ContactPerson) o);
		}
		if(o instanceof Country) {
			 getCountryDao().remove((Country) o);
		}
		if(o instanceof Defect) {
			 getDefectDao().remove((Defect) o);
		}
		if(o instanceof Event) {
			 getEventDao().remove((Event) o);
		}
		if(o instanceof Feedback) {
			 getFeedbackDao().remove((Feedback) o);
		}
		if(o instanceof FeedbackCategory) {
			 getFeedbackcategoryDao().remove((FeedbackCategory) o);
		}
		if(o instanceof FeedbackCriterium) {
			 getFeedbackcriteriumDao().remove((FeedbackCriterium) o);
		}
		if(o instanceof FeedbackEvaluation) {
			 getFeedbackevaluationDao().remove((FeedbackEvaluation) o);
		}
		if(o instanceof FeedbackForm) {
			 getFeedbackformDao().remove((FeedbackForm) o);
		}
		if(o instanceof Guest) {
			 getGuestDao().remove((Guest) o);
		}
		if(o instanceof JourneyType) {
			 getJourneytypeDao().remove((JourneyType) o);
		}
		if(o instanceof Location) {
			 getLocationDao().remove((Location) o);
		}
		if(o instanceof Nationality) {
			 getNationalityDao().remove((Nationality) o);
		}
		if(o instanceof Newsletter) {
			 getNewsletterDao().remove((Newsletter) o);
		}
		if(o instanceof Reservation) {
			 getReservationDao().remove((Reservation) o);
		}
		if(o instanceof Room) {
			 getRoomDao().remove((Room) o);
		}
		if(o instanceof RoomCategory) {
			 getRoomcategoryDao().remove((RoomCategory) o);
		}
		if(o instanceof RoomFacility) {
			 getRoomfacilityDao().remove((RoomFacility) o);
		}
		if(o instanceof Zip) {
			 getZipDao().remove((Zip) o);
		}
		if(o instanceof User) {
			 getUserDao().remove((User) o);
		}
		if(o instanceof Module) {
			 getModuleDao().remove((Module) o);
		}

	}
	
	public void update(Object o) {
		if(o instanceof Affinity) {
			 getAffinityDao().update((Affinity) o);
		}
		if(o instanceof AffinityCategory) {
			 getAffinitycategoryDao().update((AffinityCategory) o);
		}
		if(o instanceof Brocker) {
			 getBrockerDao().update((Brocker) o);
		}
		if(o instanceof Company) {
			 getCompanyDao().update((Company) o);
		}
		if(o instanceof ContactPerson) {
			 getContactpersonDao().update((ContactPerson) o);
		}
		if(o instanceof Country) {
			 getCountryDao().update((Country) o);
		}
		if(o instanceof Defect) {
			 getDefectDao().update((Defect) o);
		}
		if(o instanceof Event) {
			 getEventDao().update((Event) o);
		}
		if(o instanceof Feedback) {
			 getFeedbackDao().update((Feedback) o);
		}
		if(o instanceof FeedbackCategory) {
			 getFeedbackcategoryDao().update((FeedbackCategory) o);
		}
		if(o instanceof FeedbackCriterium) {
			 getFeedbackcriteriumDao().update((FeedbackCriterium) o);
		}
		if(o instanceof FeedbackEvaluation) {
			 getFeedbackevaluationDao().update((FeedbackEvaluation) o);
		}
		if(o instanceof FeedbackForm) {
			 getFeedbackformDao().update((FeedbackForm) o);
		}
		if(o instanceof Guest) {
			 getGuestDao().update((Guest) o);
		}
		if(o instanceof JourneyType) {
			 getJourneytypeDao().update((JourneyType) o);
		}
		if(o instanceof Location) {
			 getLocationDao().update((Location) o);
		}
		if(o instanceof Nationality) {
			 getNationalityDao().update((Nationality) o);
		}
		if(o instanceof Newsletter) {
			 getNewsletterDao().update((Newsletter) o);
		}
		if(o instanceof Reservation) {
			 getReservationDao().update((Reservation) o);
		}
		if(o instanceof Room) {
			 getRoomDao().update((Room) o);
		}
		if(o instanceof RoomCategory) {
			 getRoomcategoryDao().update((RoomCategory) o);
		}
		if(o instanceof RoomFacility) {
			 getRoomfacilityDao().update((RoomFacility) o);
		}
		if(o instanceof Zip) {
			 getZipDao().update((Zip) o);
		}
		if(o instanceof User) {
			 getUserDao().update((User) o);
		}
		if(o instanceof Module) {
			 getModuleDao().update((Module) o);
		}
	}
	
	public List<?> findByCriteria (DAOType t, boolean keepSessionOpen, Criterion... cCriteriaList )  {
		if(t.equals(DAOType.Affinity)) {
			 return getAffinityDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.AffinityCategory)) {
			 return getAffinitycategoryDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Brocker)) {
			 return getBrockerDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Company)) {
			 return getCompanyDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.ContactPerson)) {
			 return getContactpersonDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Country)) {
			 return getCountryDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Defect)) {
			 return getDefectDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Event)) {
			 return getEventDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Feedback)) {
			 return getFeedbackDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.FeedbackCategory)) {
			 return getFeedbackcategoryDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.FeedbackCriterium)) {
			 return getFeedbackcriteriumDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.FeedbackEvaluation)) {
			 return getFeedbackevaluationDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.FeedbackForm)) {
			 return getFeedbackformDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Guest)) {
			 return getGuestDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.JourneyType)) {
			 return getJourneytypeDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Location)) {
			 return getLocationDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Nationality)) {
			 return getNationalityDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Newsletter)) {
			 return getNewsletterDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Reservation)) {
			 return getReservationDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Room)) {
			 return getRoomDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.RoomCategory)) {
			 return getRoomcategoryDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.RoomFacility)) {
			 return getRoomfacilityDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Zip)) {
			 return getZipDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.User)) {
			 return getUserDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		if(t.equals(DAOType.Module)) {
			 return getModuleDao().findByCriteria(keepSessionOpen, cCriteriaList);
		}
		return null;
	}
	
	public List<?> findByDetachedCriteria (DAOType t, DetachedCriteria criteria, boolean keepSessionOpen )  {
		if(t.equals(DAOType.Affinity)) {
			 return getAffinityDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.AffinityCategory)) {
			 return getAffinitycategoryDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Brocker)) {
			 return getBrockerDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Company)) {
			 return getCompanyDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.ContactPerson)) {
			 return getContactpersonDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Country)) {
			 return getCountryDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Defect)) {
			 return getDefectDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Event)) {
			 return getEventDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Feedback)) {
			 return getFeedbackDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.FeedbackCategory)) {
			 return getFeedbackcategoryDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.FeedbackCriterium)) {
			 return getFeedbackcriteriumDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.FeedbackEvaluation)) {
			 return getFeedbackevaluationDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.FeedbackForm)) {
			 return getFeedbackformDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Guest)) {
			 return getGuestDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.JourneyType)) {
			 return getJourneytypeDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Location)) {
			 return getLocationDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Nationality)) {
			 return getNationalityDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Newsletter)) {
			 return getNewsletterDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Reservation)) {
			 return getReservationDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Room)) {
			 return getRoomDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.RoomCategory)) {
			 return getRoomcategoryDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.RoomFacility)) {
			 return getRoomfacilityDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Zip)) {
			 return getZipDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.User)) {
			 return getUserDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		if(t.equals(DAOType.Module)) {
			 return getModuleDao().findByDetachedCriteria(criteria, keepSessionOpen);
		}
		return null;
	}
	
	 public void setUserDao( UserDAO userDao ) {
		 	this.userDao = userDao;
	 }
	 public UserDAO getUserDao() {
	 	return this.userDao;
	 }
	
	 public void setModuleDao( ModuleDAO moduleDao ) {
		 	this.moduleDao = moduleDao;
	 }
	 public ModuleDAO getModuleDao() {
	 	return this.moduleDao;
	 }
	 
	 public void setAffinityDao( AffinityDAO affinityDao ) {
		 	this.affinityDao = affinityDao;
	 }
	 public AffinityDAO getAffinityDao() {
	 	return this.affinityDao;
	 }

	 public void setAffinitycategoryDao( AffinityCategoryDAO affinitycategoryDao ) {
	 	this.affinitycategoryDao = affinitycategoryDao;
	 }
	 public AffinityCategoryDAO getAffinitycategoryDao() {
	 	return this.affinitycategoryDao;
	 }

	 public void setBrockerDao( BrockerDAO brockerDao ) {
	 	this.brockerDao = brockerDao;
	 }
	 public BrockerDAO getBrockerDao() {
	 	return this.brockerDao;
	 }

	 public void setCompanyDao( CompanyDAO companyDao ) {
	 	this.companyDao = companyDao;
	 }
	 public CompanyDAO getCompanyDao() {
	 	return this.companyDao;
	 }

	 public void setContactpersonDao( ContactPersonDAO contactpersonDao ) {
	 	this.contactpersonDao = contactpersonDao;
	 }
	 public ContactPersonDAO getContactpersonDao() {
	 	return this.contactpersonDao;
	 }

	 public void setCountryDao( CountryDAO countryDao ) {
	 	this.countryDao = countryDao;
	 }
	 public CountryDAO getCountryDao() {
	 	return this.countryDao;
	 }

	 public void setDefectDao( DefectDAO defectDao ) {
	 	this.defectDao = defectDao;
	 }
	 public DefectDAO getDefectDao() {
	 	return this.defectDao;
	 }

	 public void setEventDao( EventDAO eventDao ) {
	 	this.eventDao = eventDao;
	 }
	 public EventDAO getEventDao() {
	 	return this.eventDao;
	 }

	 public void setFeedbackDao( FeedbackDAO feedbackDao ) {
	 	this.feedbackDao = feedbackDao;
	 }
	 public FeedbackDAO getFeedbackDao() {
	 	return this.feedbackDao;
	 }

	 public void setFeedbackcategoryDao( FeedbackCategoryDAO feedbackcategoryDao ) {
	 	this.feedbackcategoryDao = feedbackcategoryDao;
	 }
	 public FeedbackCategoryDAO getFeedbackcategoryDao() {
	 	return this.feedbackcategoryDao;
	 }

	 public void setFeedbackcriteriumDao( FeedbackCriteriumDAO feedbackcriteriumDao ) {
	 	this.feedbackcriteriumDao = feedbackcriteriumDao;
	 }
	 public FeedbackCriteriumDAO getFeedbackcriteriumDao() {
	 	return this.feedbackcriteriumDao;
	 }

	 public void setFeedbackevaluationDao( FeedbackEvaluationDAO feedbackevaluationDao ) {
	 	this.feedbackevaluationDao = feedbackevaluationDao;
	 }
	 public FeedbackEvaluationDAO getFeedbackevaluationDao() {
	 	return this.feedbackevaluationDao;
	 }

	 public void setFeedbackformDao( FeedbackFormDAO feedbackformDao ) {
	 	this.feedbackformDao = feedbackformDao;
	 }
	 public FeedbackFormDAO getFeedbackformDao() {
	 	return this.feedbackformDao;
	 }

	 public void setGuestDao( GuestDAO guestDao ) {
	 	this.guestDao = guestDao;
	 }
	 public GuestDAO getGuestDao() {
	 	return this.guestDao;
	 }

	 public void setJourneytypeDao( JourneyTypeDAO journeytypeDao ) {
	 	this.journeytypeDao = journeytypeDao;
	 }
	 public JourneyTypeDAO getJourneytypeDao() {
	 	return this.journeytypeDao;
	 }

	 public void setLocationDao( LocationDAO locationDao ) {
	 	this.locationDao = locationDao;
	 }
	 public LocationDAO getLocationDao() {
	 	return this.locationDao;
	 }

	 public void setNationalityDao( NationalityDAO nationalityDao ) {
	 	this.nationalityDao = nationalityDao;
	 }
	 public NationalityDAO getNationalityDao() {
	 	return this.nationalityDao;
	 }

	 public void setNewsletterDao( NewsletterDAO newsletterDao ) {
	 	this.newsletterDao = newsletterDao;
	 }
	 public NewsletterDAO getNewsletterDao() {
	 	return this.newsletterDao;
	 }

	 public void setReservationDao( ReservationDAO reservationDao ) {
	 	this.reservationDao = reservationDao;
	 }
	 public ReservationDAO getReservationDao() {
	 	return this.reservationDao;
	 }

	 public void setRoomDao( RoomDAO roomDao ) {
	 	this.roomDao = roomDao;
	 }
	 public RoomDAO getRoomDao() {
	 	return this.roomDao;
	 }

	 public void setRoomcategoryDao( RoomCategoryDAO roomcategoryDao ) {
	 	this.roomcategoryDao = roomcategoryDao;
	 }
	 public RoomCategoryDAO getRoomcategoryDao() {
	 	return this.roomcategoryDao;
	 }

	 public void setRoomfacilityDao( RoomFacilityDAO roomfacilityDao ) {
	 	this.roomfacilityDao = roomfacilityDao;
	 }
	 public RoomFacilityDAO getRoomfacilityDao() {
	 	return this.roomfacilityDao;
	 }

	 public void setZipDao( ZipDAO zipDao ) {
	 	this.zipDao = zipDao;
	 }
	 public ZipDAO getZipDao() {
	 	return this.zipDao;
	 }	
}

Die Implementierung des ObjectServices sieht so aus:
Code:
/**
 * @author Christoph
 * 
 */
public class ObjectServiceImpl implements
		ObjectService {

	private DAOCollection dao;

	@Override
	public List<?> loadAll(DAOType t, boolean keepSessionOpen) {
		return dao.loadAll(t, keepSessionOpen);
	}
	
	@Override
	public Object loadByID(DAOType t, Integer id, boolean keepSessionOpen) {
		return dao.loadByID(t, id, keepSessionOpen);
	}
	
	@Override
	public void persist(Object o) {
		dao.persist(o);
	}
	
	@Override
	public void remove(Object o) {
		dao.remove(o);
	}
	
	@Override
	public void update(Object o) {
		dao.update(o);
	}
	
	@Override
	public List<?> findByCriteria (DAOType t, boolean keepSessionOpen, Criterion... cCriteriaList ) {
		return dao.findByCriteria(t, keepSessionOpen, cCriteriaList);
	}
	
	@Override
	public List<?> findByDetachedCriteria (DAOType t, DetachedCriteria criteria, boolean keepSessionOpen ) {
		return dao.findByDetachedCriteria(t, criteria, keepSessionOpen);
	}
	
	/**
	 * @return the customerDAO
	 */
	public DAOCollection getDao() {
		return dao;
	}

	/**
	 * @param customerDAO
	 *            the customerDAO to set
	 */
	public void setDao(DAOCollection dao) {
		this.dao = dao;
	}
}
 
Die Spring Konfiguration, inklusive dem Transaktionsmanagement:
applicationContext.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<bean id="datasource"
   class="com.mchange.v2.c3p0.ComboPooledDataSource">
 <property name="driverClass" value="com.mysql.jdbc.Driver"/>
      <property name="jdbcUrl" value="jdbc:mysql://localhost/openhotel"/>
      <property name="user" value="root"/>
      <property name="password" value="mysql"/>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="datasource" />
      <property name="configLocation">
         <value>classpath:hibernate.cfg.xml</value>
      </property>
   </bean>

<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
</bean>

<bean id="affinityDao" class="at.openhotel.dao.AffinityDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="affinitycategoryDao" class="at.openhotel.dao.AffinityCategoryDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="brockerDao" class="at.openhotel.dao.BrockerDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="companyDao" class="at.openhotel.dao.CompanyDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="contactpersonDao" class="at.openhotel.dao.ContactPersonDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="countryDao" class="at.openhotel.dao.CountryDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="defectDao" class="at.openhotel.dao.DefectDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="eventDao" class="at.openhotel.dao.EventDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="feedbackDao" class="at.openhotel.dao.FeedbackDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="feedbackcategoryDao" class="at.openhotel.dao.FeedbackCategoryDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="feedbackcriteriumDao" class="at.openhotel.dao.FeedbackCriteriumDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="feedbackevaluationDao" class="at.openhotel.dao.FeedbackEvaluationDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="feedbackformDao" class="at.openhotel.dao.FeedbackFormDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="guestDao" class="at.openhotel.dao.GuestDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="journeytypeDao" class="at.openhotel.dao.JourneyTypeDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="locationDao" class="at.openhotel.dao.LocationDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="nationalityDao" class="at.openhotel.dao.NationalityDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="newsletterDao" class="at.openhotel.dao.NewsletterDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="reservationDao" class="at.openhotel.dao.ReservationDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="roomDao" class="at.openhotel.dao.RoomDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="roomcategoryDao" class="at.openhotel.dao.RoomCategoryDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="roomfacilityDao" class="at.openhotel.dao.RoomFacilityDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="zipDao" class="at.openhotel.dao.ZipDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="mailRequestDao" class="at.openhotel.dao.MailRequestDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="moduleDao" class="at.openhotel.dao.ModuleDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="userDao" class="at.openhotel.dao.UserDAO">
	 <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="DAOCollection" class="at.openhotel.dao.DAOCollection">
	<property name="affinityDao" ref="affinityDao"/>
	<property name="affinitycategoryDao" ref="affinitycategoryDao"/>
	<property name="brockerDao" ref="brockerDao"/>
	<property name="companyDao" ref="companyDao"/>
	<property name="contactpersonDao" ref="contactpersonDao"/>
	<property name="countryDao" ref="countryDao"/>
	<property name="defectDao" ref="defectDao"/>
	<property name="eventDao" ref="eventDao"/>
	<property name="feedbackDao" ref="feedbackDao"/>
	<property name="feedbackcategoryDao" ref="feedbackcategoryDao"/>
	<property name="feedbackcriteriumDao" ref="feedbackcriteriumDao"/>
	<property name="feedbackevaluationDao" ref="feedbackevaluationDao"/>
	<property name="feedbackformDao" ref="feedbackformDao"/>
	<property name="guestDao" ref="guestDao"/>
	<property name="journeytypeDao" ref="journeytypeDao"/>
	<property name="locationDao" ref="locationDao"/>
	<property name="nationalityDao" ref="nationalityDao"/>
	<property name="newsletterDao" ref="newsletterDao"/>
	<property name="reservationDao" ref="reservationDao"/>
	<property name="roomDao" ref="roomDao"/>
	<property name="roomcategoryDao" ref="roomcategoryDao"/>
	<property name="roomfacilityDao" ref="roomfacilityDao"/>
	<property name="zipDao" ref="zipDao"/>
	<property name="mailRequestDao" ref="mailRequestDao"/>
	
	<property name="userDao" ref="userDao"/>
	<property name="moduleDao" ref="moduleDao"/>
</bean>

<bean id="objectService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager">
			<ref local="transactionManager"/>
		</property>
		<property name="target">
			<bean class="at.openhotel.rmi.service.impl.ObjectServiceImpl">
				<property name="dao" ref="DAOCollection"/>
			</bean>
		</property>
		<property name="proxyInterfaces">
			<list>
				<value>
					at.openhotel.rmi.service.ObjectService</value>
			</list>
		</property>
		<property name="transactionAttributes">
			<props>
					<!-- <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop> -->
					<prop key="*">PROPAGATION_REQUIRED</prop>
					<prop key="keep*">PROPAGATION_SUPPORTS</prop>
			</props>
		</property>
	</bean>
</beans>

Der Zugriff sieht so aus:
Code:
package TESThibernatezugriff;

import java.rmi.dgc.VMID;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

import at.openhotel.model.object.Brocker;
import at.openhotel.model.object.Company;
import at.openhotel.model.object.Country;
import at.openhotel.model.object.Guest;
import at.openhotel.model.object.Nationality;
import at.openhotel.model.object.Reservation;
import at.openhotel.model.object.Room;
import at.openhotel.model.object.RoomCategory;
import at.openhotel.model.object.Zip;
import at.openhotel.model.user.Module;
import at.openhotel.rmi.service.DAOType;
import at.openhotel.rmi.service.ObjectService;
import at.openhotel.search.MyQueries;

public class SpringTest {
	public static void main(String[]args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("Client: " + new VMID() + " is ready");
        //Object-Service
        ObjectService businessService = (ObjectService)applicationContext.getBean("objectService");
        
        /**
         * Gäste laden, 
         * -false damit kein SessionFactoryUtils.initDeferredClose(getHibernateTemplate().getSessionFactory()); ausgeführt wird
         */
        List<Guest> list = (List<Guest>) businessService.loadAll(DAOType.Guest, false);
        
        //LAZYInitializationException, da auf ein Unterobjekt des Gastes zugegriffen wird.
        System.out.println(list.get(0).getNationality().getName());
	}
}

Ich hoffe, dass mir jemand Tipps zum Aufbau des Programms, aber in erster Linie mit dem Lazy-Problem helfen kann.

MfG
Christoph
 
Ich vermute, dur wirst nicht darumherum kommen, saubere Transaktionsgrenzen zu definieren und darin deine Objekte so vorzubereiten, dass du ausserhalb keine LLE mehr bekommst. Was ich nicht ganz verstehe ist, wozu du diese DAOCollection brauchst. Wenn du Spring benutzt kannst du dir in die entsprechenden Beans genau die DAOs injecten lassen die du brauchst. Bei deinem Entwurf musst du ja beim Client auch wissen, welchen DAO Type du benötigst, warum also die DependencyInjection wegwerfen?

Grundsätzlich würde ich beim Thema ORM versuchen auf JPA zu setzen, gerade was den Clientcode angeht. Du exposed mit deinen DAOs Hibernate API (Criterion) was quasi die DAO Abstraktion ad absurdum führt. Es gibt da so ein nettes OpenSource Projekt namens Hades (dessen Project Lead ich zufällig bin ;) ), dass die Implementierung von Persistenzlayern mit Spring und JPA erheblich vereinfacht. Wenn du Lust hast, schaus dir mal an: http://hades.synyx.org. Das hilft dir sicher, viel weniger Code selbst zu implementieren bzw. die Flaws da rauszubauen. Fragen dazu beantworte ich dann auch gerne offline ;).

Gruß
Ollie
 
Hi,
ja mit der LLE habe ich mich auch vor einigen Monaten rumgeschlagen. Lag bei mir ebenfalls am Transactionsmanagement meiner Applikation. Du solltest darauf achten das sämtliche Datenzugriffe eines Serviceaufrufes o.ä. innerhalb der selben Transaction ablaufen.
 
Hi,
ja mit der LLE habe ich mich auch vor einigen Monaten rumgeschlagen. Lag bei mir ebenfalls am Transactionsmanagement meiner Applikation. Du solltest darauf achten das sämtliche Datenzugriffe eines Serviceaufrufes o.ä. innerhalb der selben Transaction ablaufen.
Hallo, danke für die Antwort. Doch das Transactionsmanagement ist doch mit Spring konfiguriert. Wie habe ich Einfluss darauf, dass die Zugriffe innerhalb der selben Transaktion ablaufen? Über Beispielcode würde ich mich sehr freuen :D.
 
Hi,
du kannst den Scope mit Spring AOP setzen. Hier ist meine Konfiguration:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Application context definition -->
<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/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

	<!-- Transaction Manager Bean -->
	<bean id="TransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory"><ref bean="SettingsSessionFactory"/></property>
		<property name="entityInterceptor"><ref local="ChainInterceptor" /></property>
		<!-- Start of user code for TransactionManager Bean properties !-->
		<!-- End of user code for TransactionManager Bean properties !-->
    </bean>

	<!-- Chain interceptor -->
	<bean id="ChainInterceptor" class="com.bla.blub.commons.hibernate.ChainInterceptor">
		<property name="interceptorList">
			<list>
				<!-- Start of user code for ChainInterceptor Bean definition !-->
				<!-- End of user code for ChainInterceptor Bean definition !-->
			</list>
		</property>
	</bean>

	<!-- the transactional advice (i.e. what 'happens'; see the <aop:advisor/> bean below) -->
	<tx:advice id="TransactionAdvice" transaction-manager="TransactionManager">
		<tx:attributes>
			<!-- tx:method name="get*" read-only="true"/>
			<tx:method name="find*" read-only="true"/>
			<tx:method name="check*" read-only="true"/-->
			
			<tx:method name="*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- ensure that the above transactional advice runs for any execution
		of an operation defined by the Process interface -->
	<aop:config>
		<aop:pointcut id="allWebOperation" expression="execution(* com.bla.jcp.ui.framework.execution.application.PafInvokerImpl.invoke(..))"/>
		<aop:advisor advice-ref="TransactionAdvice" pointcut-ref="allWebOperation"/>
	</aop:config>
	
<!-- Start of user code !-->
<!-- End of user code !-->

</beans>

Eine Transaktion startet vor dem Aufruf der Methode: com.bla.jcp.ui.framework.execution.application.PafInvokerImpl.invoke(..)
und ended beim Verlassen der selben.
 
Ähm ja... das ist technisch sicher richtig. Für Webapps würde ich mir allerdings nicht diesen Konfigurationsaufwand geben und eher den OpenSessionInViewFilter/-interceptor nutzen.

Das Problem bei Swingclients ist ja eher das, dass es schwer ist eine architektonische Linie zu finden an der man das Session- bzw. Transaktionsmanagement macht. Für Webapps bietet sich die Session auf Requestebene und die Transaktion auf Servicelayerebene an. Für Swingapps ist das eher wolkig. Also eher weniger ein rein technisches als ein konzeptionelles Problem.

Gruß
Ollie
 
Ähm ja... das ist technisch sicher richtig. Für Webapps würde ich mir allerdings nicht diesen Konfigurationsaufwand geben und eher den OpenSessionInViewFilter/-interceptor nutzen.

Das Problem bei Swingclients ist ja eher das, dass es schwer ist eine architektonische Linie zu finden an der man das Session- bzw. Transaktionsmanagement macht. Für Webapps bietet sich die Session auf Requestebene und die Transaktion auf Servicelayerebene an. Für Swingapps ist das eher wolkig. Also eher weniger ein rein technisches als ein konzeptionelles Problem.

Gruß
Ollie
 

Neue Beiträge

Zurück