Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormtaal2009-06-11 00:58:53 -0400
committermtaal2009-06-11 00:58:53 -0400
commit262c38a435c63dcf2b2348086aaf4f719233b706 (patch)
treea53c52ab0a6712222dde4e0b7a45b55c050ab3f4
parentd5934ff4a43ec71a5671a6e7831f43692a947574 (diff)
downloadorg.eclipse.emf.teneo-262c38a435c63dcf2b2348086aaf4f719233b706.tar.gz
org.eclipse.emf.teneo-262c38a435c63dcf2b2348086aaf4f719233b706.tar.xz
org.eclipse.emf.teneo-262c38a435c63dcf2b2348086aaf4f719233b706.zip
[278561]
[279862] [279863] [279864]
-rw-r--r--plugins/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/HbUtil.java112
-rw-r--r--plugins/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/mapping/property/EAttributePropertyHandler.java103
2 files changed, 94 insertions, 121 deletions
diff --git a/plugins/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/HbUtil.java b/plugins/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/HbUtil.java
index debcb6982..df8143f93 100644
--- a/plugins/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/HbUtil.java
+++ b/plugins/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/HbUtil.java
@@ -11,7 +11,7 @@
* Martin Taal
* </copyright>
*
- * $Id: HbUtil.java,v 1.24 2009/03/15 14:49:46 mtaal Exp $
+ * $Id: HbUtil.java,v 1.25 2009/06/11 04:58:54 mtaal Exp $
*/
package org.eclipse.emf.teneo.hibernate;
@@ -34,6 +34,7 @@ import org.eclipse.emf.teneo.hibernate.mapping.HibernatePersistentStoreAdapter;
import org.eclipse.emf.teneo.hibernate.mapping.econtainer.NewEContainerFeatureIDPropertyHandler;
import org.eclipse.emf.teneo.hibernate.mapping.identifier.IdentifierCacheHandler;
import org.eclipse.emf.teneo.hibernate.mapping.identifier.IdentifierPropertyHandler;
+import org.eclipse.emf.teneo.hibernate.mapping.property.EAttributePropertyHandler;
import org.eclipse.emf.teneo.type.PersistentStoreAdapter;
import org.eclipse.emf.teneo.util.StoreUtil;
import org.hibernate.cfg.Environment;
@@ -53,7 +54,7 @@ import org.hibernate.type.Type;
* Contains some utility methods.
*
* @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
- * @version $Revision: 1.24 $
+ * @version $Revision: 1.25 $
*/
public class HbUtil {
@@ -62,18 +63,15 @@ public class HbUtil {
/** Encode the id of an eobject */
public static String idToString(EObject eobj, HbDataStore hd) {
- final PersistentClass pc = hd.getPersistentClass(hd
- .getEntityNameStrategy().toEntityName(eobj.eClass()));
+ final PersistentClass pc = hd.getPersistentClass(hd.getEntityNameStrategy().toEntityName(eobj.eClass()));
if (pc == null) { // can happen with map entries
return null;
}
Object id;
if (eobj instanceof HibernateProxy) {
- id = ((HibernateProxy) eobj).getHibernateLazyInitializer()
- .getIdentifier();
+ id = ((HibernateProxy) eobj).getHibernateLazyInitializer().getIdentifier();
} else {
- id = pc.getIdentifierProperty().getGetter(eobj.getClass())
- .get(eobj);
+ id = pc.getIdentifierProperty().getGetter(eobj.getClass()).get(eobj);
}
if (id == null) {
id = IdentifierCacheHandler.getInstance().getID(eobj);
@@ -93,8 +91,7 @@ public class HbUtil {
/** Encode the id of an eobject */
public static Object stringToId(EClass eclass, HbDataStore hd, String id) {
try {
- final PersistentClass pc = hd.getPersistentClass(hd
- .getEntityNameStrategy().toEntityName(eclass));
+ final PersistentClass pc = hd.getPersistentClass(hd.getEntityNameStrategy().toEntityName(eclass));
final Type t = pc.getIdentifierProperty().getType();
if (t instanceof IdentifierType) {
return ((IdentifierType) t).stringToObject(id);
@@ -103,53 +100,42 @@ public class HbUtil {
}
return null;
} catch (Exception e) {
- throw new HbStoreException("Exception while converting id: " + id
- + " of eclass " + eclass.getName());
+ throw new HbStoreException("Exception while converting id: " + id + " of eclass " + eclass.getName());
}
}
/** Returns the correct accessor on the basis of the type of property */
- public static PropertyAccessor getPropertyAccessor(Property mappedProperty,
- HbDataStore ds, String entityName) {
+ public static PropertyAccessor getPropertyAccessor(Property mappedProperty, HbDataStore ds, String entityName) {
if (mappedProperty.getMetaAttribute(HbMapperConstants.ID_META) != null) { // synthetic
// ID
return new IdentifierPropertyHandler();
- } else if (mappedProperty
- .getMetaAttribute(HbMapperConstants.VERSION_META) != null) {
+ } else if (mappedProperty.getMetaAttribute(HbMapperConstants.VERSION_META) != null) {
return ds.getHbContext().createVersionAccessor();
- } else if (mappedProperty.getName().compareToIgnoreCase(
- "_identifierMapper") == 0) { // name
+ } else if (mappedProperty.getName().compareToIgnoreCase("_identifierMapper") == 0) { // name
// is
// used
// by
// hb
return new EmbeddedPropertyAccessor(); // new
// DummyPropertyHandler();
- } else if (mappedProperty.getName().compareToIgnoreCase(
- HbConstants.PROPERTY_ECONTAINER) == 0) {
+ } else if (mappedProperty.getName().compareToIgnoreCase(HbConstants.PROPERTY_ECONTAINER) == 0) {
return ds.getHbContext().createEContainerAccessor();
- } else if (mappedProperty.getName().compareToIgnoreCase(
- HbConstants.PROPERTY_ECONTAINER_FEATURE_NAME) == 0) {
- return ds.getExtensionManager().getExtension(
- NewEContainerFeatureIDPropertyHandler.class);
- } else if (mappedProperty.getName().compareToIgnoreCase(
- HbConstants.PROPERTY_ECONTAINER_FEATURE_ID) == 0) {
+ } else if (mappedProperty.getName().compareToIgnoreCase(HbConstants.PROPERTY_ECONTAINER_FEATURE_NAME) == 0) {
+ return ds.getExtensionManager().getExtension(NewEContainerFeatureIDPropertyHandler.class);
+ } else if (mappedProperty.getName().compareToIgnoreCase(HbConstants.PROPERTY_ECONTAINER_FEATURE_ID) == 0) {
return ds.getHbContext().createEContainerFeatureIDAccessor();
}
final EClass eClass = ds.getEntityNameStrategy().toEClass(entityName);
- final EStructuralFeature efeature = StoreUtil.getEStructuralFeature(
- eClass, mappedProperty.getName());
+ final EStructuralFeature efeature = StoreUtil.getEStructuralFeature(eClass, mappedProperty.getName());
if (efeature == null) {
- throw new HbMapperException(
- "Feature not found for eclass/entity/property "
- + eClass.getName() + "/" + entityName + "/"
- + mappedProperty.getName());
+ throw new HbMapperException("Feature not found for eclass/entity/property " + eClass.getName() + "/"
+ + entityName + "/" + mappedProperty.getName());
}
- log.debug("Creating property accessor for " + mappedProperty.getName()
- + "/" + entityName + "/" + efeature.getName());
+ log.debug("Creating property accessor for " + mappedProperty.getName() + "/" + entityName + "/"
+ + efeature.getName());
// check extra lazy
final boolean extraLazy = mappedProperty.getValue() instanceof Collection
@@ -160,8 +146,7 @@ public class HbUtil {
} else if (efeature instanceof EReference) {
final EReference eref = (EReference) efeature;
if (eref.isMany()) {
- return ds.getHbContext().createEListAccessor(efeature,
- extraLazy,
+ return ds.getHbContext().createEListAccessor(efeature, extraLazy,
ds.getPersistenceOptions().isMapEMapAsTrueMap());
} else {
return ds.getHbContext().createEReferenceAccessor(eref);
@@ -169,20 +154,25 @@ public class HbUtil {
} else {
final EAttribute eattr = (EAttribute) efeature;
if (eattr.isMany()) {
- return ds.getHbContext().createEListAccessor(efeature,
- extraLazy,
+ return ds.getHbContext().createEListAccessor(efeature, extraLazy,
ds.getPersistenceOptions().isMapEMapAsTrueMap());
} else {
// note also array types are going here!
- return ds.getHbContext().createEAttributeAccessor(eattr);
+ final PropertyAccessor pa = ds.getHbContext().createEAttributeAccessor(eattr);
+ // note this check is necessary because maybe somebody override HBContext.createEAttributeAccessor
+ // to not return a EAttributePropertyHandler
+ if (pa instanceof EAttributePropertyHandler) {
+ final EAttributePropertyHandler eAttributePropertyHandler = (EAttributePropertyHandler) pa;
+ eAttributePropertyHandler.setPersistenceOptions(ds.getPersistenceOptions());
+ }
+ return pa;
}
}
}
/** Returns the meta class uri, if not found then null is returned */
public static String getEClassNameFromFeatureMapMeta(PersistentClass pc) {
- MetaAttribute ma = pc
- .getMetaAttribute(HbMapperConstants.FEATUREMAP_META);
+ MetaAttribute ma = pc.getMetaAttribute(HbMapperConstants.FEATUREMAP_META);
if (ma == null) {
return null;
}
@@ -190,18 +180,15 @@ public class HbUtil {
}
/**
- * Returns the structural feature, handles the case of structural features
- * which are part of a feature map entry. public static EStructuralFeature
- * getFeature(PersistentClass pc, String propName, EPackage[] epacks) {
- * final MetaAttribute ma = pc.getMetaAttribute("eclass"); // TODO:
- * externalize final String eclassName; if (ma != null) { eclassName =
- * ma.getValue(); } else { eclassName = pc.getEntityName(); } return
+ * Returns the structural feature, handles the case of structural features which are part of a feature map entry.
+ * public static EStructuralFeature getFeature(PersistentClass pc, String propName, EPackage[] epacks) { final
+ * MetaAttribute ma = pc.getMetaAttribute("eclass"); // TODO: externalize final String eclassName; if (ma != null) {
+ * eclassName = ma.getValue(); } else { eclassName = pc.getEntityName(); } return
* StoreUtil.getEStructuralFeature(eclassName, propName, epacks); }
*/
/**
- * Creates and registers an emf data store using a set of generic store
- * properties
+ * Creates and registers an emf data store using a set of generic store properties
*/
public static HbDataStore getCreateDataStore(Properties props) {
final String name = props.getProperty(Constants.PROP_NAME);
@@ -212,19 +199,13 @@ public class HbUtil {
final Properties hbProps = new Properties();
hbProps.putAll(props);
- hbProps.put(Environment.USER, doTrim(props
- .getProperty(Constants.PROP_DB_USER)));
- hbProps.put(Environment.PASS, doTrim(props
- .getProperty(Constants.PROP_DB_PWD)));
- hbProps.put(Environment.DRIVER, doTrim(props
- .getProperty(Constants.PROP_DB_DRIVER)));
- hbProps.put(Environment.URL, doTrim(props
- .getProperty(Constants.PROP_DB_URL)));
- hbProps.put(Environment.DIALECT, doTrim(props
- .getProperty(Constants.PROP_DB_DIALECT)));
+ hbProps.put(Environment.USER, doTrim(props.getProperty(Constants.PROP_DB_USER)));
+ hbProps.put(Environment.PASS, doTrim(props.getProperty(Constants.PROP_DB_PWD)));
+ hbProps.put(Environment.DRIVER, doTrim(props.getProperty(Constants.PROP_DB_DRIVER)));
+ hbProps.put(Environment.URL, doTrim(props.getProperty(Constants.PROP_DB_URL)));
+ hbProps.put(Environment.DIALECT, doTrim(props.getProperty(Constants.PROP_DB_DIALECT)));
- EPackage[] epacks = StoreUtil.getEPackages(doTrim(props
- .getProperty(Constants.PROP_EPACKAGE_NSURI)));
+ EPackage[] epacks = StoreUtil.getEPackages(doTrim(props.getProperty(Constants.PROP_EPACKAGE_NSURI)));
// create a EMF Data Store, this is retrieved later again
eds = HbHelper.INSTANCE.createRegisterDataStore(name);
@@ -243,14 +224,11 @@ public class HbUtil {
}
/**
- * Checks if an object has a HibernatePersistentStoreAdapter and if it
- * doesn't creates one and returns it.
+ * Checks if an object has a HibernatePersistentStoreAdapter and if it doesn't creates one and returns it.
*/
- public static PersistentStoreAdapter getPersistentStoreAdapter(
- EObject eObject) {
+ public static PersistentStoreAdapter getPersistentStoreAdapter(EObject eObject) {
for (Adapter adapter : eObject.eAdapters()) {
- if (PersistentStoreAdapter.class.isAssignableFrom(adapter
- .getClass())) {
+ if (PersistentStoreAdapter.class.isAssignableFrom(adapter.getClass())) {
return (PersistentStoreAdapter) adapter;
}
}
diff --git a/plugins/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/mapping/property/EAttributePropertyHandler.java b/plugins/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/mapping/property/EAttributePropertyHandler.java
index f707b87ed..05259a4df 100644
--- a/plugins/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/mapping/property/EAttributePropertyHandler.java
+++ b/plugins/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/mapping/property/EAttributePropertyHandler.java
@@ -11,7 +11,7 @@
* Martin Taal
* </copyright>
*
- * $Id: EAttributePropertyHandler.java,v 1.11 2009/02/24 12:04:50 mtaal Exp $
+ * $Id: EAttributePropertyHandler.java,v 1.12 2009/06/11 04:58:53 mtaal Exp $
*/
package org.eclipse.emf.teneo.hibernate.mapping.property;
@@ -23,6 +23,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.util.AssertUtil;
import org.eclipse.emf.teneo.util.StoreUtil;
import org.hibernate.HibernateException;
@@ -34,21 +35,18 @@ import org.hibernate.property.PropertyAccessor;
import org.hibernate.property.Setter;
/**
- * Is a getter and setter for EMF eattribute which uses eGet and eSet.Handles
- * many==false properties.
+ * Is a getter and setter for EMF eattribute which uses eGet and eSet.Handles many==false properties.
*
- * This class implements both the getter, setter and propertyaccessor
- * interfaces. When the getGetter and getSetter methods are called it returns
- * itself.
+ * This class implements both the getter, setter and propertyaccessor interfaces. When the getGetter and getSetter
+ * methods are called it returns itself.
*
* This accessor also handles arrays of primitive types.
*
* @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
- * @version $Revision: 1.11 $
+ * @version $Revision: 1.12 $
*/
@SuppressWarnings("unchecked")
-public class EAttributePropertyHandler implements Getter, Setter,
- PropertyAccessor {
+public class EAttributePropertyHandler implements Getter, Setter, PropertyAccessor {
/**
* Generated Serial Version ID
*/
@@ -63,16 +61,15 @@ public class EAttributePropertyHandler implements Getter, Setter,
/** The instanceclass */
protected final Class instanceClass;
+ private boolean handleUnsetAsNull = false;
+
/** Constructor */
public EAttributePropertyHandler(EAttribute eAttribute) {
this.eAttribute = eAttribute;
instanceClass = eAttribute.getEType().getInstanceClass();
- AssertUtil.assertTrue(eAttribute.getName()
- + " is a many feature which is not handled by this accessor ",
+ AssertUtil.assertTrue(eAttribute.getName() + " is a many feature which is not handled by this accessor ",
!eAttribute.isMany());
- log
- .debug("Created getter/setter for "
- + StoreUtil.toString(eAttribute));
+ log.debug("Created getter/setter for " + StoreUtil.toString(eAttribute));
}
public EAttribute getEAttribute() {
@@ -82,22 +79,18 @@ public class EAttributePropertyHandler implements Getter, Setter,
/*
* (non-Javadoc)
*
- * @see org.hibernate.property.PropertyAccessor#getGetter(java.lang.Class,
- * java.lang.String)
+ * @see org.hibernate.property.PropertyAccessor#getGetter(java.lang.Class, java.lang.String)
*/
- public Getter getGetter(Class theClass, String propertyName)
- throws PropertyNotFoundException {
+ public Getter getGetter(Class theClass, String propertyName) throws PropertyNotFoundException {
return this;
}
/*
* (non-Javadoc)
*
- * @see org.hibernate.property.PropertyAccessor#getSetter(java.lang.Class,
- * java.lang.String)
+ * @see org.hibernate.property.PropertyAccessor#getSetter(java.lang.Class, java.lang.String)
*/
- public Setter getSetter(Class theClass, String propertyName)
- throws PropertyNotFoundException {
+ public Setter getSetter(Class theClass, String propertyName) throws PropertyNotFoundException {
return this;
}
@@ -107,17 +100,22 @@ public class EAttributePropertyHandler implements Getter, Setter,
* @see org.hibernate.property.Getter#get(java.lang.Object)
*/
public Object get(Object owner) throws HibernateException {
+ if (handleUnsetAsNull) {
+ EObject eobj = (EObject) owner;
+ if (!eobj.eIsSet(eAttribute)) {
+ return null;
+ }
+ }
return ((EObject) owner).eGet(eAttribute);
}
/*
* (non-Javadoc)
*
- * @see org.hibernate.property.Getter#getForInsert(java.lang.Object,
- * java.util.Map, org.hibernate.engine.SessionImplementor)
+ * @see org.hibernate.property.Getter#getForInsert(java.lang.Object, java.util.Map,
+ * org.hibernate.engine.SessionImplementor)
*/
- public Object getForInsert(Object arg0, Map arg1, SessionImplementor arg2)
- throws HibernateException {
+ public Object getForInsert(Object arg0, Map arg1, SessionImplementor arg2) throws HibernateException {
return get(arg0);
}
@@ -151,15 +149,16 @@ public class EAttributePropertyHandler implements Getter, Setter,
/*
* (non-Javadoc)
*
- * @see org.hibernate.property.Setter#set(java.lang.Object,
- * java.lang.Object, org.hibernate.engine.SessionFactoryImplementor)
+ * @see org.hibernate.property.Setter#set(java.lang.Object, java.lang.Object,
+ * org.hibernate.engine.SessionFactoryImplementor)
*/
- public void set(Object target, Object value,
- SessionFactoryImplementor factory) throws HibernateException {
+ public void set(Object target, Object value, SessionFactoryImplementor factory) throws HibernateException {
if (value == null) {
- // do not overwrite the defaults in the eobject
- // TODO is this really a correct approach?
+ if (handleUnsetAsNull) {
+ EObject eobj = (EObject) target;
+ eobj.eUnset(eAttribute);
+ }
return;
}
@@ -173,8 +172,7 @@ public class EAttributePropertyHandler implements Getter, Setter,
EObject eobj = (EObject) target;
final Object setValue;
- if (value != null && instanceClass != null
- && value.getClass() != instanceClass) {
+ if (value != null && instanceClass != null && value.getClass() != instanceClass) {
final Class valClass = value.getClass();
if (valClass == Integer[].class) {
setValue = convert((Integer[]) value);
@@ -203,8 +201,7 @@ public class EAttributePropertyHandler implements Getter, Setter,
private Object convert(Integer[] arr) {
if (instanceClass != int[].class) {
if (log.isDebugEnabled()) {
- log.debug("Expecting " + instanceClass.getName()
- + " as instance class but it is: "
+ log.debug("Expecting " + instanceClass.getName() + " as instance class but it is: "
+ arr.getClass().getName());
}
return arr;
@@ -220,8 +217,7 @@ public class EAttributePropertyHandler implements Getter, Setter,
private Object convert(Boolean[] arr) {
if (instanceClass != boolean[].class) {
if (log.isDebugEnabled()) {
- log.debug("Expecting " + instanceClass.getName()
- + " as instance class but it is: "
+ log.debug("Expecting " + instanceClass.getName() + " as instance class but it is: "
+ arr.getClass().getName());
}
return arr;
@@ -237,8 +233,7 @@ public class EAttributePropertyHandler implements Getter, Setter,
private Object convert(Byte[] arr) {
if (instanceClass != byte[].class) {
if (log.isDebugEnabled()) {
- log.debug("Expecting " + instanceClass.getName()
- + " as instance class but it is: "
+ log.debug("Expecting " + instanceClass.getName() + " as instance class but it is: "
+ arr.getClass().getName());
}
return arr;
@@ -254,8 +249,7 @@ public class EAttributePropertyHandler implements Getter, Setter,
private Object convert(Double[] arr) {
if (instanceClass != double[].class) {
if (log.isDebugEnabled()) {
- log.debug("Expecting " + instanceClass.getName()
- + " as instance class but it is: "
+ log.debug("Expecting " + instanceClass.getName() + " as instance class but it is: "
+ arr.getClass().getName());
}
return arr;
@@ -271,8 +265,7 @@ public class EAttributePropertyHandler implements Getter, Setter,
private Object convert(Float[] arr) {
if (instanceClass != float[].class) {
if (log.isDebugEnabled()) {
- log.debug("Expecting " + instanceClass.getName()
- + " as instance class but it is: "
+ log.debug("Expecting " + instanceClass.getName() + " as instance class but it is: "
+ arr.getClass().getName());
}
return arr;
@@ -288,8 +281,7 @@ public class EAttributePropertyHandler implements Getter, Setter,
private Object convert(Long[] arr) {
if (instanceClass != long[].class) {
if (log.isDebugEnabled()) {
- log.debug("Expecting [] as instance class but it is: "
- + instanceClass.getName());
+ log.debug("Expecting [] as instance class but it is: " + instanceClass.getName());
}
return arr;
}
@@ -304,8 +296,7 @@ public class EAttributePropertyHandler implements Getter, Setter,
private Object convert(Short[] arr) {
if (instanceClass != short[].class) {
if (log.isDebugEnabled()) {
- log.debug("Expecting short[] as instance class but it is: "
- + instanceClass.getName());
+ log.debug("Expecting short[] as instance class but it is: " + instanceClass.getName());
}
return arr;
}
@@ -318,14 +309,18 @@ public class EAttributePropertyHandler implements Getter, Setter,
/** Capature all, do not convert */
private Object convert(Object arr) {
- if (arr != null
- && instanceClass != null
- && (!instanceClass.isPrimitive() || !arr.getClass()
- .isPrimitive())) {
- log.debug("Expecting " + instanceClass.getName()
- + " as instance class but it is: "
+ if (arr != null && instanceClass != null && (!instanceClass.isPrimitive() || !arr.getClass().isPrimitive())) {
+ log.debug("Expecting " + instanceClass.getName() + " as instance class but it is: "
+ arr.getClass().getName());
}
return arr;
}
+
+ /**
+ * Pass in the persistion options.
+ */
+ public void setPersistenceOptions(PersistenceOptions po) {
+ handleUnsetAsNull = po.getHandleUnsetAsNull();
+ }
+
} \ No newline at end of file

Back to the top