Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c8169bd6af67db8fe466c225dafffda7f97c38d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/***************************************************************************
 * 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.internal.protocol.revision.InternalCDORevision;

import org.eclipse.net4j.internal.util.collection.MoveableArrayList;

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
    final PersistentCollection collection = PersistableListHolder.getInstance().getListMapping(target, getCDOFeature());
    if (collection != null)
    {
      return collection;
    }

    // not yet, get the moveablearraylist
    @SuppressWarnings("unchecked")
    MoveableArrayList<Object> list = (MoveableArrayList<Object>)super.get(target);
    if (list == null)
    {
      // TODO: what initial size?
      list = new MoveableArrayList<Object>(10);
      final InternalCDORevision revision = (InternalCDORevision)target;
      revision.setValue(getCDOFeature(), list);
    }

    // wrap the moveablearraylist
    final HibernateMoveableListWrapper wrapper = new HibernateMoveableListWrapper();
    wrapper.setDelegate(list);

    // and return it
    return wrapper;
  }
}

Back to the top