Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » [Teneo] Possible to use Teneo with hibernate and Spring Framework?
[Teneo] Possible to use Teneo with hibernate and Spring Framework? [message #73546] Sun, 04 March 2007 18:35 Go to next message
Jean-Denis Boudreault is currently offline Jean-Denis BoudreaultFriend
Messages: 55
Registered: July 2009
Member
Hi!

i was wondering if it was possible to use Teneo with the spring framework.
Spring handles my factory as well as transactions automatically. Could i
configure teneo to work with this?


and if so, is there an example of how to do it out there?


thanks!
Re: [Teneo] Possible to use Teneo with hibernate and Spring Framework? [message #73562 is a reply to message #73546] Sun, 04 March 2007 22:16 Go to previous messageGo to next message
Aleksander Bandelj is currently offline Aleksander BandeljFriend
Messages: 98
Registered: July 2009
Member
This is a multi-part message in MIME format.
--------------060707010409040009040601
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

You could simply extend LocalSessionFactoryBean. See the attachments for
implementations, I place them under same license as Teneo and hope they
could be cleaned up and rolled into Teneo at some point. Example setup:

<bean id="entity.epackage" class="si.academa.spring.emf.EPackageSet">
<property name="generatedPackages">
<props>
<prop key="http:///si/academa/entity/geo.ecore">
si.academa.entity.geo.GeoPackage
</prop>
</props>
</property>
<property name="modelLocations">
<value>classpath:academa/extension/ecore/rpe.ecore</value>
</property>
</bean>

<bean id="hsf.default" parent="abstract.hsf" lazy-init="false">
<property name="dataSource" ref="ds.default" />
<property name="schemaUpdate" value="true" />

<property name="modelPackages" ref="entity.epackage" />
<property name="modelPersistenceProperties">
<props>
<prop
key="teneo.mapping.set_entity_automatically">false</prop>
<prop key="teneo.mapping.always_version">false</prop>
<prop key="teneo.mapping.disable_econtainer">true</prop>
</props>
</property>
</bean>


Jean-Denis Boudreault wrote:
> Hi!
>
> i was wondering if it was possible to use Teneo with the spring framework.
> Spring handles my factory as well as transactions automatically. Could i
> configure teneo to work with this?
>
>
> and if so, is there an example of how to do it out there?
>
>
> thanks!
>
>
>


--------------060707010409040009040601
Content-Type: text/x-java;
name="EPackageSet.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="EPackageSet.java"

package si.academa.spring.emf;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.ClassUtils;

public class EPackageSet implements FactoryBean,InitializingBean {

public Object getObject() throws Exception {
return _epackages;
}
@SuppressWarnings("unchecked")
public Class getObjectType() {
return Set.class;
}
public boolean isSingleton() {
return true;
}
public void afterPropertiesSet() throws Exception {
_epackages= registerEPackages();
}

private Properties _genPackages;

public Properties getGeneratedPackages() {
return _genPackages;
}
public void setGeneratedPackages(Properties packages) {
_genPackages= packages;
}

private Set<EPackage> _epackages;

@SuppressWarnings("unchecked")
private Set<EPackage> registerEPackages() throws Exception {
if (_genPackages == null && _modelLocations == null) return Collections.EMPTY_SET;
Set<EPackage> epackages= new HashSet<EPackage>();
registerGeneratedPackages(epackages);
registerDynamicPackages(epackages);
return epackages;
}
///
/// generated
///
private void registerGeneratedPackages(Set<EPackage> epackages) throws Exception {
for (Map.Entry e : _genPackages.entrySet()) {
EPackage epackage= registerGeneratedPackage((String) e.getKey(), (String) e.getValue());
epackages.add(epackage);
}
}
private EPackage registerGeneratedPackage(String key, String klassName) throws Exception {
Class< ? > klass= ClassUtils.forName(klassName);
EPackage epackage= (EPackage) klass.getField("eINSTANCE").get(null);
EPackage.Registry.INSTANCE.put(key, epackage);
return epackage;
}

///
/// dynamic
///
private ResourceSet _packageResourceSet;

public ResourceSet getPackageResourceSet() {
if (_packageResourceSet == null) {
_packageResourceSet= new ResourceSetImpl();
_packageResourceSet.getResourceFactoryRegistry().getExtensio nToFactoryMap().put( "ecore",
new EcoreResourceFactoryImpl());
}
return _packageResourceSet;
}

private org.springframework.core.io.Resource[] _modelLocations;

public org.springframework.core.io.Resource[] getModelLocations() {
return _modelLocations;
}
public void setModelLocations(org.springframework.core.io.Resource[] ecoreLocations) {
this._modelLocations= ecoreLocations;
}
private void registerDynamicPackages(Set<EPackage> epackages) throws Exception {
for (org.springframework.core.io.Resource resourcePath : _modelLocations) {
Resource resource= getPackageResourceSet().getResource(
URI.createFileURI(resourcePath.getFile().getAbsolutePath()), true);
for (EObject e1 : resource.getContents()) {
EPackage epackage= (EPackage) e1;
getPackageResourceSet().getPackageRegistry().put(epackage.ge tNsURI(), epackage);
epackages.add(epackage);
}
}
}
}

--------------060707010409040009040601
Content-Type: text/x-java;
name="LocalSessionFactoryBean.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="LocalSessionFactoryBean.java"

package si.academa.spring.emf.teneo;

import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.hibernate.HbContextImpl;
import org.eclipse.emf.teneo.hibernate.HbDataStore;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class LocalSessionFactoryBean extends org.springframework.orm.hibernate3.LocalSessionFactoryBean {

private final static Log LOG= LogFactory.getLog(LocalSessionFactoryBean.class);

private Properties _persistenceProperties;

public Properties getModelPersistenceProperties() {
return _persistenceProperties;
}
public void setModelPersistenceProperties(Properties props) {
_persistenceProperties= props;
}

private Set<EPackage> _packages;

public Set<EPackage> getModelPackages() {
return _packages;
}
public void setModelPackages(Set<EPackage> packages) {
_packages= packages;
}

private HbDataStore _store;

public HbDataStore getStore() {
return _store;
}
protected SessionFactory newSessionFactory(final Configuration config) throws HibernateException {
Properties props= PersistenceOptions.getDefaultProperties();
if (_persistenceProperties != null) {
for (Map.Entry e : _persistenceProperties.entrySet()) {
props.put(e.getKey(), e.getValue());
}
}
// schema update will be performed by LocalSessionFactoryBean
props.setProperty(PersistenceOptions.UPDATE_SCHEMA, "false");
EPackage[] packages= getEPackages();
if (packages == null) return super.newSessionFactory(config);
_store= new HbDataStore();
_store.setPersistenceProperties(props);
// reuse already created configuration
_store.setHbContext(new HbContextImpl() {

public Configuration createConfiguration() {
return config;
}
});
_store.setEPackages(packages);
SessionFactory sessionFactory= _store.getSessionFactory();
if(LOG.isDebugEnabled()) {
LOG.debug(_store.getMappingXML());
}
return sessionFactory;
}
private EPackage[] getEPackages() {
if (_packages == null) return null;
return _packages.toArray(new EPackage[_packages.size()]);
}
}

--------------060707010409040009040601--
Re: [Teneo] Possible to use Teneo with hibernate and Spring Framework? [message #73580 is a reply to message #73562] Mon, 05 March 2007 01:55 Go to previous message
Jean-Denis Boudreault is currently offline Jean-Denis BoudreaultFriend
Messages: 55
Registered: July 2009
Member
Hi Aleksander,





I just tried it out and it works like a charm. Thanks a lot for your great
answer!





I was investigating Teneo and it pretty much wraps it up for us, we are
going forward with teneo.



thanks





"Aleksander Bandelj" <aleksander.bandelj@academa.si> wrote in message
news:esfgfl$vmj$1@utils.eclipse.org...
> You could simply extend LocalSessionFactoryBean. See the attachments for
> implementations, I place them under same license as Teneo and hope they
> could be cleaned up and rolled into Teneo at some point. Example setup:
>
> <bean id="entity.epackage" class="si.academa.spring.emf.EPackageSet">
> <property name="generatedPackages">
> <props>
> <prop key="http:///si/academa/entity/geo.ecore">
> si.academa.entity.geo.GeoPackage
> </prop>
> </props>
> </property>
> <property name="modelLocations">
> <value>classpath:academa/extension/ecore/rpe.ecore</value>
> </property>
> </bean>
>
> <bean id="hsf.default" parent="abstract.hsf" lazy-init="false">
> <property name="dataSource" ref="ds.default" />
> <property name="schemaUpdate" value="true" />
>
> <property name="modelPackages" ref="entity.epackage" />
> <property name="modelPersistenceProperties">
> <props>
> <prop
> key="teneo.mapping.set_entity_automatically">false</prop>
> <prop key="teneo.mapping.always_version">false</prop>
> <prop key="teneo.mapping.disable_econtainer">true</prop>
> </props>
> </property>
> </bean>
>
>
> Jean-Denis Boudreault wrote:
>> Hi!
>>
>> i was wondering if it was possible to use Teneo with the spring
>> framework.
>> Spring handles my factory as well as transactions automatically. Could i
>> configure teneo to work with this?
>>
>>
>> and if so, is there an example of how to do it out there?
>>
>>
>> thanks!
>>
>>
>>
>
>


------------------------------------------------------------ --------------------


> package si.academa.spring.emf;
>
> import java.util.Collections;
> import java.util.HashSet;
> import java.util.Map;
> import java.util.Properties;
> import java.util.Set;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.emf.ecore.EPackage;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
> import org.springframework.beans.factory.FactoryBean;
> import org.springframework.beans.factory.InitializingBean;
> import org.springframework.util.ClassUtils;
>
> public class EPackageSet implements FactoryBean,InitializingBean {
>
> public Object getObject() throws Exception {
> return _epackages;
> }
> @SuppressWarnings("unchecked")
> public Class getObjectType() {
> return Set.class;
> }
> public boolean isSingleton() {
> return true;
> }
> public void afterPropertiesSet() throws Exception {
> _epackages= registerEPackages();
> }
>
> private Properties _genPackages;
>
> public Properties getGeneratedPackages() {
> return _genPackages;
> }
> public void setGeneratedPackages(Properties packages) {
> _genPackages= packages;
> }
>
> private Set<EPackage> _epackages;
>
> @SuppressWarnings("unchecked")
> private Set<EPackage> registerEPackages() throws Exception {
> if (_genPackages == null && _modelLocations == null) return
> Collections.EMPTY_SET;
> Set<EPackage> epackages= new HashSet<EPackage>();
> registerGeneratedPackages(epackages);
> registerDynamicPackages(epackages);
> return epackages;
> }
> ///
> /// generated
> ///
> private void registerGeneratedPackages(Set<EPackage> epackages) throws
> Exception {
> for (Map.Entry e : _genPackages.entrySet()) {
> EPackage epackage= registerGeneratedPackage((String) e.getKey(), (String)
> e.getValue());
> epackages.add(epackage);
> }
> }
> private EPackage registerGeneratedPackage(String key, String klassName)
> throws Exception {
> Class< ? > klass= ClassUtils.forName(klassName);
> EPackage epackage= (EPackage) klass.getField("eINSTANCE").get(null);
> EPackage.Registry.INSTANCE.put(key, epackage);
> return epackage;
> }
>
> ///
> /// dynamic
> ///
> private ResourceSet _packageResourceSet;
>
> public ResourceSet getPackageResourceSet() {
> if (_packageResourceSet == null) {
> _packageResourceSet= new ResourceSetImpl();
> _packageResourceSet.getResourceFactoryRegistry().getExtensio nToFactoryMap().put( "ecore",
> new EcoreResourceFactoryImpl());
> }
> return _packageResourceSet;
> }
>
> private org.springframework.core.io.Resource[] _modelLocations;
>
> public org.springframework.core.io.Resource[] getModelLocations() {
> return _modelLocations;
> }
> public void setModelLocations(org.springframework.core.io.Resource[]
> ecoreLocations) {
> this._modelLocations= ecoreLocations;
> }
> private void registerDynamicPackages(Set<EPackage> epackages) throws
> Exception {
> for (org.springframework.core.io.Resource resourcePath : _modelLocations)
> {
> Resource resource= getPackageResourceSet().getResource(
> URI.createFileURI(resourcePath.getFile().getAbsolutePath()), true);
> for (EObject e1 : resource.getContents()) {
> EPackage epackage= (EPackage) e1;
> getPackageResourceSet().getPackageRegistry().put(epackage.ge tNsURI(),
> epackage);
> epackages.add(epackage);
> }
> }
> }
> }
>


------------------------------------------------------------ --------------------


> package si.academa.spring.emf.teneo;
>
> import java.util.Map;
> import java.util.Properties;
> import java.util.Set;
>
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
> import org.eclipse.emf.ecore.EPackage;
> import org.eclipse.emf.teneo.PersistenceOptions;
> import org.eclipse.emf.teneo.hibernate.HbContextImpl;
> import org.eclipse.emf.teneo.hibernate.HbDataStore;
> import org.hibernate.HibernateException;
> import org.hibernate.SessionFactory;
> import org.hibernate.cfg.Configuration;
>
> public class LocalSessionFactoryBean extends
> org.springframework.orm.hibernate3.LocalSessionFactoryBean {
>
> private final static Log LOG=
> LogFactory.getLog(LocalSessionFactoryBean.class);
>
> private Properties _persistenceProperties;
>
> public Properties getModelPersistenceProperties() {
> return _persistenceProperties;
> }
> public void setModelPersistenceProperties(Properties props) {
> _persistenceProperties= props;
> }
>
> private Set<EPackage> _packages;
>
> public Set<EPackage> getModelPackages() {
> return _packages;
> }
> public void setModelPackages(Set<EPackage> packages) {
> _packages= packages;
> }
>
> private HbDataStore _store;
>
> public HbDataStore getStore() {
> return _store;
> }
> protected SessionFactory newSessionFactory(final Configuration config)
> throws HibernateException {
> Properties props= PersistenceOptions.getDefaultProperties();
> if (_persistenceProperties != null) {
> for (Map.Entry e : _persistenceProperties.entrySet()) {
> props.put(e.getKey(), e.getValue());
> }
> }
> // schema update will be performed by LocalSessionFactoryBean
> props.setProperty(PersistenceOptions.UPDATE_SCHEMA, "false");
> EPackage[] packages= getEPackages();
> if (packages == null) return super.newSessionFactory(config);
> _store= new HbDataStore();
> _store.setPersistenceProperties(props);
> // reuse already created configuration
> _store.setHbContext(new HbContextImpl() {
>
> public Configuration createConfiguration() {
> return config;
> }
> });
> _store.setEPackages(packages);
> SessionFactory sessionFactory= _store.getSessionFactory();
> if(LOG.isDebugEnabled()) {
> LOG.debug(_store.getMappingXML());
> }
> return sessionFactory;
> }
> private EPackage[] getEPackages() {
> if (_packages == null) return null;
> return _packages.toArray(new EPackage[_packages.size()]);
> }
> }
>
Re: [Teneo] Possible to use Teneo with hibernate and Spring Framework? [message #603090 is a reply to message #73546] Sun, 04 March 2007 22:16 Go to previous message
Aleksander Bandelj is currently offline Aleksander BandeljFriend
Messages: 98
Registered: July 2009
Member
This is a multi-part message in MIME format.
--------------060707010409040009040601
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

You could simply extend LocalSessionFactoryBean. See the attachments for
implementations, I place them under same license as Teneo and hope they
could be cleaned up and rolled into Teneo at some point. Example setup:

<bean id="entity.epackage" class="si.academa.spring.emf.EPackageSet">
<property name="generatedPackages">
<props>
<prop key="http:///si/academa/entity/geo.ecore">
si.academa.entity.geo.GeoPackage
</prop>
</props>
</property>
<property name="modelLocations">
<value>classpath:academa/extension/ecore/rpe.ecore</value>
</property>
</bean>

<bean id="hsf.default" parent="abstract.hsf" lazy-init="false">
<property name="dataSource" ref="ds.default" />
<property name="schemaUpdate" value="true" />

<property name="modelPackages" ref="entity.epackage" />
<property name="modelPersistenceProperties">
<props>
<prop
key="teneo.mapping.set_entity_automatically">false</prop>
<prop key="teneo.mapping.always_version">false</prop>
<prop key="teneo.mapping.disable_econtainer">true</prop>
</props>
</property>
</bean>


Jean-Denis Boudreault wrote:
> Hi!
>
> i was wondering if it was possible to use Teneo with the spring framework.
> Spring handles my factory as well as transactions automatically. Could i
> configure teneo to work with this?
>
>
> and if so, is there an example of how to do it out there?
>
>
> thanks!
>
>
>


--------------060707010409040009040601
Content-Type: text/x-java;
name="EPackageSet.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="EPackageSet.java"

package si.academa.spring.emf;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.ClassUtils;

public class EPackageSet implements FactoryBean,InitializingBean {

public Object getObject() throws Exception {
return _epackages;
}
@SuppressWarnings("unchecked")
public Class getObjectType() {
return Set.class;
}
public boolean isSingleton() {
return true;
}
public void afterPropertiesSet() throws Exception {
_epackages= registerEPackages();
}

private Properties _genPackages;

public Properties getGeneratedPackages() {
return _genPackages;
}
public void setGeneratedPackages(Properties packages) {
_genPackages= packages;
}

private Set<EPackage> _epackages;

@SuppressWarnings("unchecked")
private Set<EPackage> registerEPackages() throws Exception {
if (_genPackages == null && _modelLocations == null) return Collections.EMPTY_SET;
Set<EPackage> epackages= new HashSet<EPackage>();
registerGeneratedPackages(epackages);
registerDynamicPackages(epackages);
return epackages;
}
///
/// generated
///
private void registerGeneratedPackages(Set<EPackage> epackages) throws Exception {
for (Map.Entry e : _genPackages.entrySet()) {
EPackage epackage= registerGeneratedPackage((String) e.getKey(), (String) e.getValue());
epackages.add(epackage);
}
}
private EPackage registerGeneratedPackage(String key, String klassName) throws Exception {
Class< ? > klass= ClassUtils.forName(klassName);
EPackage epackage= (EPackage) klass.getField("eINSTANCE").get(null);
EPackage.Registry.INSTANCE.put(key, epackage);
return epackage;
}

///
/// dynamic
///
private ResourceSet _packageResourceSet;

public ResourceSet getPackageResourceSet() {
if (_packageResourceSet == null) {
_packageResourceSet= new ResourceSetImpl();
_packageResourceSet.getResourceFactoryRegistry().getExtensio nToFactoryMap().put( "ecore",
new EcoreResourceFactoryImpl());
}
return _packageResourceSet;
}

private org.springframework.core.io.Resource[] _modelLocations;

public org.springframework.core.io.Resource[] getModelLocations() {
return _modelLocations;
}
public void setModelLocations(org.springframework.core.io.Resource[] ecoreLocations) {
this._modelLocations= ecoreLocations;
}
private void registerDynamicPackages(Set<EPackage> epackages) throws Exception {
for (org.springframework.core.io.Resource resourcePath : _modelLocations) {
Resource resource= getPackageResourceSet().getResource(
URI.createFileURI(resourcePath.getFile().getAbsolutePath()), true);
for (EObject e1 : resource.getContents()) {
EPackage epackage= (EPackage) e1;
getPackageResourceSet().getPackageRegistry().put(epackage.ge tNsURI(), epackage);
epackages.add(epackage);
}
}
}
}

--------------060707010409040009040601
Content-Type: text/x-java;
name="LocalSessionFactoryBean.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="LocalSessionFactoryBean.java"

package si.academa.spring.emf.teneo;

import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.hibernate.HbContextImpl;
import org.eclipse.emf.teneo.hibernate.HbDataStore;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class LocalSessionFactoryBean extends org.springframework.orm.hibernate3.LocalSessionFactoryBean {

private final static Log LOG= LogFactory.getLog(LocalSessionFactoryBean.class);

private Properties _persistenceProperties;

public Properties getModelPersistenceProperties() {
return _persistenceProperties;
}
public void setModelPersistenceProperties(Properties props) {
_persistenceProperties= props;
}

private Set<EPackage> _packages;

public Set<EPackage> getModelPackages() {
return _packages;
}
public void setModelPackages(Set<EPackage> packages) {
_packages= packages;
}

private HbDataStore _store;

public HbDataStore getStore() {
return _store;
}
protected SessionFactory newSessionFactory(final Configuration config) throws HibernateException {
Properties props= PersistenceOptions.getDefaultProperties();
if (_persistenceProperties != null) {
for (Map.Entry e : _persistenceProperties.entrySet()) {
props.put(e.getKey(), e.getValue());
}
}
// schema update will be performed by LocalSessionFactoryBean
props.setProperty(PersistenceOptions.UPDATE_SCHEMA, "false");
EPackage[] packages= getEPackages();
if (packages == null) return super.newSessionFactory(config);
_store= new HbDataStore();
_store.setPersistenceProperties(props);
// reuse already created configuration
_store.setHbContext(new HbContextImpl() {

public Configuration createConfiguration() {
return config;
}
});
_store.setEPackages(packages);
SessionFactory sessionFactory= _store.getSessionFactory();
if(LOG.isDebugEnabled()) {
LOG.debug(_store.getMappingXML());
}
return sessionFactory;
}
private EPackage[] getEPackages() {
if (_packages == null) return null;
return _packages.toArray(new EPackage[_packages.size()]);
}
}

--------------060707010409040009040601--
Re: [Teneo] Possible to use Teneo with hibernate and Spring Framework? [message #603096 is a reply to message #73562] Mon, 05 March 2007 01:55 Go to previous message
Jean-Denis Boudreault is currently offline Jean-Denis BoudreaultFriend
Messages: 55
Registered: July 2009
Member
Hi Aleksander,





I just tried it out and it works like a charm. Thanks a lot for your great
answer!





I was investigating Teneo and it pretty much wraps it up for us, we are
going forward with teneo.



thanks





"Aleksander Bandelj" <aleksander.bandelj@academa.si> wrote in message
news:esfgfl$vmj$1@utils.eclipse.org...
> You could simply extend LocalSessionFactoryBean. See the attachments for
> implementations, I place them under same license as Teneo and hope they
> could be cleaned up and rolled into Teneo at some point. Example setup:
>
> <bean id="entity.epackage" class="si.academa.spring.emf.EPackageSet">
> <property name="generatedPackages">
> <props>
> <prop key="http:///si/academa/entity/geo.ecore">
> si.academa.entity.geo.GeoPackage
> </prop>
> </props>
> </property>
> <property name="modelLocations">
> <value>classpath:academa/extension/ecore/rpe.ecore</value>
> </property>
> </bean>
>
> <bean id="hsf.default" parent="abstract.hsf" lazy-init="false">
> <property name="dataSource" ref="ds.default" />
> <property name="schemaUpdate" value="true" />
>
> <property name="modelPackages" ref="entity.epackage" />
> <property name="modelPersistenceProperties">
> <props>
> <prop
> key="teneo.mapping.set_entity_automatically">false</prop>
> <prop key="teneo.mapping.always_version">false</prop>
> <prop key="teneo.mapping.disable_econtainer">true</prop>
> </props>
> </property>
> </bean>
>
>
> Jean-Denis Boudreault wrote:
>> Hi!
>>
>> i was wondering if it was possible to use Teneo with the spring
>> framework.
>> Spring handles my factory as well as transactions automatically. Could i
>> configure teneo to work with this?
>>
>>
>> and if so, is there an example of how to do it out there?
>>
>>
>> thanks!
>>
>>
>>
>
>


------------------------------------------------------------ --------------------


> package si.academa.spring.emf;
>
> import java.util.Collections;
> import java.util.HashSet;
> import java.util.Map;
> import java.util.Properties;
> import java.util.Set;
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.emf.ecore.EPackage;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
> import org.springframework.beans.factory.FactoryBean;
> import org.springframework.beans.factory.InitializingBean;
> import org.springframework.util.ClassUtils;
>
> public class EPackageSet implements FactoryBean,InitializingBean {
>
> public Object getObject() throws Exception {
> return _epackages;
> }
> @SuppressWarnings("unchecked")
> public Class getObjectType() {
> return Set.class;
> }
> public boolean isSingleton() {
> return true;
> }
> public void afterPropertiesSet() throws Exception {
> _epackages= registerEPackages();
> }
>
> private Properties _genPackages;
>
> public Properties getGeneratedPackages() {
> return _genPackages;
> }
> public void setGeneratedPackages(Properties packages) {
> _genPackages= packages;
> }
>
> private Set<EPackage> _epackages;
>
> @SuppressWarnings("unchecked")
> private Set<EPackage> registerEPackages() throws Exception {
> if (_genPackages == null && _modelLocations == null) return
> Collections.EMPTY_SET;
> Set<EPackage> epackages= new HashSet<EPackage>();
> registerGeneratedPackages(epackages);
> registerDynamicPackages(epackages);
> return epackages;
> }
> ///
> /// generated
> ///
> private void registerGeneratedPackages(Set<EPackage> epackages) throws
> Exception {
> for (Map.Entry e : _genPackages.entrySet()) {
> EPackage epackage= registerGeneratedPackage((String) e.getKey(), (String)
> e.getValue());
> epackages.add(epackage);
> }
> }
> private EPackage registerGeneratedPackage(String key, String klassName)
> throws Exception {
> Class< ? > klass= ClassUtils.forName(klassName);
> EPackage epackage= (EPackage) klass.getField("eINSTANCE").get(null);
> EPackage.Registry.INSTANCE.put(key, epackage);
> return epackage;
> }
>
> ///
> /// dynamic
> ///
> private ResourceSet _packageResourceSet;
>
> public ResourceSet getPackageResourceSet() {
> if (_packageResourceSet == null) {
> _packageResourceSet= new ResourceSetImpl();
> _packageResourceSet.getResourceFactoryRegistry().getExtensio nToFactoryMap().put( "ecore",
> new EcoreResourceFactoryImpl());
> }
> return _packageResourceSet;
> }
>
> private org.springframework.core.io.Resource[] _modelLocations;
>
> public org.springframework.core.io.Resource[] getModelLocations() {
> return _modelLocations;
> }
> public void setModelLocations(org.springframework.core.io.Resource[]
> ecoreLocations) {
> this._modelLocations= ecoreLocations;
> }
> private void registerDynamicPackages(Set<EPackage> epackages) throws
> Exception {
> for (org.springframework.core.io.Resource resourcePath : _modelLocations)
> {
> Resource resource= getPackageResourceSet().getResource(
> URI.createFileURI(resourcePath.getFile().getAbsolutePath()), true);
> for (EObject e1 : resource.getContents()) {
> EPackage epackage= (EPackage) e1;
> getPackageResourceSet().getPackageRegistry().put(epackage.ge tNsURI(),
> epackage);
> epackages.add(epackage);
> }
> }
> }
> }
>


------------------------------------------------------------ --------------------


> package si.academa.spring.emf.teneo;
>
> import java.util.Map;
> import java.util.Properties;
> import java.util.Set;
>
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
> import org.eclipse.emf.ecore.EPackage;
> import org.eclipse.emf.teneo.PersistenceOptions;
> import org.eclipse.emf.teneo.hibernate.HbContextImpl;
> import org.eclipse.emf.teneo.hibernate.HbDataStore;
> import org.hibernate.HibernateException;
> import org.hibernate.SessionFactory;
> import org.hibernate.cfg.Configuration;
>
> public class LocalSessionFactoryBean extends
> org.springframework.orm.hibernate3.LocalSessionFactoryBean {
>
> private final static Log LOG=
> LogFactory.getLog(LocalSessionFactoryBean.class);
>
> private Properties _persistenceProperties;
>
> public Properties getModelPersistenceProperties() {
> return _persistenceProperties;
> }
> public void setModelPersistenceProperties(Properties props) {
> _persistenceProperties= props;
> }
>
> private Set<EPackage> _packages;
>
> public Set<EPackage> getModelPackages() {
> return _packages;
> }
> public void setModelPackages(Set<EPackage> packages) {
> _packages= packages;
> }
>
> private HbDataStore _store;
>
> public HbDataStore getStore() {
> return _store;
> }
> protected SessionFactory newSessionFactory(final Configuration config)
> throws HibernateException {
> Properties props= PersistenceOptions.getDefaultProperties();
> if (_persistenceProperties != null) {
> for (Map.Entry e : _persistenceProperties.entrySet()) {
> props.put(e.getKey(), e.getValue());
> }
> }
> // schema update will be performed by LocalSessionFactoryBean
> props.setProperty(PersistenceOptions.UPDATE_SCHEMA, "false");
> EPackage[] packages= getEPackages();
> if (packages == null) return super.newSessionFactory(config);
> _store= new HbDataStore();
> _store.setPersistenceProperties(props);
> // reuse already created configuration
> _store.setHbContext(new HbContextImpl() {
>
> public Configuration createConfiguration() {
> return config;
> }
> });
> _store.setEPackages(packages);
> SessionFactory sessionFactory= _store.getSessionFactory();
> if(LOG.isDebugEnabled()) {
> LOG.debug(_store.getMappingXML());
> }
> return sessionFactory;
> }
> private EPackage[] getEPackages() {
> if (_packages == null) return null;
> return _packages.toArray(new EPackage[_packages.size()]);
> }
> }
>
Previous Topic:[Teneo] Possible to use Teneo with hibernate and Spring Framework?
Next Topic:[Elver/Teneo] Hibernate Search (Apache Lucene) integration
Goto Forum:
  


Current Time: Tue Oct 20 12:32:35 GMT 2020

Powered by FUDForum. Page generated in 0.05235 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top