blob: 858a75b80e7335de39d8c6f08be4263c0e0678d5 [file] [log] [blame]
/***************************************************************************
* Copyright (c) 2004 - 2008 Martin Taal and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Martin Taal - copied from CDORevisionPropertyHandler and adapted
**************************************************************************/
package org.eclipse.emf.cdo.server.internal.hibernate.tuplizer;
import org.eclipse.emf.cdo.common.revision.CDOList;
import org.eclipse.emf.cdo.spi.common.InternalCDORevision;
import org.hibernate.HibernateException;
import org.hibernate.collection.PersistentCollection;
/**
* @author Martin Taal
*/
// Howto handle hibernate lists:
// - a new owner: the owner is persisted and its lists are replaced with hibernate
// persistentlist, the hibernate persitentlist will have a delegate (internally) which is the list which was previously
// present in the owner.
// - an existing owner: the owner is read from the db and hibernate will set a persistentlist
// directly
//
// The solution also needs to handle the following:
// - cdo does not have direct java references but stores cdoids in the list while hibernate expects real java object
// references.
// - cdo uses a moveablearraylist and not the standard arraylist
//
// The solution:
// - never return null when hibernate asks for the current value of the manyreference, always
// return a MoveableArrayList so that hibernate uses that as the delegate, set the MoveableArrayList
public class CDOManyReferenceGetter extends CDOPropertyGetter
{
private static final long serialVersionUID = 1L;
public CDOManyReferenceGetter(CDORevisionTuplizer tuplizer, String propertyName)
{
super(tuplizer, propertyName);
}
@Override
public Object get(Object target) throws HibernateException
{
// Check if there is already a persistentcollection
PersistentCollection collection = PersistableListHolder.getInstance().getListMapping(target, getCDOFeature());
if (collection != null)
{
return collection;
}
InternalCDORevision revision = (InternalCDORevision)target;
CDOList list = revision.getList(getCDOFeature(), 10);
// Wrap the moveablearraylist
HibernateMoveableListWrapper wrapper = new HibernateMoveableListWrapper();
wrapper.setDelegate(list);
// And return it
return wrapper;
}
}