Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [Teneo] Example to dynamically modify EPackages stored using Teneo
[Teneo] Example to dynamically modify EPackages stored using Teneo [message #426343] Sun, 28 December 2008 01:25 Go to next message
Eclipse UserFriend
Originally posted by: mduduzi.keswa.isizwe.com

Hi,
I'm just about given up with the previous trial-and-error in working
with EPackages stored via teneo. Is there an example (I also looked
through Teneo test cases) that demonstrates the following:

1. Create a dynamic (empty) package and save it using Teneo
'session.save()'.

2. Load the package in 1. properly (registration and all) and add a
simple EClass to it; save it back to using 'session.save()'.

3. Load the package again and use it to create an EOBject from a class
added in 2.; and save the newly created EObject.

4. Do 1., 2., and 3. in separate test cases?


I'm using the recent version of Teneo (1.0.1) and dependent EMF libraries.
Any help will be much appreciated!

Rgds,
-Mdu
Re: [Teneo] Example to dynamically modify EPackages stored using Teneo [message #426347 is a reply to message #426343] Mon, 29 December 2008 03:20 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mduduzi.keswa.isizwe.com

Hi All,
I've gone a little further with a test case. I first run test case
testStoreEPackages() to create Ecore, XML, and new EPackage 'elv'. I'm
having a problem in testAddClassToElvEPackage(): please see a block
comment with PROBLEM: section.

Thanks,
-Mdu

package com.sbide.das.tests;

import java.util.List;
import java.util.Properties;

import junit.framework.TestCase;


import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
import org.eclipse.emf.teneo.hibernate.HbDataStore;
import org.eclipse.emf.teneo.hibernate.HbHelper;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Environment;

import com.irg.eaf.common.util.ApplicationProperties;
import com.sbide.das.EnvironmentConstants;

public class DynamicEPackageTestCase extends TestCase {

public void setUp()
{
}

public void testStoreEPackages()
{


final EcoreFactory efactory = EcoreFactory.eINSTANCE;
final EcorePackage epackage = EcorePackage.eINSTANCE;
final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;

EPackage.Registry.INSTANCE.put(epackage.getNsURI(), epackage);
EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(), xmlpackage);


// create the SchoolBook EClass
EClass schoolBookEClass = efactory.createEClass();
schoolBookEClass.setName("SchoolBook");

// create a new attribute for this EClass
EAttribute level = efactory.createEAttribute();
level.setName("level");
level.setEType(epackage.getEInt());
schoolBookEClass.getEStructuralFeatures().add(level);

// create a course
EClass courseEClass = efactory.createEClass();
courseEClass.setName("Course");

// give the Course a name
EAttribute courseName = efactory.createEAttribute();
courseName.setName("courseName");
courseName.setEType(epackage.getEString());
courseEClass.getEStructuralFeatures().add(courseName);

// A course always uses one SchoolBook
EReference courseBook = efactory.createEReference();
courseBook.setName("courseBook");
courseBook.setEType(schoolBookEClass);
courseBook.setContainment(false);
courseEClass.getEStructuralFeatures().add(courseBook);

// Create a new EPackage and add the new EClasses
EPackage schoolPackage = efactory.createEPackage();
schoolPackage.setName("elv");
schoolPackage.setNsPrefix("elv");
schoolPackage.setNsURI("http:///www.elver.org/School");
schoolPackage.getEClassifiers().add(courseEClass);
schoolPackage.getEClassifiers().add(schoolBookEClass);
EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),
schoolPackage);

// Now reset the epackages in the datastore
HbDataStore hbds = createHBDS(new
EPackage[]{epackage,schoolPackage,xmlpackage});

// print the hibernate.hbm.xml for demo purposes
System.err.println(hbds.getMappingXML());

// and create a course
EObject course =
schoolPackage.getEFactoryInstance().create(courseEClass);
course.eSet(courseName, "Dutch Literature Level 1");

final SessionFactory sessionFactory = hbds.getSessionFactory();

// now persist them all
Session session = sessionFactory.openSession();
Transaction tx = session.getTransaction();
tx.begin();
session.save(xmlpackage);
session.save(epackage);
session.save(schoolPackage);
session.save(course);
tx.commit();

session.close();
}

public void testAddClassToElvEPackage()
{

final EcoreFactory efactory = EcoreFactory.eINSTANCE;
final EcorePackage epackage = EcorePackage.eINSTANCE;


// Now reset the epackages in the datastore
HbDataStore hbds = createHBDS(new EPackage[]{epackage});

SessionFactory sessionFactory = hbds.getSessionFactory();

/**
* Read all relevant epackages to use them for later calling
setPackages
*/
Session session = sessionFactory.openSession();
Transaction tx = session.getTransaction();
tx.begin();
Query qry = session.createQuery("from EPackage where name='elv'");
List<EPackage> pkgs = qry.list();
EPackage elvPkg = pkgs.get(0);
EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
qry = session.createQuery("from EPackage where name='ecore'");
pkgs = qry.list();
EPackage ecorePkg = pkgs.get(0);
EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
qry = session.createQuery("from EPackage where name='type'");
pkgs = qry.list();
EPackage typePkg = pkgs.get(0);
EPackage.Registry.INSTANCE.put(typePkg.getNsURI(), typePkg);
System.out.println("Collection Count:
"+session.getStatistics().getCollectionCount());
System.out.println("Entity Count:
"+session.getStatistics().getEntityCount());
elvPkg.getEClassifiers().size();
tx.commit();

/**
* Re-initialize store
*/
hbds.setEPackages((new EPackage[]{ecorePkg,elvPkg,typePkg}));
hbds.initialize();
sessionFactory = hbds.getSessionFactory();
session = sessionFactory.openSession();
tx = session.getTransaction();

/**
* Re-read epackages - if they are not re-read, they'd be still
attached to
* the old session before we called .setEPackages() and .initialize():
*
* PROBLEM: Each EPackage query below returns empty list (qry.list()).
* QUESTION: 1. I expected these to be still in the database - why
is the query
* returning empty results?
*/
tx.begin();
qry = session.createQuery("from EPackage where name='type'");
pkgs = qry.list();
typePkg = pkgs.get(0);
EPackage.Registry.INSTANCE.put(typePkg.getNsURI(), typePkg);
qry = session.createQuery("from EPackage where name='ecore'");
pkgs = qry.list();
ecorePkg = pkgs.get(0);
EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
qry = session.createQuery("from EPackage where name='elv'");
pkgs = qry.list();
elvPkg = pkgs.get(0);
EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
tx.commit();

/**
* Fetch all three epackages for reference and modification
*/
tx.begin();
System.out.println("Collection Count:
"+session.getStatistics().getCollectionCount());
System.out.println("Entity Count:
"+session.getStatistics().getEntityCount());

qry = session.createQuery("from EPackage where name='ecore'");
pkgs = qry.list();
ecorePkg = pkgs.get(0);
EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);

qry = session.createQuery("from EPackage where name='elv'");
pkgs = qry.list();
elvPkg = pkgs.get(0);
EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
tx.commit();

tx.begin();

EClass testClass1 = efactory.createEClass();
testClass1.setName("TestClass1");
EAttribute attr1 = efactory.createEAttribute();
attr1.setName("intAttr");
attr1.setEType(epackage.getEInt());
testClass1.getEStructuralFeatures().add(attr1);

session.save(attr1);
session.save(testClass1);
elvPkg.getEClassifiers().add(testClass1);
session.saveOrUpdate(typePkg);
session.saveOrUpdate(ecorePkg);
session.saveOrUpdate(elvPkg);
tx.commit();

session.close();
}

public static HbDataStore createHBDS(EPackage[] epackages)
{
HbDataStore hbds =
(HbDataStore)HbHelper.INSTANCE.createRegisterDataStore(Appli cationProperties.get(EnvironmentConstants.TENEO_DS_NAME));

final Properties props = new Properties();
props.setProperty(Environment.DRIVER,
ApplicationProperties.get(Environment.DRIVER));
props.setProperty(Environment.USER,
ApplicationProperties.get(Environment.USER));
props.setProperty(Environment.URL,
ApplicationProperties.get(Environment.URL));
props.setProperty(Environment.PASS,
ApplicationProperties.get(Environment.PASS));
props.setProperty(Environment.DIALECT,
ApplicationProperties.get(Environment.DIALECT));

hbds.setProperties(props);

hbds.setEPackages(epackages);
hbds.initialize();

return hbds;
}

}
Mdu wrote:
> Hi,
> I'm just about given up with the previous trial-and-error in working
> with EPackages stored via teneo. Is there an example (I also looked
> through Teneo test cases) that demonstrates the following:
>
> 1. Create a dynamic (empty) package and save it using Teneo
> 'session.save()'.
>
> 2. Load the package in 1. properly (registration and all) and add a
> simple EClass to it; save it back to using 'session.save()'.
>
> 3. Load the package again and use it to create an EOBject from a class
> added in 2.; and save the newly created EObject.
>
> 4. Do 1., 2., and 3. in separate test cases?
>
>
> I'm using the recent version of Teneo (1.0.1) and dependent EMF libraries.
> Any help will be much appreciated!
>
> Rgds,
> -Mdu
Re: [Teneo] Example to dynamically modify EPackages stored using Teneo [message #426349 is a reply to message #426347] Mon, 29 December 2008 07:06 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Mdu,
I see that you add the xmltypepackage here.

Is the database empty after the re-initialize? (so is the issue with the querying or with the
database being cleared during reinitialize. In the last case, do you have hibernate.properties in
the root of the classpath which set the update database option of hibernate to create-drop?

Can you enable log4j logging for hibernate (http://www.elver.org/hibernate/samples/log4j.properties)
to see what hibernate does during the re-initialize?

Note that hibernate has some limitations when updating an existing db schema, it will create tables,
columns, foreign keys, but afaik no indexes are created (if you have separate index annotations).

Btw, instead of using one datastore for both the package itself and the data you can also use a
different datastore for the ecorepackage/xmltypepackage (storing the epackages) and the data
(instances of the eclasses of elvpackage). Unless you have references from your data to the model
then one datastore is required.

Hope this helps a bit.

I am on holiday so my answers take a bit longer than normal.

gr. Martin

Mdu wrote:
> Hi All,
> I've gone a little further with a test case. I first run test case
> testStoreEPackages() to create Ecore, XML, and new EPackage 'elv'. I'm
> having a problem in testAddClassToElvEPackage(): please see a block
> comment with PROBLEM: section.
>
> Thanks,
> -Mdu
>
> package com.sbide.das.tests;
>
> import java.util.List;
> import java.util.Properties;
>
> import junit.framework.TestCase;
>
>
> import org.eclipse.emf.ecore.EAttribute;
> import org.eclipse.emf.ecore.EClass;
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.emf.ecore.EPackage;
> import org.eclipse.emf.ecore.EReference;
> import org.eclipse.emf.ecore.EcoreFactory;
> import org.eclipse.emf.ecore.EcorePackage;
> import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
> import org.eclipse.emf.teneo.hibernate.HbDataStore;
> import org.eclipse.emf.teneo.hibernate.HbHelper;
> import org.hibernate.Query;
> import org.hibernate.Session;
> import org.hibernate.SessionFactory;
> import org.hibernate.Transaction;
> import org.hibernate.cfg.Environment;
>
> import com.irg.eaf.common.util.ApplicationProperties;
> import com.sbide.das.EnvironmentConstants;
>
> public class DynamicEPackageTestCase extends TestCase {
>
> public void setUp()
> {
> }
>
> public void testStoreEPackages()
> {
>
>
> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
> final EcorePackage epackage = EcorePackage.eINSTANCE;
> final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;
>
> EPackage.Registry.INSTANCE.put(epackage.getNsURI(), epackage);
> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(), xmlpackage);
>
>
> // create the SchoolBook EClass
> EClass schoolBookEClass = efactory.createEClass();
> schoolBookEClass.setName("SchoolBook");
>
> // create a new attribute for this EClass
> EAttribute level = efactory.createEAttribute();
> level.setName("level");
> level.setEType(epackage.getEInt());
> schoolBookEClass.getEStructuralFeatures().add(level);
>
> // create a course
> EClass courseEClass = efactory.createEClass();
> courseEClass.setName("Course");
>
> // give the Course a name
> EAttribute courseName = efactory.createEAttribute();
> courseName.setName("courseName");
> courseName.setEType(epackage.getEString());
> courseEClass.getEStructuralFeatures().add(courseName);
>
> // A course always uses one SchoolBook
> EReference courseBook = efactory.createEReference();
> courseBook.setName("courseBook");
> courseBook.setEType(schoolBookEClass);
> courseBook.setContainment(false);
> courseEClass.getEStructuralFeatures().add(courseBook);
>
> // Create a new EPackage and add the new EClasses
> EPackage schoolPackage = efactory.createEPackage();
> schoolPackage.setName("elv");
> schoolPackage.setNsPrefix("elv");
> schoolPackage.setNsURI("http:///www.elver.org/School");
> schoolPackage.getEClassifiers().add(courseEClass);
> schoolPackage.getEClassifiers().add(schoolBookEClass);
> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),
> schoolPackage);
>
> // Now reset the epackages in the datastore
> HbDataStore hbds = createHBDS(new
> EPackage[]{epackage,schoolPackage,xmlpackage});
>
> // print the hibernate.hbm.xml for demo purposes
> System.err.println(hbds.getMappingXML());
>
> // and create a course
> EObject course =
> schoolPackage.getEFactoryInstance().create(courseEClass);
> course.eSet(courseName, "Dutch Literature Level 1");
>
> final SessionFactory sessionFactory = hbds.getSessionFactory();
>
> // now persist them all
> Session session = sessionFactory.openSession();
> Transaction tx = session.getTransaction();
> tx.begin();
> session.save(xmlpackage);
> session.save(epackage);
> session.save(schoolPackage);
> session.save(course);
> tx.commit();
>
> session.close();
> }
>
> public void testAddClassToElvEPackage()
> {
>
> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
> final EcorePackage epackage = EcorePackage.eINSTANCE;
>
>
> // Now reset the epackages in the datastore
> HbDataStore hbds = createHBDS(new EPackage[]{epackage});
>
> SessionFactory sessionFactory = hbds.getSessionFactory();
>
> /**
> * Read all relevant epackages to use them for later calling
> setPackages
> */
> Session session = sessionFactory.openSession();
> Transaction tx = session.getTransaction();
> tx.begin();
> Query qry = session.createQuery("from EPackage where name='elv'");
> List<EPackage> pkgs = qry.list();
> EPackage elvPkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
> qry = session.createQuery("from EPackage where name='ecore'");
> pkgs = qry.list();
> EPackage ecorePkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
> qry = session.createQuery("from EPackage where name='type'");
> pkgs = qry.list();
> EPackage typePkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(), typePkg);
> System.out.println("Collection Count:
> "+session.getStatistics().getCollectionCount());
> System.out.println("Entity Count:
> "+session.getStatistics().getEntityCount());
> elvPkg.getEClassifiers().size();
> tx.commit();
>
> /**
> * Re-initialize store
> */
> hbds.setEPackages((new EPackage[]{ecorePkg,elvPkg,typePkg}));
> hbds.initialize();
> sessionFactory = hbds.getSessionFactory();
> session = sessionFactory.openSession();
> tx = session.getTransaction();
>
> /**
> * Re-read epackages - if they are not re-read, they'd be still
> attached to
> * the old session before we called .setEPackages() and
> .initialize():
> *
> * PROBLEM: Each EPackage query below returns empty list
> (qry.list()).
> * QUESTION: 1. I expected these to be still in the database -
> why is the query
> * returning empty results?
> */
> tx.begin();
> qry = session.createQuery("from EPackage where name='type'");
> pkgs = qry.list();
> typePkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
> typePkg);
> qry = session.createQuery("from EPackage where name='ecore'");
> pkgs = qry.list();
> ecorePkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
> qry = session.createQuery("from EPackage where name='elv'");
> pkgs = qry.list();
> elvPkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
> tx.commit();
>
> /**
> * Fetch all three epackages for reference and modification
> */
> tx.begin();
> System.out.println("Collection Count:
> "+session.getStatistics().getCollectionCount());
> System.out.println("Entity Count:
> "+session.getStatistics().getEntityCount());
>
> qry = session.createQuery("from EPackage where name='ecore'");
> pkgs = qry.list();
> ecorePkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>
> qry = session.createQuery("from EPackage where name='elv'");
> pkgs = qry.list();
> elvPkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
> tx.commit();
>
> tx.begin();
>
> EClass testClass1 = efactory.createEClass();
> testClass1.setName("TestClass1");
> EAttribute attr1 = efactory.createEAttribute();
> attr1.setName("intAttr");
> attr1.setEType(epackage.getEInt());
> testClass1.getEStructuralFeatures().add(attr1);
>
> session.save(attr1);
> session.save(testClass1);
> elvPkg.getEClassifiers().add(testClass1);
> session.saveOrUpdate(typePkg);
> session.saveOrUpdate(ecorePkg);
> session.saveOrUpdate(elvPkg);
> tx.commit();
>
> session.close();
> }
>
> public static HbDataStore createHBDS(EPackage[] epackages)
> {
> HbDataStore hbds =
> (HbDataStore)HbHelper.INSTANCE.createRegisterDataStore(Appli cationProperties.get(EnvironmentConstants.TENEO_DS_NAME));
>
>
> final Properties props = new Properties();
> props.setProperty(Environment.DRIVER,
> ApplicationProperties.get(Environment.DRIVER));
> props.setProperty(Environment.USER,
> ApplicationProperties.get(Environment.USER));
> props.setProperty(Environment.URL,
> ApplicationProperties.get(Environment.URL));
> props.setProperty(Environment.PASS,
> ApplicationProperties.get(Environment.PASS));
> props.setProperty(Environment.DIALECT,
> ApplicationProperties.get(Environment.DIALECT));
>
> hbds.setProperties(props);
>
> hbds.setEPackages(epackages);
> hbds.initialize();
>
> return hbds;
> }
>
> }
> Mdu wrote:
>> Hi,
>> I'm just about given up with the previous trial-and-error in working
>> with EPackages stored via teneo. Is there an example (I also looked
>> through Teneo test cases) that demonstrates the following:
>>
>> 1. Create a dynamic (empty) package and save it using Teneo
>> 'session.save()'.
>>
>> 2. Load the package in 1. properly (registration and all) and add a
>> simple EClass to it; save it back to using 'session.save()'.
>>
>> 3. Load the package again and use it to create an EOBject from a class
>> added in 2.; and save the newly created EObject.
>>
>> 4. Do 1., 2., and 3. in separate test cases?
>>
>>
>> I'm using the recent version of Teneo (1.0.1) and dependent EMF
>> libraries.
>> Any help will be much appreciated!
>>
>> Rgds,
>> -Mdu


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] Example to dynamically modify EPackages stored using Teneo [message #426350 is a reply to message #426347] Mon, 29 December 2008 07:06 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Mdu,
I see that you add the xmltypepackage here.

Is the database empty after the re-initialize? (so is the issue with the querying or with the
database being cleared during reinitialize. In the last case, do you have hibernate.properties in
the root of the classpath which set the update database option of hibernate to create-drop?

Can you enable log4j logging for hibernate (http://www.elver.org/hibernate/samples/log4j.properties)
to see what hibernate does during the re-initialize?

Note that hibernate has some limitations when updating an existing db schema, it will create tables,
columns, foreign keys, but afaik no indexes are created (if you have separate index annotations).

Btw, instead of using one datastore for both the package itself and the data you can also use a
different datastore for the ecorepackage/xmltypepackage (storing the epackages) and the data
(instances of the eclasses of elvpackage). Unless you have references from your data to the model
then one datastore is required.

Hope this helps a bit.

I am on holiday so my answers take a bit longer than normal.

gr. Martin

Mdu wrote:
> Hi All,
> I've gone a little further with a test case. I first run test case
> testStoreEPackages() to create Ecore, XML, and new EPackage 'elv'. I'm
> having a problem in testAddClassToElvEPackage(): please see a block
> comment with PROBLEM: section.
>
> Thanks,
> -Mdu
>
> package com.sbide.das.tests;
>
> import java.util.List;
> import java.util.Properties;
>
> import junit.framework.TestCase;
>
>
> import org.eclipse.emf.ecore.EAttribute;
> import org.eclipse.emf.ecore.EClass;
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.emf.ecore.EPackage;
> import org.eclipse.emf.ecore.EReference;
> import org.eclipse.emf.ecore.EcoreFactory;
> import org.eclipse.emf.ecore.EcorePackage;
> import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
> import org.eclipse.emf.teneo.hibernate.HbDataStore;
> import org.eclipse.emf.teneo.hibernate.HbHelper;
> import org.hibernate.Query;
> import org.hibernate.Session;
> import org.hibernate.SessionFactory;
> import org.hibernate.Transaction;
> import org.hibernate.cfg.Environment;
>
> import com.irg.eaf.common.util.ApplicationProperties;
> import com.sbide.das.EnvironmentConstants;
>
> public class DynamicEPackageTestCase extends TestCase {
>
> public void setUp()
> {
> }
>
> public void testStoreEPackages()
> {
>
>
> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
> final EcorePackage epackage = EcorePackage.eINSTANCE;
> final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;
>
> EPackage.Registry.INSTANCE.put(epackage.getNsURI(), epackage);
> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(), xmlpackage);
>
>
> // create the SchoolBook EClass
> EClass schoolBookEClass = efactory.createEClass();
> schoolBookEClass.setName("SchoolBook");
>
> // create a new attribute for this EClass
> EAttribute level = efactory.createEAttribute();
> level.setName("level");
> level.setEType(epackage.getEInt());
> schoolBookEClass.getEStructuralFeatures().add(level);
>
> // create a course
> EClass courseEClass = efactory.createEClass();
> courseEClass.setName("Course");
>
> // give the Course a name
> EAttribute courseName = efactory.createEAttribute();
> courseName.setName("courseName");
> courseName.setEType(epackage.getEString());
> courseEClass.getEStructuralFeatures().add(courseName);
>
> // A course always uses one SchoolBook
> EReference courseBook = efactory.createEReference();
> courseBook.setName("courseBook");
> courseBook.setEType(schoolBookEClass);
> courseBook.setContainment(false);
> courseEClass.getEStructuralFeatures().add(courseBook);
>
> // Create a new EPackage and add the new EClasses
> EPackage schoolPackage = efactory.createEPackage();
> schoolPackage.setName("elv");
> schoolPackage.setNsPrefix("elv");
> schoolPackage.setNsURI("http:///www.elver.org/School");
> schoolPackage.getEClassifiers().add(courseEClass);
> schoolPackage.getEClassifiers().add(schoolBookEClass);
> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),
> schoolPackage);
>
> // Now reset the epackages in the datastore
> HbDataStore hbds = createHBDS(new
> EPackage[]{epackage,schoolPackage,xmlpackage});
>
> // print the hibernate.hbm.xml for demo purposes
> System.err.println(hbds.getMappingXML());
>
> // and create a course
> EObject course =
> schoolPackage.getEFactoryInstance().create(courseEClass);
> course.eSet(courseName, "Dutch Literature Level 1");
>
> final SessionFactory sessionFactory = hbds.getSessionFactory();
>
> // now persist them all
> Session session = sessionFactory.openSession();
> Transaction tx = session.getTransaction();
> tx.begin();
> session.save(xmlpackage);
> session.save(epackage);
> session.save(schoolPackage);
> session.save(course);
> tx.commit();
>
> session.close();
> }
>
> public void testAddClassToElvEPackage()
> {
>
> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
> final EcorePackage epackage = EcorePackage.eINSTANCE;
>
>
> // Now reset the epackages in the datastore
> HbDataStore hbds = createHBDS(new EPackage[]{epackage});
>
> SessionFactory sessionFactory = hbds.getSessionFactory();
>
> /**
> * Read all relevant epackages to use them for later calling
> setPackages
> */
> Session session = sessionFactory.openSession();
> Transaction tx = session.getTransaction();
> tx.begin();
> Query qry = session.createQuery("from EPackage where name='elv'");
> List<EPackage> pkgs = qry.list();
> EPackage elvPkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
> qry = session.createQuery("from EPackage where name='ecore'");
> pkgs = qry.list();
> EPackage ecorePkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
> qry = session.createQuery("from EPackage where name='type'");
> pkgs = qry.list();
> EPackage typePkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(), typePkg);
> System.out.println("Collection Count:
> "+session.getStatistics().getCollectionCount());
> System.out.println("Entity Count:
> "+session.getStatistics().getEntityCount());
> elvPkg.getEClassifiers().size();
> tx.commit();
>
> /**
> * Re-initialize store
> */
> hbds.setEPackages((new EPackage[]{ecorePkg,elvPkg,typePkg}));
> hbds.initialize();
> sessionFactory = hbds.getSessionFactory();
> session = sessionFactory.openSession();
> tx = session.getTransaction();
>
> /**
> * Re-read epackages - if they are not re-read, they'd be still
> attached to
> * the old session before we called .setEPackages() and
> .initialize():
> *
> * PROBLEM: Each EPackage query below returns empty list
> (qry.list()).
> * QUESTION: 1. I expected these to be still in the database -
> why is the query
> * returning empty results?
> */
> tx.begin();
> qry = session.createQuery("from EPackage where name='type'");
> pkgs = qry.list();
> typePkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
> typePkg);
> qry = session.createQuery("from EPackage where name='ecore'");
> pkgs = qry.list();
> ecorePkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
> qry = session.createQuery("from EPackage where name='elv'");
> pkgs = qry.list();
> elvPkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
> tx.commit();
>
> /**
> * Fetch all three epackages for reference and modification
> */
> tx.begin();
> System.out.println("Collection Count:
> "+session.getStatistics().getCollectionCount());
> System.out.println("Entity Count:
> "+session.getStatistics().getEntityCount());
>
> qry = session.createQuery("from EPackage where name='ecore'");
> pkgs = qry.list();
> ecorePkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>
> qry = session.createQuery("from EPackage where name='elv'");
> pkgs = qry.list();
> elvPkg = pkgs.get(0);
> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
> tx.commit();
>
> tx.begin();
>
> EClass testClass1 = efactory.createEClass();
> testClass1.setName("TestClass1");
> EAttribute attr1 = efactory.createEAttribute();
> attr1.setName("intAttr");
> attr1.setEType(epackage.getEInt());
> testClass1.getEStructuralFeatures().add(attr1);
>
> session.save(attr1);
> session.save(testClass1);
> elvPkg.getEClassifiers().add(testClass1);
> session.saveOrUpdate(typePkg);
> session.saveOrUpdate(ecorePkg);
> session.saveOrUpdate(elvPkg);
> tx.commit();
>
> session.close();
> }
>
> public static HbDataStore createHBDS(EPackage[] epackages)
> {
> HbDataStore hbds =
> (HbDataStore)HbHelper.INSTANCE.createRegisterDataStore(Appli cationProperties.get(EnvironmentConstants.TENEO_DS_NAME));
>
>
> final Properties props = new Properties();
> props.setProperty(Environment.DRIVER,
> ApplicationProperties.get(Environment.DRIVER));
> props.setProperty(Environment.USER,
> ApplicationProperties.get(Environment.USER));
> props.setProperty(Environment.URL,
> ApplicationProperties.get(Environment.URL));
> props.setProperty(Environment.PASS,
> ApplicationProperties.get(Environment.PASS));
> props.setProperty(Environment.DIALECT,
> ApplicationProperties.get(Environment.DIALECT));
>
> hbds.setProperties(props);
>
> hbds.setEPackages(epackages);
> hbds.initialize();
>
> return hbds;
> }
>
> }
> Mdu wrote:
>> Hi,
>> I'm just about given up with the previous trial-and-error in working
>> with EPackages stored via teneo. Is there an example (I also looked
>> through Teneo test cases) that demonstrates the following:
>>
>> 1. Create a dynamic (empty) package and save it using Teneo
>> 'session.save()'.
>>
>> 2. Load the package in 1. properly (registration and all) and add a
>> simple EClass to it; save it back to using 'session.save()'.
>>
>> 3. Load the package again and use it to create an EOBject from a class
>> added in 2.; and save the newly created EObject.
>>
>> 4. Do 1., 2., and 3. in separate test cases?
>>
>>
>> I'm using the recent version of Teneo (1.0.1) and dependent EMF
>> libraries.
>> Any help will be much appreciated!
>>
>> Rgds,
>> -Mdu


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] Example to dynamically modify EPackages stored using Teneo [message #426358 is a reply to message #426349] Mon, 29 December 2008 14:19 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mduduzi.keswa.isizwe.com

Thanks Martin,
I turned on debugging and used create-drop option. Part of the log
shows:


4476 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
if exists `eannotation_contents`
4479 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
if exists `eannotation_references`
4480 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
if exists `eclass_esupertypes`
4483 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
if exists `ecore_eobject`
4485 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
if exists `eoperation_eexceptions`
4486 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
if exists `ereference_ekeys`
4488 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
if exists details


Questions:
1. Since I'm neither changing EcorePackage('ecore') nor
XMLTypePackage('type') - only modifying package 'elv', why do I have to
re-initialize HbDataStore?


2. It makes sense to store EcorePackage and XMLTypePackage in a separate
HBDS as the are static, I still need to update custom EPackages (e.g.
'elv') which refer to these two packages. How do I accomplish this
without running into re-initialize issues.


Rgds,
-Mdu
Martin Taal wrote:
> Hi Mdu,
> I see that you add the xmltypepackage here.
>
> Is the database empty after the re-initialize? (so is the issue with the
> querying or with the database being cleared during reinitialize. In the
> last case, do you have hibernate.properties in the root of the classpath
> which set the update database option of hibernate to create-drop?
>
> Can you enable log4j logging for hibernate
> (http://www.elver.org/hibernate/samples/log4j.properties) to see what
> hibernate does during the re-initialize?
>
> Note that hibernate has some limitations when updating an existing db
> schema, it will create tables, columns, foreign keys, but afaik no
> indexes are created (if you have separate index annotations).
>
> Btw, instead of using one datastore for both the package itself and the
> data you can also use a different datastore for the
> ecorepackage/xmltypepackage (storing the epackages) and the data
> (instances of the eclasses of elvpackage). Unless you have references
> from your data to the model then one datastore is required.
>
> Hope this helps a bit.
>
> I am on holiday so my answers take a bit longer than normal.
>
> gr. Martin
>
> Mdu wrote:
>> Hi All,
>> I've gone a little further with a test case. I first run test case
>> testStoreEPackages() to create Ecore, XML, and new EPackage 'elv'. I'm
>> having a problem in testAddClassToElvEPackage(): please see a block
>> comment with PROBLEM: section.
>>
>> Thanks,
>> -Mdu
>>
>> package com.sbide.das.tests;
>>
>> import java.util.List;
>> import java.util.Properties;
>>
>> import junit.framework.TestCase;
>>
>>
>> import org.eclipse.emf.ecore.EAttribute;
>> import org.eclipse.emf.ecore.EClass;
>> import org.eclipse.emf.ecore.EObject;
>> import org.eclipse.emf.ecore.EPackage;
>> import org.eclipse.emf.ecore.EReference;
>> import org.eclipse.emf.ecore.EcoreFactory;
>> import org.eclipse.emf.ecore.EcorePackage;
>> import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
>> import org.eclipse.emf.teneo.hibernate.HbDataStore;
>> import org.eclipse.emf.teneo.hibernate.HbHelper;
>> import org.hibernate.Query;
>> import org.hibernate.Session;
>> import org.hibernate.SessionFactory;
>> import org.hibernate.Transaction;
>> import org.hibernate.cfg.Environment;
>>
>> import com.irg.eaf.common.util.ApplicationProperties;
>> import com.sbide.das.EnvironmentConstants;
>>
>> public class DynamicEPackageTestCase extends TestCase {
>>
>> public void setUp()
>> {
>> }
>>
>> public void testStoreEPackages()
>> {
>> final EcoreFactory efactory =
>> EcoreFactory.eINSTANCE;
>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>> final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;
>> EPackage.Registry.INSTANCE.put(epackage.getNsURI(),
>> epackage);
>> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),
>> xmlpackage);
>>
>> // create the SchoolBook EClass
>> EClass schoolBookEClass = efactory.createEClass();
>> schoolBookEClass.setName("SchoolBook");
>> // create a new attribute for this EClass
>> EAttribute level = efactory.createEAttribute();
>> level.setName("level");
>> level.setEType(epackage.getEInt());
>> schoolBookEClass.getEStructuralFeatures().add(level);
>>
>> // create a course
>> EClass courseEClass = efactory.createEClass();
>> courseEClass.setName("Course");
>> // give the Course a name
>> EAttribute courseName = efactory.createEAttribute();
>> courseName.setName("courseName");
>> courseName.setEType(epackage.getEString());
>> courseEClass.getEStructuralFeatures().add(courseName);
>> // A course always uses one SchoolBook
>> EReference courseBook = efactory.createEReference();
>> courseBook.setName("courseBook");
>> courseBook.setEType(schoolBookEClass);
>> courseBook.setContainment(false);
>> courseEClass.getEStructuralFeatures().add(courseBook);
>> // Create a new EPackage and add the new EClasses
>> EPackage schoolPackage = efactory.createEPackage();
>> schoolPackage.setName("elv");
>> schoolPackage.setNsPrefix("elv");
>> schoolPackage.setNsURI("http:///www.elver.org/School");
>> schoolPackage.getEClassifiers().add(courseEClass);
>> schoolPackage.getEClassifiers().add(schoolBookEClass);
>> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),
>> schoolPackage);
>> // Now reset the epackages in the datastore
>> HbDataStore hbds = createHBDS(new
>> EPackage[]{epackage,schoolPackage,xmlpackage});
>> // print the hibernate.hbm.xml for demo purposes
>> System.err.println(hbds.getMappingXML());
>> // and create a course
>> EObject course =
>> schoolPackage.getEFactoryInstance().create(courseEClass);
>> course.eSet(courseName, "Dutch Literature Level 1");
>> final SessionFactory sessionFactory =
>> hbds.getSessionFactory();
>> // now persist them all
>> Session session = sessionFactory.openSession();
>> Transaction tx = session.getTransaction();
>> tx.begin();
>> session.save(xmlpackage);
>> session.save(epackage);
>> session.save(schoolPackage);
>> session.save(course);
>> tx.commit();
>>
>> session.close();
>> }
>> public void testAddClassToElvEPackage()
>> {
>>
>> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>
>> // Now reset the epackages in the datastore
>> HbDataStore hbds = createHBDS(new EPackage[]{epackage});
>>
>> SessionFactory sessionFactory = hbds.getSessionFactory();
>> /**
>> * Read all relevant epackages to use them for later calling
>> setPackages
>> */
>> Session session = sessionFactory.openSession();
>> Transaction tx = session.getTransaction();
>> tx.begin();
>> Query qry = session.createQuery("from EPackage where
>> name='elv'");
>> List<EPackage> pkgs = qry.list();
>> EPackage elvPkg = pkgs.get(0);
>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>> qry = session.createQuery("from EPackage where name='ecore'");
>> pkgs = qry.list();
>> EPackage ecorePkg = pkgs.get(0);
>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>> qry = session.createQuery("from EPackage where name='type'");
>> pkgs = qry.list();
>> EPackage typePkg = pkgs.get(0);
>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(), typePkg);
>> System.out.println("Collection Count:
>> "+session.getStatistics().getCollectionCount());
>> System.out.println("Entity Count:
>> "+session.getStatistics().getEntityCount());
>> elvPkg.getEClassifiers().size();
>> tx.commit();
>> /**
>> * Re-initialize store
>> */
>> hbds.setEPackages((new
>> EPackage[]{ecorePkg,elvPkg,typePkg})); hbds.initialize();
>> sessionFactory = hbds.getSessionFactory(); session =
>> sessionFactory.openSession();
>> tx = session.getTransaction();
>> /**
>> * Re-read epackages - if they are not re-read, they'd be
>> still attached to
>> * the old session before we called .setEPackages() and
>> .initialize():
>> *
>> * PROBLEM: Each EPackage query below returns empty list
>> (qry.list()).
>> * QUESTION: 1. I expected these to be still in the database -
>> why is the query
>> * returning empty results?
>> */
>> tx.begin();
>> qry = session.createQuery("from EPackage where name='type'");
>> pkgs = qry.list();
>> typePkg = pkgs.get(0);
>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(), typePkg);
>> qry = session.createQuery("from EPackage where name='ecore'");
>> pkgs = qry.list();
>> ecorePkg = pkgs.get(0);
>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>> qry = session.createQuery("from EPackage where name='elv'");
>> pkgs = qry.list();
>> elvPkg = pkgs.get(0);
>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(),
>> elvPkg); tx.commit();
>> /**
>> * Fetch all three epackages for reference and modification
>> */
>> tx.begin();
>> System.out.println("Collection Count:
>> "+session.getStatistics().getCollectionCount());
>> System.out.println("Entity Count:
>> "+session.getStatistics().getEntityCount());
>> qry = session.createQuery("from EPackage where name='ecore'");
>> pkgs = qry.list();
>> ecorePkg = pkgs.get(0);
>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>> qry = session.createQuery("from EPackage where name='elv'");
>> pkgs = qry.list();
>> elvPkg = pkgs.get(0);
>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>> tx.commit(); tx.begin();
>> EClass testClass1 = efactory.createEClass();
>> testClass1.setName("TestClass1");
>> EAttribute attr1 = efactory.createEAttribute();
>> attr1.setName("intAttr");
>> attr1.setEType(epackage.getEInt());
>> testClass1.getEStructuralFeatures().add(attr1);
>> session.save(attr1);
>> session.save(testClass1);
>> elvPkg.getEClassifiers().add(testClass1);
>> session.saveOrUpdate(typePkg);
>> session.saveOrUpdate(ecorePkg);
>> session.saveOrUpdate(elvPkg); tx.commit();
>>
>> session.close();
>> } public static HbDataStore createHBDS(EPackage[]
>> epackages)
>> { HbDataStore hbds =
>> (HbDataStore)HbHelper.INSTANCE.createRegisterDataStore(Appli cationProperties.get(EnvironmentConstants.TENEO_DS_NAME));
>>
>>
>> final Properties props = new Properties();
>> props.setProperty(Environment.DRIVER,
>> ApplicationProperties.get(Environment.DRIVER));
>> props.setProperty(Environment.USER,
>> ApplicationProperties.get(Environment.USER));
>> props.setProperty(Environment.URL,
>> ApplicationProperties.get(Environment.URL));
>> props.setProperty(Environment.PASS,
>> ApplicationProperties.get(Environment.PASS));
>> props.setProperty(Environment.DIALECT,
>> ApplicationProperties.get(Environment.DIALECT));
>> hbds.setProperties(props);
>> hbds.setEPackages(epackages);
>> hbds.initialize();
>> return hbds;
>> }
>> }
>> Mdu wrote:
>>> Hi,
>>> I'm just about given up with the previous trial-and-error in
>>> working with EPackages stored via teneo. Is there an example (I also
>>> looked through Teneo test cases) that demonstrates the following:
>>>
>>> 1. Create a dynamic (empty) package and save it using Teneo
>>> 'session.save()'.
>>>
>>> 2. Load the package in 1. properly (registration and all) and add a
>>> simple EClass to it; save it back to using 'session.save()'.
>>>
>>> 3. Load the package again and use it to create an EOBject from a
>>> class added in 2.; and save the newly created EObject.
>>>
>>> 4. Do 1., 2., and 3. in separate test cases?
>>>
>>>
>>> I'm using the recent version of Teneo (1.0.1) and dependent EMF
>>> libraries.
>>> Any help will be much appreciated!
>>>
>>> Rgds,
>>> -Mdu
>
>
Re: [Teneo] Example to dynamically modify EPackages stored using Teneo [message #426367 is a reply to message #426358] Tue, 30 December 2008 20:50 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Mdu,
Create-drop will drop the database when re-initializing the datastore, so in your case I would use
update.

Answers:
1) I did not completely understand the question, if the elv package changes then new tables/columns
need to be created this is done when (re-)initializing the datastore.
2) You can use 2 datastores (one for the ecore/xml package and one for the elv package) if there are
no direct references from elv to ecore. With direct I mean that you did not specify in your model an
ereference which references the EClass type in the ecore package (or eattribute, or ereference).
Re-initializating should not be a problem with the hibernate update option.

gr. Martin

Mdu wrote:
> Thanks Martin,
> I turned on debugging and used create-drop option. Part of the log
> shows:
>
>
> 4476 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
> if exists `eannotation_contents`
> 4479 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
> if exists `eannotation_references`
> 4480 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
> if exists `eclass_esupertypes`
> 4483 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
> if exists `ecore_eobject`
> 4485 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
> if exists `eoperation_eexceptions`
> 4486 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
> if exists `ereference_ekeys`
> 4488 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table
> if exists details
>
>
> Questions:
> 1. Since I'm neither changing EcorePackage('ecore') nor
> XMLTypePackage('type') - only modifying package 'elv', why do I have to
> re-initialize HbDataStore?
>
>
> 2. It makes sense to store EcorePackage and XMLTypePackage in a separate
> HBDS as the are static, I still need to update custom EPackages (e.g.
> 'elv') which refer to these two packages. How do I accomplish this
> without running into re-initialize issues.
>
>
> Rgds,
> -Mdu
> Martin Taal wrote:
>> Hi Mdu,
>> I see that you add the xmltypepackage here.
>>
>> Is the database empty after the re-initialize? (so is the issue with
>> the querying or with the database being cleared during reinitialize.
>> In the last case, do you have hibernate.properties in the root of the
>> classpath which set the update database option of hibernate to
>> create-drop?
>>
>> Can you enable log4j logging for hibernate
>> (http://www.elver.org/hibernate/samples/log4j.properties) to see what
>> hibernate does during the re-initialize?
>>
>> Note that hibernate has some limitations when updating an existing db
>> schema, it will create tables, columns, foreign keys, but afaik no
>> indexes are created (if you have separate index annotations).
>>
>> Btw, instead of using one datastore for both the package itself and
>> the data you can also use a different datastore for the
>> ecorepackage/xmltypepackage (storing the epackages) and the data
>> (instances of the eclasses of elvpackage). Unless you have references
>> from your data to the model then one datastore is required.
>>
>> Hope this helps a bit.
>>
>> I am on holiday so my answers take a bit longer than normal.
>>
>> gr. Martin
>>
>> Mdu wrote:
>>> Hi All,
>>> I've gone a little further with a test case. I first run test case
>>> testStoreEPackages() to create Ecore, XML, and new EPackage 'elv'.
>>> I'm having a problem in testAddClassToElvEPackage(): please see a
>>> block comment with PROBLEM: section.
>>>
>>> Thanks,
>>> -Mdu
>>>
>>> package com.sbide.das.tests;
>>>
>>> import java.util.List;
>>> import java.util.Properties;
>>>
>>> import junit.framework.TestCase;
>>>
>>>
>>> import org.eclipse.emf.ecore.EAttribute;
>>> import org.eclipse.emf.ecore.EClass;
>>> import org.eclipse.emf.ecore.EObject;
>>> import org.eclipse.emf.ecore.EPackage;
>>> import org.eclipse.emf.ecore.EReference;
>>> import org.eclipse.emf.ecore.EcoreFactory;
>>> import org.eclipse.emf.ecore.EcorePackage;
>>> import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
>>> import org.eclipse.emf.teneo.hibernate.HbDataStore;
>>> import org.eclipse.emf.teneo.hibernate.HbHelper;
>>> import org.hibernate.Query;
>>> import org.hibernate.Session;
>>> import org.hibernate.SessionFactory;
>>> import org.hibernate.Transaction;
>>> import org.hibernate.cfg.Environment;
>>>
>>> import com.irg.eaf.common.util.ApplicationProperties;
>>> import com.sbide.das.EnvironmentConstants;
>>>
>>> public class DynamicEPackageTestCase extends TestCase {
>>>
>>> public void setUp()
>>> {
>>> }
>>>
>>> public void testStoreEPackages()
>>> {
>>> final EcoreFactory efactory =
>>> EcoreFactory.eINSTANCE;
>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>> final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;
>>> EPackage.Registry.INSTANCE.put(epackage.getNsURI(),
>>> epackage);
>>> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),
>>> xmlpackage);
>>> // create the SchoolBook EClass
>>> EClass schoolBookEClass = efactory.createEClass();
>>> schoolBookEClass.setName("SchoolBook");
>>> // create a new attribute for this EClass
>>> EAttribute level = efactory.createEAttribute();
>>> level.setName("level");
>>> level.setEType(epackage.getEInt());
>>> schoolBookEClass.getEStructuralFeatures().add(level);
>>>
>>> // create a course
>>> EClass courseEClass = efactory.createEClass();
>>> courseEClass.setName("Course");
>>> // give the Course a name
>>> EAttribute courseName = efactory.createEAttribute();
>>> courseName.setName("courseName");
>>> courseName.setEType(epackage.getEString());
>>> courseEClass.getEStructuralFeatures().add(courseName);
>>> // A course always uses one SchoolBook
>>> EReference courseBook = efactory.createEReference();
>>> courseBook.setName("courseBook");
>>> courseBook.setEType(schoolBookEClass);
>>> courseBook.setContainment(false);
>>> courseEClass.getEStructuralFeatures().add(courseBook);
>>> // Create a new EPackage and add the new EClasses
>>> EPackage schoolPackage = efactory.createEPackage();
>>> schoolPackage.setName("elv");
>>> schoolPackage.setNsPrefix("elv");
>>> schoolPackage.setNsURI("http:///www.elver.org/School");
>>> schoolPackage.getEClassifiers().add(courseEClass);
>>> schoolPackage.getEClassifiers().add(schoolBookEClass);
>>> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),
>>> schoolPackage);
>>> // Now reset the epackages in the datastore
>>> HbDataStore hbds = createHBDS(new
>>> EPackage[]{epackage,schoolPackage,xmlpackage});
>>> // print the hibernate.hbm.xml for demo purposes
>>> System.err.println(hbds.getMappingXML());
>>> // and create a course
>>> EObject course =
>>> schoolPackage.getEFactoryInstance().create(courseEClass);
>>> course.eSet(courseName, "Dutch Literature Level 1");
>>> final SessionFactory sessionFactory =
>>> hbds.getSessionFactory();
>>> // now persist them all
>>> Session session = sessionFactory.openSession();
>>> Transaction tx = session.getTransaction();
>>> tx.begin();
>>> session.save(xmlpackage);
>>> session.save(epackage);
>>> session.save(schoolPackage);
>>> session.save(course);
>>> tx.commit();
>>>
>>> session.close();
>>> }
>>> public void testAddClassToElvEPackage()
>>> {
>>>
>>> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>> // Now reset the epackages in the datastore
>>> HbDataStore hbds = createHBDS(new EPackage[]{epackage});
>>>
>>> SessionFactory sessionFactory = hbds.getSessionFactory();
>>> /**
>>> * Read all relevant epackages to use them for later calling
>>> setPackages
>>> */
>>> Session session = sessionFactory.openSession();
>>> Transaction tx = session.getTransaction();
>>> tx.begin();
>>> Query qry = session.createQuery("from EPackage where
>>> name='elv'");
>>> List<EPackage> pkgs = qry.list();
>>> EPackage elvPkg = pkgs.get(0);
>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>> qry = session.createQuery("from EPackage where name='ecore'");
>>> pkgs = qry.list();
>>> EPackage ecorePkg = pkgs.get(0);
>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>> qry = session.createQuery("from EPackage where name='type'");
>>> pkgs = qry.list();
>>> EPackage typePkg = pkgs.get(0);
>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>> typePkg); System.out.println("Collection Count:
>>> "+session.getStatistics().getCollectionCount());
>>> System.out.println("Entity Count:
>>> "+session.getStatistics().getEntityCount());
>>> elvPkg.getEClassifiers().size();
>>> tx.commit();
>>> /**
>>> * Re-initialize store
>>> */
>>> hbds.setEPackages((new
>>> EPackage[]{ecorePkg,elvPkg,typePkg})); hbds.initialize();
>>> sessionFactory = hbds.getSessionFactory(); session
>>> = sessionFactory.openSession();
>>> tx = session.getTransaction();
>>> /**
>>> * Re-read epackages - if they are not re-read, they'd be
>>> still attached to
>>> * the old session before we called .setEPackages() and
>>> .initialize():
>>> *
>>> * PROBLEM: Each EPackage query below returns empty list
>>> (qry.list()).
>>> * QUESTION: 1. I expected these to be still in the database
>>> - why is the query
>>> * returning empty results?
>>> */
>>> tx.begin();
>>> qry = session.createQuery("from EPackage where name='type'");
>>> pkgs = qry.list();
>>> typePkg = pkgs.get(0);
>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>> typePkg); qry = session.createQuery("from EPackage
>>> where name='ecore'");
>>> pkgs = qry.list();
>>> ecorePkg = pkgs.get(0);
>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>> qry = session.createQuery("from EPackage where name='elv'");
>>> pkgs = qry.list();
>>> elvPkg = pkgs.get(0);
>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(),
>>> elvPkg); tx.commit();
>>> /**
>>> * Fetch all three epackages for reference and modification
>>> */
>>> tx.begin();
>>> System.out.println("Collection Count:
>>> "+session.getStatistics().getCollectionCount());
>>> System.out.println("Entity Count:
>>> "+session.getStatistics().getEntityCount());
>>> qry = session.createQuery("from EPackage where name='ecore'");
>>> pkgs = qry.list();
>>> ecorePkg = pkgs.get(0);
>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>> qry = session.createQuery("from EPackage where name='elv'");
>>> pkgs = qry.list();
>>> elvPkg = pkgs.get(0);
>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>> tx.commit(); tx.begin();
>>> EClass testClass1 = efactory.createEClass();
>>> testClass1.setName("TestClass1");
>>> EAttribute attr1 = efactory.createEAttribute();
>>> attr1.setName("intAttr");
>>> attr1.setEType(epackage.getEInt());
>>> testClass1.getEStructuralFeatures().add(attr1);
>>> session.save(attr1);
>>> session.save(testClass1);
>>> elvPkg.getEClassifiers().add(testClass1);
>>> session.saveOrUpdate(typePkg);
>>> session.saveOrUpdate(ecorePkg);
>>> session.saveOrUpdate(elvPkg); tx.commit();
>>>
>>> session.close();
>>> } public static HbDataStore createHBDS(EPackage[]
>>> epackages)
>>> { HbDataStore hbds =
>>> (HbDataStore)HbHelper.INSTANCE.createRegisterDataStore(Appli cationProperties.get(EnvironmentConstants.TENEO_DS_NAME));
>>>
>>>
>>> final Properties props = new Properties();
>>> props.setProperty(Environment.DRIVER,
>>> ApplicationProperties.get(Environment.DRIVER));
>>> props.setProperty(Environment.USER,
>>> ApplicationProperties.get(Environment.USER));
>>> props.setProperty(Environment.URL,
>>> ApplicationProperties.get(Environment.URL));
>>> props.setProperty(Environment.PASS,
>>> ApplicationProperties.get(Environment.PASS));
>>> props.setProperty(Environment.DIALECT,
>>> ApplicationProperties.get(Environment.DIALECT));
>>> hbds.setProperties(props);
>>> hbds.setEPackages(epackages);
>>> hbds.initialize();
>>> return hbds;
>>> } }
>>> Mdu wrote:
>>>> Hi,
>>>> I'm just about given up with the previous trial-and-error in
>>>> working with EPackages stored via teneo. Is there an example (I also
>>>> looked through Teneo test cases) that demonstrates the following:
>>>>
>>>> 1. Create a dynamic (empty) package and save it using Teneo
>>>> 'session.save()'.
>>>>
>>>> 2. Load the package in 1. properly (registration and all) and add a
>>>> simple EClass to it; save it back to using 'session.save()'.
>>>>
>>>> 3. Load the package again and use it to create an EOBject from a
>>>> class added in 2.; and save the newly created EObject.
>>>>
>>>> 4. Do 1., 2., and 3. in separate test cases?
>>>>
>>>>
>>>> I'm using the recent version of Teneo (1.0.1) and dependent EMF
>>>> libraries.
>>>> Any help will be much appreciated!
>>>>
>>>> Rgds,
>>>> -Mdu
>>
>>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] Example to dynamically modify EPackages stored using Teneo [message #426369 is a reply to message #426367] Wed, 31 December 2008 01:02 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mduduzi.keswa.isizwe.com

Hi Martin - thanks for taking the time form your well deserved break!
See my comments below:
Martin Taal wrote:
> Hi Mdu,
> Create-drop will drop the database when re-initializing the datastore,
> so in your case I would use update.
I tried my 'update' option - and still have the same problem of a query
that returns no epackages after re-initialize; and also got the
following errors during re-initalize:

51027 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter
table `eobject` add index emodelelement_eannotations
(`eannotation_emodelelement_e_id`), add constraint
emodelelement_eannotations foreign key
(`eannotation_emodelelement_e_id`) references `eobject` (e_id)
51209 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate -
Unsuccessful: alter table `eobject` add index emodelelement_eannotations
(`eannotation_emodelelement_e_id`), add constraint
emodelelement_eannotations foreign key
(`eannotation_emodelelement_e_id`) references `eobject` (e_id)
51209 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - Can't
create table 'solutionassembly.#sql-1208_1c' (errno: 121)
>
> Answers:
> 1) I did not completely understand the question, if the elv package
> changes then new tables/columns need to be created this is done when
> (re-)initializing the datastore.
> 2) You can use 2 datastores (one for the ecore/xml package and one for
> the elv package) if there are no direct references from elv to ecore.
> With direct I mean that you did not specify in your model an ereference
> which references the EClass type in the ecore package (or eattribute, or
> ereference). Re-initializating should not be a problem with the
> hibernate update option.
This is where my ignorance shows a lot. Here's my understand how what
you are suggesting would work:

Let DS_Elv be data store for Elv package;
DS_Ecore be data store for Ecore and XMLType packages
HBDS_Elv be HB data source for DS_Elv
HBDS_Ecore be HB data source for DS_Ecore
Ssn_Elv be session from HBDS_Elv
Ssn_Ecore be session from HBDS_Ecore
ecorePkg and xmlTypePkg be persistent packages saved and loaded via
Ssn_Ecore

1. Create HBDS_Elv:
- HBDS_Elv.setEPackages({*}) - should * be ecorePkg and xmlTypePkg
from Ssn_Ecore?
- HBDS_Elv.initialize()

2. Create and save Elv package 'elvPkg':
- Obtain Ssn_Elv from HBDS_Elv sessionFactory
- Create elvPkg and class 'class1' with 'attr1'
- Transactionally call Ssn_Elv.save(elvPkg)
QUESTION: Doesn't this require persistent ecorePkg and xmlTypePkg
implicilty to save 'class1' and 'attr1' i.e. instances from Ssn_Ecore.
If so, wouldn't I get errors when I save a elvPkg that implicitly
depends on epackages loaded from Ssn_Ecore - a separate session?

3. Read elvPkg back for modification:
- HBDS_Elv.setEPackages({**}) - should ** be same ecorePkg and
xmlTypePkg from Ssn_Ecore?
- HBDS_Elv.initialize()
- Create query 'from EPackage where name='Elv'
- Assign elvPkg to the result
- Add another 'class2' with 'attr2' to elvPkg
- Transactionally call Ssn_Elv.save(elvPkg)
QUESTION: Same question as above

The only way I could see this working from here is by:
Everytime I change elvPkg, I first have to read it; clone it using
elvPkgClone = EcoreUtil.copy(); modify it; HBDS_Env.initialize(); and
Ssn_Elv.save(elvPkgClone).

Unless I fix my Hibernate (errno: 121) errors - assuming that's what
preventing 'update' from working; this is my only choice. I also realize
that I'm starting to meddle 'model evolution' issues here as well - that
is how to evolve metamodels to be always in sync with their underlying
models. I noticed that this has been discussed on CDO forums a bit.

Regards,
-Mdu

>
> gr. Martin
>
> Mdu wrote:
>> Thanks Martin,
>> I turned on debugging and used create-drop option. Part of the log
>> shows:
>>
>>
>> 4476 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>> table if exists `eannotation_contents`
>> 4479 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>> table if exists `eannotation_references`
>> 4480 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>> table if exists `eclass_esupertypes`
>> 4483 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>> table if exists `ecore_eobject`
>> 4485 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>> table if exists `eoperation_eexceptions`
>> 4486 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>> table if exists `ereference_ekeys`
>> 4488 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>> table if exists details
>>
>>
>> Questions:
>> 1. Since I'm neither changing EcorePackage('ecore') nor
>> XMLTypePackage('type') - only modifying package 'elv', why do I have
>> to re-initialize HbDataStore?
>>
>>
>> 2. It makes sense to store EcorePackage and XMLTypePackage in a
>> separate HBDS as the are static, I still need to update custom
>> EPackages (e.g. 'elv') which refer to these two packages. How do I
>> accomplish this without running into re-initialize issues.
>>
>>
>> Rgds,
>> -Mdu
>> Martin Taal wrote:
>>> Hi Mdu,
>>> I see that you add the xmltypepackage here.
>>>
>>> Is the database empty after the re-initialize? (so is the issue with
>>> the querying or with the database being cleared during reinitialize.
>>> In the last case, do you have hibernate.properties in the root of the
>>> classpath which set the update database option of hibernate to
>>> create-drop?
>>>
>>> Can you enable log4j logging for hibernate
>>> (http://www.elver.org/hibernate/samples/log4j.properties) to see what
>>> hibernate does during the re-initialize?
>>>
>>> Note that hibernate has some limitations when updating an existing db
>>> schema, it will create tables, columns, foreign keys, but afaik no
>>> indexes are created (if you have separate index annotations).
>>>
>>> Btw, instead of using one datastore for both the package itself and
>>> the data you can also use a different datastore for the
>>> ecorepackage/xmltypepackage (storing the epackages) and the data
>>> (instances of the eclasses of elvpackage). Unless you have references
>>> from your data to the model then one datastore is required.
>>>
>>> Hope this helps a bit.
>>>
>>> I am on holiday so my answers take a bit longer than normal.
>>>
>>> gr. Martin
>>>
>>> Mdu wrote:
>>>> Hi All,
>>>> I've gone a little further with a test case. I first run test case
>>>> testStoreEPackages() to create Ecore, XML, and new EPackage 'elv'.
>>>> I'm having a problem in testAddClassToElvEPackage(): please see a
>>>> block comment with PROBLEM: section.
>>>>
>>>> Thanks,
>>>> -Mdu
>>>>
>>>> package com.sbide.das.tests;
>>>>
>>>> import java.util.List;
>>>> import java.util.Properties;
>>>>
>>>> import junit.framework.TestCase;
>>>>
>>>>
>>>> import org.eclipse.emf.ecore.EAttribute;
>>>> import org.eclipse.emf.ecore.EClass;
>>>> import org.eclipse.emf.ecore.EObject;
>>>> import org.eclipse.emf.ecore.EPackage;
>>>> import org.eclipse.emf.ecore.EReference;
>>>> import org.eclipse.emf.ecore.EcoreFactory;
>>>> import org.eclipse.emf.ecore.EcorePackage;
>>>> import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
>>>> import org.eclipse.emf.teneo.hibernate.HbDataStore;
>>>> import org.eclipse.emf.teneo.hibernate.HbHelper;
>>>> import org.hibernate.Query;
>>>> import org.hibernate.Session;
>>>> import org.hibernate.SessionFactory;
>>>> import org.hibernate.Transaction;
>>>> import org.hibernate.cfg.Environment;
>>>>
>>>> import com.irg.eaf.common.util.ApplicationProperties;
>>>> import com.sbide.das.EnvironmentConstants;
>>>>
>>>> public class DynamicEPackageTestCase extends TestCase {
>>>>
>>>> public void setUp()
>>>> {
>>>> }
>>>>
>>>> public void testStoreEPackages()
>>>> {
>>>> final EcoreFactory efactory =
>>>> EcoreFactory.eINSTANCE;
>>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>>> final XMLTypePackage xmlpackage =
>>>> XMLTypePackage.eINSTANCE;
>>>> EPackage.Registry.INSTANCE.put(epackage.getNsURI(), epackage);
>>>> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),
>>>> xmlpackage);
>>>> // create the SchoolBook EClass
>>>> EClass schoolBookEClass = efactory.createEClass();
>>>> schoolBookEClass.setName("SchoolBook");
>>>> // create a new attribute for this EClass
>>>> EAttribute level = efactory.createEAttribute();
>>>> level.setName("level");
>>>> level.setEType(epackage.getEInt());
>>>> schoolBookEClass.getEStructuralFeatures().add(level);
>>>>
>>>> // create a course
>>>> EClass courseEClass = efactory.createEClass();
>>>> courseEClass.setName("Course");
>>>> // give the Course a name
>>>> EAttribute courseName = efactory.createEAttribute();
>>>> courseName.setName("courseName");
>>>> courseName.setEType(epackage.getEString());
>>>> courseEClass.getEStructuralFeatures().add(courseName);
>>>> // A course always uses one SchoolBook
>>>> EReference courseBook = efactory.createEReference();
>>>> courseBook.setName("courseBook");
>>>> courseBook.setEType(schoolBookEClass);
>>>> courseBook.setContainment(false);
>>>> courseEClass.getEStructuralFeatures().add(courseBook);
>>>> // Create a new EPackage and add the new EClasses
>>>> EPackage schoolPackage = efactory.createEPackage();
>>>> schoolPackage.setName("elv");
>>>> schoolPackage.setNsPrefix("elv");
>>>> schoolPackage.setNsURI("http:///www.elver.org/School");
>>>> schoolPackage.getEClassifiers().add(courseEClass);
>>>> schoolPackage.getEClassifiers().add(schoolBookEClass);
>>>> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),
>>>> schoolPackage);
>>>> // Now reset the epackages in the datastore
>>>> HbDataStore hbds = createHBDS(new
>>>> EPackage[]{epackage,schoolPackage,xmlpackage});
>>>> // print the hibernate.hbm.xml for demo purposes
>>>> System.err.println(hbds.getMappingXML());
>>>> // and create a course
>>>> EObject course =
>>>> schoolPackage.getEFactoryInstance().create(courseEClass);
>>>> course.eSet(courseName, "Dutch Literature Level 1");
>>>> final SessionFactory sessionFactory =
>>>> hbds.getSessionFactory();
>>>> // now persist them all
>>>> Session session = sessionFactory.openSession();
>>>> Transaction tx = session.getTransaction();
>>>> tx.begin();
>>>> session.save(xmlpackage);
>>>> session.save(epackage);
>>>> session.save(schoolPackage);
>>>> session.save(course);
>>>> tx.commit();
>>>>
>>>> session.close();
>>>> }
>>>> public void testAddClassToElvEPackage()
>>>> {
>>>>
>>>> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
>>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>>> // Now reset the epackages in the datastore
>>>> HbDataStore hbds = createHBDS(new EPackage[]{epackage});
>>>>
>>>> SessionFactory sessionFactory = hbds.getSessionFactory();
>>>> /**
>>>> * Read all relevant epackages to use them for later calling
>>>> setPackages
>>>> */
>>>> Session session = sessionFactory.openSession();
>>>> Transaction tx = session.getTransaction();
>>>> tx.begin();
>>>> Query qry = session.createQuery("from EPackage where
>>>> name='elv'");
>>>> List<EPackage> pkgs = qry.list();
>>>> EPackage elvPkg = pkgs.get(0);
>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>>> qry = session.createQuery("from EPackage where name='ecore'");
>>>> pkgs = qry.list();
>>>> EPackage ecorePkg = pkgs.get(0);
>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>>> qry = session.createQuery("from EPackage where name='type'");
>>>> pkgs = qry.list();
>>>> EPackage typePkg = pkgs.get(0);
>>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>>> typePkg); System.out.println("Collection Count:
>>>> "+session.getStatistics().getCollectionCount());
>>>> System.out.println("Entity Count:
>>>> "+session.getStatistics().getEntityCount());
>>>> elvPkg.getEClassifiers().size();
>>>> tx.commit();
>>>> /**
>>>> * Re-initialize store
>>>> */
>>>> hbds.setEPackages((new
>>>> EPackage[]{ecorePkg,elvPkg,typePkg}));
>>>> hbds.initialize(); sessionFactory =
>>>> hbds.getSessionFactory(); session =
>>>> sessionFactory.openSession();
>>>> tx = session.getTransaction();
>>>> /**
>>>> * Re-read epackages - if they are not re-read, they'd be
>>>> still attached to
>>>> * the old session before we called .setEPackages() and
>>>> .initialize():
>>>> *
>>>> * PROBLEM: Each EPackage query below returns empty list
>>>> (qry.list()).
>>>> * QUESTION: 1. I expected these to be still in the database
>>>> - why is the query
>>>> * returning empty results?
>>>> */
>>>> tx.begin();
>>>> qry = session.createQuery("from EPackage where name='type'");
>>>> pkgs = qry.list();
>>>> typePkg = pkgs.get(0);
>>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>>> typePkg); qry = session.createQuery("from EPackage
>>>> where name='ecore'");
>>>> pkgs = qry.list();
>>>> ecorePkg = pkgs.get(0);
>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>>> qry = session.createQuery("from EPackage where name='elv'");
>>>> pkgs = qry.list();
>>>> elvPkg = pkgs.get(0);
>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(),
>>>> elvPkg); tx.commit();
>>>> /**
>>>> * Fetch all three epackages for reference and modification
>>>> */
>>>> tx.begin();
>>>> System.out.println("Collection Count:
>>>> "+session.getStatistics().getCollectionCount());
>>>> System.out.println("Entity Count:
>>>> "+session.getStatistics().getEntityCount());
>>>> qry = session.createQuery("from EPackage where name='ecore'");
>>>> pkgs = qry.list();
>>>> ecorePkg = pkgs.get(0);
>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>>> qry = session.createQuery("from EPackage where name='elv'");
>>>> pkgs = qry.list();
>>>> elvPkg = pkgs.get(0);
>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>>> tx.commit(); tx.begin();
>>>> EClass testClass1 = efactory.createEClass();
>>>> testClass1.setName("TestClass1");
>>>> EAttribute attr1 = efactory.createEAttribute();
>>>> attr1.setName("intAttr");
>>>> attr1.setEType(epackage.getEInt());
>>>>
>>>> testClass1.getEStructuralFeatures().add(attr1);
>>>> session.save(attr1);
>>>> session.save(testClass1);
>>>> elvPkg.getEClassifiers().add(testClass1);
>>>> session.saveOrUpdate(typePkg);
>>>> session.saveOrUpdate(ecorePkg);
>>>> session.saveOrUpdate(elvPkg); tx.commit();
>>>>
>>>> session.close();
>>>> } public static HbDataStore createHBDS(EPackage[]
>>>> epackages)
>>>> { HbDataStore hbds =
>>>> (HbDataStore)HbHelper.INSTANCE.createRegisterDataStore(Appli cationProperties.get(EnvironmentConstants.TENEO_DS_NAME));
>>>>
>>>>
>>>> final Properties props = new Properties();
>>>> props.setProperty(Environment.DRIVER,
>>>> ApplicationProperties.get(Environment.DRIVER));
>>>> props.setProperty(Environment.USER,
>>>> ApplicationProperties.get(Environment.USER));
>>>> props.setProperty(Environment.URL,
>>>> ApplicationProperties.get(Environment.URL));
>>>> props.setProperty(Environment.PASS,
>>>> ApplicationProperties.get(Environment.PASS));
>>>> props.setProperty(Environment.DIALECT,
>>>> ApplicationProperties.get(Environment.DIALECT));
>>>> hbds.setProperties(props);
>>>> hbds.setEPackages(epackages);
>>>> hbds.initialize();
>>>> return hbds;
>>>> } }
>>>> Mdu wrote:
>>>>> Hi,
>>>>> I'm just about given up with the previous trial-and-error in
>>>>> working with EPackages stored via teneo. Is there an example (I
>>>>> also looked through Teneo test cases) that demonstrates the following:
>>>>>
>>>>> 1. Create a dynamic (empty) package and save it using Teneo
>>>>> 'session.save()'.
>>>>>
>>>>> 2. Load the package in 1. properly (registration and all) and add a
>>>>> simple EClass to it; save it back to using 'session.save()'.
>>>>>
>>>>> 3. Load the package again and use it to create an EOBject from a
>>>>> class added in 2.; and save the newly created EObject.
>>>>>
>>>>> 4. Do 1., 2., and 3. in separate test cases?
>>>>>
>>>>>
>>>>> I'm using the recent version of Teneo (1.0.1) and dependent EMF
>>>>> libraries.
>>>>> Any help will be much appreciated!
>>>>>
>>>>> Rgds,
>>>>> -Mdu
>>>
>>>
>
>
Re: [Teneo] Example to dynamically modify EPackages stored using Teneo [message #426372 is a reply to message #426369] Wed, 31 December 2008 09:25 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Mdu,
See comments inline.

Regarding not getting results when reading epackages:
When you check the database, is the epackage present in the database?

gr. Martin

Mdu wrote:
> Hi Martin - thanks for taking the time form your well deserved break!
> See my comments below:
> Martin Taal wrote:
>> Hi Mdu,
>> Create-drop will drop the database when re-initializing the datastore,
>> so in your case I would use update.
> I tried my 'update' option - and still have the same problem of a query
> that returns no epackages after re-initialize; and also got the
> following errors during re-initalize:
>
> 51027 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter
> table `eobject` add index emodelelement_eannotations
> (`eannotation_emodelelement_e_id`), add constraint
> emodelelement_eannotations foreign key
> (`eannotation_emodelelement_e_id`) references `eobject` (e_id)
> 51209 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate -
> Unsuccessful: alter table `eobject` add index emodelelement_eannotations
> (`eannotation_emodelelement_e_id`), add constraint
> emodelelement_eannotations foreign key
> (`eannotation_emodelelement_e_id`) references `eobject` (e_id)
> 51209 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - Can't
> create table 'solutionassembly.#sql-1208_1c' (errno: 121)
>>
MT>> Does the log earlier show errors when doing create table?

>> Answers:
>> 1) I did not completely understand the question, if the elv package
>> changes then new tables/columns need to be created this is done when
>> (re-)initializing the datastore.
>> 2) You can use 2 datastores (one for the ecore/xml package and one for
>> the elv package) if there are no direct references from elv to ecore.
>> With direct I mean that you did not specify in your model an
>> ereference which references the EClass type in the ecore package (or
>> eattribute, or ereference). Re-initializating should not be a problem
>> with the hibernate update option.
> This is where my ignorance shows a lot. Here's my understand how what
> you are suggesting would work:
>
> Let DS_Elv be data store for Elv package;
> DS_Ecore be data store for Ecore and XMLType packages
> HBDS_Elv be HB data source for DS_Elv
> HBDS_Ecore be HB data source for DS_Ecore
> Ssn_Elv be session from HBDS_Elv
> Ssn_Ecore be session from HBDS_Ecore
> ecorePkg and xmlTypePkg be persistent packages saved and loaded via
> Ssn_Ecore
MT>> What do you mean with Hb data source? The datastore?

It is not necessary to store and retrieve the ecore and xml packages from the db as they won't change.

I would say that:
- The HBDS_Ecore will store instances of the Ecore package (not the ecore package itself), the Elv
EPackage is an instance of the ecore epackage, so the HBDS_Ecore will store the Elv EPackage itself.
- The HBDS_Elv will store instances of the Elv model (so assume that the Elv EPackage has an eClass
Teneo, then this store will save an EObject which has eClass Teneo).

With this in mind I will react below.

>
> 1. Create HBDS_Elv:
> - HBDS_Elv.setEPackages({*}) - should * be ecorePkg and xmlTypePkg
> from Ssn_Ecore?
> - HBDS_Elv.initialize()
MT>> I would say that * is the Elv Package

To initialize the HBDS_Ecore:
HBDS_Ecore.setEPackages(new EPackage[]{ecorepkg, xmlpkg})
HBDS_Ecore.initialize();

>
> 2. Create and save Elv package 'elvPkg':
> - Obtain Ssn_Elv from HBDS_Elv sessionFactory
> - Create elvPkg and class 'class1' with 'attr1'
> - Transactionally call Ssn_Elv.save(elvPkg)
MT>> Obtain Ssn_Ecore from HBDS_Ecore and call Ssn_Ecore.save(elvPkg) (and commit the transaction)

> QUESTION: Doesn't this require persistent ecorePkg and xmlTypePkg
> implicilty to save 'class1' and 'attr1' i.e. instances from Ssn_Ecore.
> If so, wouldn't I get errors when I save a elvPkg that implicitly
> depends on epackages loaded from Ssn_Ecore - a separate session?
>
> 3. Read elvPkg back for modification:
> - HBDS_Elv.setEPackages({**}) - should ** be same ecorePkg and
> xmlTypePkg from Ssn_Ecore?
> - HBDS_Elv.initialize()
> - Create query 'from EPackage where name='Elv'
> - Assign elvPkg to the result
> - Add another 'class2' with 'attr2' to elvPkg
> - Transactionally call Ssn_Elv.save(elvPkg)
> QUESTION: Same question as above
MT>> Instead of HBDS_Elv I would you use HBDS_Ecore here.
After doing Ssn_Ecore.save(elvPkg) re-initialize the HBDS_Elv to update the database schema.

Also be aware that there is a global EPackage registry which stores mappings from nsuri to
EPackages. Also a resourceset has an epackage registry which stores this relation. So after reading
the elvpackage from the database set it in the global epackage registry.

>
> The only way I could see this working from here is by:
> Everytime I change elvPkg, I first have to read it; clone it using
> elvPkgClone = EcoreUtil.copy(); modify it; HBDS_Env.initialize(); and
> Ssn_Elv.save(elvPkgClone).
MT>> Afaics you don't need to clone, I would use HBDS_Ecore.

>
> Unless I fix my Hibernate (errno: 121) errors - assuming that's what
> preventing 'update' from working; this is my only choice. I also realize
> that I'm starting to meddle 'model evolution' issues here as well - that
> is how to evolve metamodels to be always in sync with their underlying
> models. I noticed that this has been discussed on CDO forums a bit.
MT>> Yes it is also a topic for CDO. The update of the database schema is one part of the story. In
the end changing relational database schemes on the fly is a touchy thing, on the other hand adding
nullable columns and new tables should work fine.

>
> Regards,
> -Mdu
>
>>
>> gr. Martin
>>
>> Mdu wrote:
>>> Thanks Martin,
>>> I turned on debugging and used create-drop option. Part of the log
>>> shows:
>>>
>>>
>>> 4476 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>> table if exists `eannotation_contents`
>>> 4479 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>> table if exists `eannotation_references`
>>> 4480 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>> table if exists `eclass_esupertypes`
>>> 4483 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>> table if exists `ecore_eobject`
>>> 4485 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>> table if exists `eoperation_eexceptions`
>>> 4486 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>> table if exists `ereference_ekeys`
>>> 4488 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>> table if exists details
>>>
>>>
>>> Questions:
>>> 1. Since I'm neither changing EcorePackage('ecore') nor
>>> XMLTypePackage('type') - only modifying package 'elv', why do I have
>>> to re-initialize HbDataStore?
>>>
>>>
>>> 2. It makes sense to store EcorePackage and XMLTypePackage in a
>>> separate HBDS as the are static, I still need to update custom
>>> EPackages (e.g. 'elv') which refer to these two packages. How do I
>>> accomplish this without running into re-initialize issues.
>>>
>>>
>>> Rgds,
>>> -Mdu
>>> Martin Taal wrote:
>>>> Hi Mdu,
>>>> I see that you add the xmltypepackage here.
>>>>
>>>> Is the database empty after the re-initialize? (so is the issue with
>>>> the querying or with the database being cleared during reinitialize.
>>>> In the last case, do you have hibernate.properties in the root of
>>>> the classpath which set the update database option of hibernate to
>>>> create-drop?
>>>>
>>>> Can you enable log4j logging for hibernate
>>>> (http://www.elver.org/hibernate/samples/log4j.properties) to see
>>>> what hibernate does during the re-initialize?
>>>>
>>>> Note that hibernate has some limitations when updating an existing
>>>> db schema, it will create tables, columns, foreign keys, but afaik
>>>> no indexes are created (if you have separate index annotations).
>>>>
>>>> Btw, instead of using one datastore for both the package itself and
>>>> the data you can also use a different datastore for the
>>>> ecorepackage/xmltypepackage (storing the epackages) and the data
>>>> (instances of the eclasses of elvpackage). Unless you have
>>>> references from your data to the model then one datastore is required.
>>>>
>>>> Hope this helps a bit.
>>>>
>>>> I am on holiday so my answers take a bit longer than normal.
>>>>
>>>> gr. Martin
>>>>
>>>> Mdu wrote:
>>>>> Hi All,
>>>>> I've gone a little further with a test case. I first run test
>>>>> case testStoreEPackages() to create Ecore, XML, and new EPackage
>>>>> 'elv'. I'm having a problem in testAddClassToElvEPackage(): please
>>>>> see a block comment with PROBLEM: section.
>>>>>
>>>>> Thanks,
>>>>> -Mdu
>>>>>
>>>>> package com.sbide.das.tests;
>>>>>
>>>>> import java.util.List;
>>>>> import java.util.Properties;
>>>>>
>>>>> import junit.framework.TestCase;
>>>>>
>>>>>
>>>>> import org.eclipse.emf.ecore.EAttribute;
>>>>> import org.eclipse.emf.ecore.EClass;
>>>>> import org.eclipse.emf.ecore.EObject;
>>>>> import org.eclipse.emf.ecore.EPackage;
>>>>> import org.eclipse.emf.ecore.EReference;
>>>>> import org.eclipse.emf.ecore.EcoreFactory;
>>>>> import org.eclipse.emf.ecore.EcorePackage;
>>>>> import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
>>>>> import org.eclipse.emf.teneo.hibernate.HbDataStore;
>>>>> import org.eclipse.emf.teneo.hibernate.HbHelper;
>>>>> import org.hibernate.Query;
>>>>> import org.hibernate.Session;
>>>>> import org.hibernate.SessionFactory;
>>>>> import org.hibernate.Transaction;
>>>>> import org.hibernate.cfg.Environment;
>>>>>
>>>>> import com.irg.eaf.common.util.ApplicationProperties;
>>>>> import com.sbide.das.EnvironmentConstants;
>>>>>
>>>>> public class DynamicEPackageTestCase extends TestCase {
>>>>>
>>>>> public void setUp()
>>>>> {
>>>>> }
>>>>>
>>>>> public void testStoreEPackages()
>>>>> {
>>>>> final EcoreFactory efactory =
>>>>> EcoreFactory.eINSTANCE;
>>>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>>>> final XMLTypePackage xmlpackage =
>>>>> XMLTypePackage.eINSTANCE;
>>>>> EPackage.Registry.INSTANCE.put(epackage.getNsURI(), epackage);
>>>>> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),
>>>>> xmlpackage);
>>>>> // create the SchoolBook EClass
>>>>> EClass schoolBookEClass = efactory.createEClass();
>>>>> schoolBookEClass.setName("SchoolBook");
>>>>> // create a new attribute for this EClass
>>>>> EAttribute level = efactory.createEAttribute();
>>>>> level.setName("level");
>>>>> level.setEType(epackage.getEInt());
>>>>> schoolBookEClass.getEStructuralFeatures().add(level);
>>>>>
>>>>> // create a course
>>>>> EClass courseEClass = efactory.createEClass();
>>>>> courseEClass.setName("Course");
>>>>> // give the Course a name
>>>>> EAttribute courseName = efactory.createEAttribute();
>>>>> courseName.setName("courseName");
>>>>> courseName.setEType(epackage.getEString());
>>>>> courseEClass.getEStructuralFeatures().add(courseName);
>>>>> // A course always uses one SchoolBook
>>>>> EReference courseBook = efactory.createEReference();
>>>>> courseBook.setName("courseBook");
>>>>> courseBook.setEType(schoolBookEClass);
>>>>> courseBook.setContainment(false);
>>>>> courseEClass.getEStructuralFeatures().add(courseBook);
>>>>> // Create a new EPackage and add the new EClasses
>>>>> EPackage schoolPackage = efactory.createEPackage();
>>>>> schoolPackage.setName("elv");
>>>>> schoolPackage.setNsPrefix("elv");
>>>>> schoolPackage.setNsURI("http:///www.elver.org/School");
>>>>> schoolPackage.getEClassifiers().add(courseEClass);
>>>>> schoolPackage.getEClassifiers().add(schoolBookEClass);
>>>>> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),
>>>>> schoolPackage);
>>>>> // Now reset the epackages in the datastore
>>>>> HbDataStore hbds = createHBDS(new
>>>>> EPackage[]{epackage,schoolPackage,xmlpackage});
>>>>> // print the hibernate.hbm.xml for demo purposes
>>>>> System.err.println(hbds.getMappingXML());
>>>>> // and create a course
>>>>> EObject course =
>>>>> schoolPackage.getEFactoryInstance().create(courseEClass);
>>>>> course.eSet(courseName, "Dutch Literature Level 1");
>>>>> final SessionFactory sessionFactory =
>>>>> hbds.getSessionFactory();
>>>>> // now persist them all
>>>>> Session session = sessionFactory.openSession();
>>>>> Transaction tx = session.getTransaction();
>>>>> tx.begin();
>>>>> session.save(xmlpackage);
>>>>> session.save(epackage);
>>>>> session.save(schoolPackage);
>>>>> session.save(course);
>>>>> tx.commit();
>>>>>
>>>>> session.close();
>>>>> }
>>>>> public void testAddClassToElvEPackage()
>>>>> {
>>>>>
>>>>> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
>>>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>>>> // Now reset the epackages in the datastore
>>>>> HbDataStore hbds = createHBDS(new EPackage[]{epackage});
>>>>>
>>>>> SessionFactory sessionFactory = hbds.getSessionFactory();
>>>>> /**
>>>>> * Read all relevant epackages to use them for later
>>>>> calling setPackages
>>>>> */
>>>>> Session session = sessionFactory.openSession();
>>>>> Transaction tx = session.getTransaction();
>>>>> tx.begin();
>>>>> Query qry = session.createQuery("from EPackage where
>>>>> name='elv'");
>>>>> List<EPackage> pkgs = qry.list();
>>>>> EPackage elvPkg = pkgs.get(0);
>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>>>> qry = session.createQuery("from EPackage where name='ecore'");
>>>>> pkgs = qry.list();
>>>>> EPackage ecorePkg = pkgs.get(0);
>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>>>> qry = session.createQuery("from EPackage where name='type'");
>>>>> pkgs = qry.list();
>>>>> EPackage typePkg = pkgs.get(0);
>>>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>>>> typePkg); System.out.println("Collection Count:
>>>>> "+session.getStatistics().getCollectionCount());
>>>>> System.out.println("Entity Count:
>>>>> "+session.getStatistics().getEntityCount());
>>>>> elvPkg.getEClassifiers().size();
>>>>> tx.commit();
>>>>> /**
>>>>> * Re-initialize store
>>>>> */
>>>>> hbds.setEPackages((new
>>>>> EPackage[]{ecorePkg,elvPkg,typePkg}));
>>>>> hbds.initialize(); sessionFactory =
>>>>> hbds.getSessionFactory(); session =
>>>>> sessionFactory.openSession();
>>>>> tx = session.getTransaction();
>>>>> /**
>>>>> * Re-read epackages - if they are not re-read, they'd be
>>>>> still attached to
>>>>> * the old session before we called .setEPackages() and
>>>>> .initialize():
>>>>> *
>>>>> * PROBLEM: Each EPackage query below returns empty list
>>>>> (qry.list()).
>>>>> * QUESTION: 1. I expected these to be still in the
>>>>> database - why is the query
>>>>> * returning empty results?
>>>>> */
>>>>> tx.begin();
>>>>> qry = session.createQuery("from EPackage where name='type'");
>>>>> pkgs = qry.list();
>>>>> typePkg = pkgs.get(0);
>>>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>>>> typePkg); qry = session.createQuery("from
>>>>> EPackage where name='ecore'");
>>>>> pkgs = qry.list();
>>>>> ecorePkg = pkgs.get(0);
>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>>>> qry = session.createQuery("from EPackage where name='elv'");
>>>>> pkgs = qry.list();
>>>>> elvPkg = pkgs.get(0);
>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(),
>>>>> elvPkg); tx.commit();
>>>>> /**
>>>>> * Fetch all three epackages for reference and modification
>>>>> */
>>>>> tx.begin();
>>>>> System.out.println("Collection Count:
>>>>> "+session.getStatistics().getCollectionCount());
>>>>> System.out.println("Entity Count:
>>>>> "+session.getStatistics().getEntityCount());
>>>>> qry = session.createQuery("from EPackage where name='ecore'");
>>>>> pkgs = qry.list();
>>>>> ecorePkg = pkgs.get(0);
>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(),
>>>>> ecorePkg); qry = session.createQuery("from EPackage
>>>>> where name='elv'");
>>>>> pkgs = qry.list();
>>>>> elvPkg = pkgs.get(0);
>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>>>> tx.commit(); tx.begin();
>>>>> EClass testClass1 = efactory.createEClass();
>>>>> testClass1.setName("TestClass1");
>>>>> EAttribute attr1 = efactory.createEAttribute();
>>>>> attr1.setName("intAttr");
>>>>> attr1.setEType(epackage.getEInt());
>>>>>
>>>>> testClass1.getEStructuralFeatures().add(attr1);
>>>>> session.save(attr1);
>>>>> session.save(testClass1);
>>>>> elvPkg.getEClassifiers().add(testClass1);
>>>>> session.saveOrUpdate(typePkg);
>>>>> session.saveOrUpdate(ecorePkg);
>>>>> session.saveOrUpdate(elvPkg); tx.commit();
>>>>>
>>>>> session.close();
>>>>> } public static HbDataStore createHBDS(EPackage[]
>>>>> epackages)
>>>>> { HbDataStore hbds =
>>>>> (HbDataStore)HbHelper.INSTANCE.createRegisterDataStore(Appli cationProperties.get(EnvironmentConstants.TENEO_DS_NAME));
>>>>>
>>>>>
>>>>> final Properties props = new Properties();
>>>>> props.setProperty(Environment.DRIVER,
>>>>> ApplicationProperties.get(Environment.DRIVER));
>>>>> props.setProperty(Environment.USER,
>>>>> ApplicationProperties.get(Environment.USER));
>>>>> props.setProperty(Environment.URL,
>>>>> ApplicationProperties.get(Environment.URL));
>>>>> props.setProperty(Environment.PASS,
>>>>> ApplicationProperties.get(Environment.PASS));
>>>>> props.setProperty(Environment.DIALECT,
>>>>> ApplicationProperties.get(Environment.DIALECT));
>>>>> hbds.setProperties(props);
>>>>> hbds.setEPackages(epackages);
>>>>> hbds.initialize();
>>>>> return hbds;
>>>>> } }
>>>>> Mdu wrote:
>>>>>> Hi,
>>>>>> I'm just about given up with the previous trial-and-error in
>>>>>> working with EPackages stored via teneo. Is there an example (I
>>>>>> also looked through Teneo test cases) that demonstrates the
>>>>>> following:
>>>>>>
>>>>>> 1. Create a dynamic (empty) package and save it using Teneo
>>>>>> 'session.save()'.
>>>>>>
>>>>>> 2. Load the package in 1. properly (registration and all) and add
>>>>>> a simple EClass to it; save it back to using 'session.save()'.
>>>>>>
>>>>>> 3. Load the package again and use it to create an EOBject from a
>>>>>> class added in 2.; and save the newly created EObject.
>>>>>>
>>>>>> 4. Do 1., 2., and 3. in separate test cases?
>>>>>>
>>>>>>
>>>>>> I'm using the recent version of Teneo (1.0.1) and dependent EMF
>>>>>> libraries.
>>>>>> Any help will be much appreciated!
>>>>>>
>>>>>> Rgds,
>>>>>> -Mdu
>>>>
>>>>
>>
>>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] Example to dynamically modify EPackages stored using Teneo [message #426377 is a reply to message #426372] Wed, 31 December 2008 15:04 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mduduzi.keswa.isizwe.com

Thanks Martin for being patient with this. I have gone a little further:

SUMMARY:
Error occurs when saving the EDataType of the attribute 'level' below
(see TRACE and ERROR).
BTW- The tables (including ecore_eobject and SchoolBool) are being
created - but obviously no data in them.

1. Test Case:
public void testPersistDynamicPackage()
{
//--
final EcoreFactory efactory = EcoreFactory.eINSTANCE;
final EcorePackage epackage = EcorePackage.eINSTANCE;
final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;

//--
EPackage.Registry.INSTANCE.put(epackage.getNsURI(),epackage) ;
EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),xmlpack age);

//--
EPackage schoolPackage = efactory.createEPackage();
schoolPackage.setName("SchoolPackage");
schoolPackage.setNsPrefix("SchoolPackage");
schoolPackage.setNsURI("http:///www.elver.org/School");

EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),scho olPackage);

EClass schoolBookEClass = efactory.createEClass();
schoolBookEClass.setName("SchoolBook");
EAttribute level = efactory.createEAttribute();
level.setName("level");
level.setEType(epackage.getEInt());
schoolBookEClass.getEStructuralFeatures().add(level);
schoolPackage.getEClassifiers().add(schoolBookEClass);
//--
HbDataStore emfMDRepoHBS = DbUtil.createEMFRepoHBDS(new
EPackage[]{epackage,xmlpackage});
SessionFactory emfRepoSessionFactory =
emfMDRepoHBS.getSessionFactory();
Session emfRepoSession = emfRepoSessionFactory.openSession();
Transaction txEmfRepo = emfRepoSession.getTransaction();

txEmfRepo.begin();
emfRepoSession.save(schoolPackage);
txEmfRepo.commit();

emfRepoSession.close();
}

2. DEBUG TRACE:
6059 [main] DEBUG
org.eclipse.emf.teneo.hibernate.mapping.identifier.Identifie rCacheHandler
- ID for object org.eclipse.emf.ecore.impl.EDataTypeImpl not found in
id cache
6059 [main] DEBUG
org.eclipse.emf.teneo.hibernate.mapping.identifier.Identifie rCacheHandler
- ID for object org.eclipse.emf.ecore.impl.EDataTypeImpl not found in
id cache


3. ERROR (EXCEPTION):
org.hibernate.TransientObjectException: object references an unsaved
transient instance - save the transient instance before flushing:
EClassifier
at
org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java: 265)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:61 9)
at
org.hibernate.persister.entity.AbstractEntityPersister.findD irty(AbstractEntityPersister.java:3151)
at
org.hibernate.event.def.DefaultFlushEntityEventListener.dirt yCheck(DefaultFlushEntityEventListener.java:501)
at
org.hibernate.event.def.DefaultFlushEntityEventListener.isUp dateNecessary(DefaultFlushEntityEventListener.java:227)
at
org.hibernate.event.def.DefaultFlushEntityEventListener.onFl ushEntity(DefaultFlushEntityEventListener.java:150)
at
org.hibernate.event.def.AbstractFlushingEventListener.flushE ntities(AbstractFlushingEventListener.java:219)
at
org.hibernate.event.def.AbstractFlushingEventListener.flushE verythingToExecutions(AbstractFlushingEventListener.java:99)
at
org.hibernate.event.def.DefaultFlushEventListener.onFlush(De faultFlushEventListener.java:49)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java :365)
at
org.hibernate.transaction.JDBCTransaction.commit(JDBCTransac tion.java:137)
at
com.sbide.das.tests.EcoreDTOUtilTestCase2.testPersistDynamic Package(EcoreDTOUtilTestCase2.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce ssorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe thodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at
org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestRefer ence.run(JUnit3TestReference.java:130)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(Test Execution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:460)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:673)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(R emoteTestRunner.java:386)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main( RemoteTestRunner.java:196)




Martin Taal wrote:
> Hi Mdu,
> See comments inline.
>
> Regarding not getting results when reading epackages:
> When you check the database, is the epackage present in the database?
>
> gr. Martin
>
> Mdu wrote:
>> Hi Martin - thanks for taking the time form your well deserved break!
>> See my comments below:
>> Martin Taal wrote:
>>> Hi Mdu,
>>> Create-drop will drop the database when re-initializing the
>>> datastore, so in your case I would use update.
>> I tried my 'update' option - and still have the same problem of a
>> query that returns no epackages after re-initialize; and also got the
>> following errors during re-initalize:
>>
>> 51027 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter
>> table `eobject` add index emodelelement_eannotations
>> (`eannotation_emodelelement_e_id`), add constraint
>> emodelelement_eannotations foreign key
>> (`eannotation_emodelelement_e_id`) references `eobject` (e_id)
>> 51209 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate -
>> Unsuccessful: alter table `eobject` add index
>> emodelelement_eannotations (`eannotation_emodelelement_e_id`), add
>> constraint emodelelement_eannotations foreign key
>> (`eannotation_emodelelement_e_id`) references `eobject` (e_id)
>> 51209 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - Can't
>> create table 'solutionassembly.#sql-1208_1c' (errno: 121)
>>>
> MT>> Does the log earlier show errors when doing create table?
>
>>> Answers:
>>> 1) I did not completely understand the question, if the elv package
>>> changes then new tables/columns need to be created this is done when
>>> (re-)initializing the datastore.
>>> 2) You can use 2 datastores (one for the ecore/xml package and one
>>> for the elv package) if there are no direct references from elv to
>>> ecore. With direct I mean that you did not specify in your model an
>>> ereference which references the EClass type in the ecore package (or
>>> eattribute, or ereference). Re-initializating should not be a problem
>>> with the hibernate update option.
>> This is where my ignorance shows a lot. Here's my understand how what
>> you are suggesting would work:
>>
>> Let DS_Elv be data store for Elv package;
>> DS_Ecore be data store for Ecore and XMLType packages
>> HBDS_Elv be HB data source for DS_Elv
>> HBDS_Ecore be HB data source for DS_Ecore
>> Ssn_Elv be session from HBDS_Elv
>> Ssn_Ecore be session from HBDS_Ecore
>> ecorePkg and xmlTypePkg be persistent packages saved and loaded
>> via Ssn_Ecore
> MT>> What do you mean with Hb data source? The datastore?
>
> It is not necessary to store and retrieve the ecore and xml packages
> from the db as they won't change.
>
> I would say that:
> - The HBDS_Ecore will store instances of the Ecore package (not the
> ecore package itself), the Elv EPackage is an instance of the ecore
> epackage, so the HBDS_Ecore will store the Elv EPackage itself.
> - The HBDS_Elv will store instances of the Elv model (so assume that the
> Elv EPackage has an eClass Teneo, then this store will save an EObject
> which has eClass Teneo).
>
> With this in mind I will react below.
>
>>
>> 1. Create HBDS_Elv:
>> - HBDS_Elv.setEPackages({*}) - should * be ecorePkg and xmlTypePkg
>> from Ssn_Ecore?
>> - HBDS_Elv.initialize()
> MT>> I would say that * is the Elv Package
>
> To initialize the HBDS_Ecore:
> HBDS_Ecore.setEPackages(new EPackage[]{ecorepkg, xmlpkg})
> HBDS_Ecore.initialize();
>
>>
>> 2. Create and save Elv package 'elvPkg':
>> - Obtain Ssn_Elv from HBDS_Elv sessionFactory
>> - Create elvPkg and class 'class1' with 'attr1'
>> - Transactionally call Ssn_Elv.save(elvPkg)
> MT>> Obtain Ssn_Ecore from HBDS_Ecore and call Ssn_Ecore.save(elvPkg)
> (and commit the transaction)
>
>> QUESTION: Doesn't this require persistent ecorePkg and xmlTypePkg
>> implicilty to save 'class1' and 'attr1' i.e. instances from Ssn_Ecore.
>> If so, wouldn't I get errors when I save a elvPkg that implicitly
>> depends on epackages loaded from Ssn_Ecore - a separate session?
>>
>> 3. Read elvPkg back for modification:
>> - HBDS_Elv.setEPackages({**}) - should ** be same ecorePkg and
>> xmlTypePkg from Ssn_Ecore?
>> - HBDS_Elv.initialize()
>> - Create query 'from EPackage where name='Elv'
>> - Assign elvPkg to the result
>> - Add another 'class2' with 'attr2' to elvPkg
>> - Transactionally call Ssn_Elv.save(elvPkg)
>> QUESTION: Same question as above
> MT>> Instead of HBDS_Elv I would you use HBDS_Ecore here.
> After doing Ssn_Ecore.save(elvPkg) re-initialize the HBDS_Elv to update
> the database schema.
>
> Also be aware that there is a global EPackage registry which stores
> mappings from nsuri to EPackages. Also a resourceset has an epackage
> registry which stores this relation. So after reading the elvpackage
> from the database set it in the global epackage registry.
>
>>
>> The only way I could see this working from here is by:
>> Everytime I change elvPkg, I first have to read it; clone it using
>> elvPkgClone = EcoreUtil.copy(); modify it; HBDS_Env.initialize(); and
>> Ssn_Elv.save(elvPkgClone).
> MT>> Afaics you don't need to clone, I would use HBDS_Ecore.
>
>>
>> Unless I fix my Hibernate (errno: 121) errors - assuming that's what
>> preventing 'update' from working; this is my only choice. I also
>> realize that I'm starting to meddle 'model evolution' issues here as
>> well - that is how to evolve metamodels to be always in sync with
>> their underlying models. I noticed that this has been discussed on CDO
>> forums a bit.
> MT>> Yes it is also a topic for CDO. The update of the database schema
> is one part of the story. In the end changing relational database
> schemes on the fly is a touchy thing, on the other hand adding nullable
> columns and new tables should work fine.
>
>>
>> Regards,
>> -Mdu
>>
>>>
>>> gr. Martin
>>>
>>> Mdu wrote:
>>>> Thanks Martin,
>>>> I turned on debugging and used create-drop option. Part of the
>>>> log shows:
>>>>
>>>>
>>>> 4476 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>> table if exists `eannotation_contents`
>>>> 4479 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>> table if exists `eannotation_references`
>>>> 4480 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>> table if exists `eclass_esupertypes`
>>>> 4483 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>> table if exists `ecore_eobject`
>>>> 4485 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>> table if exists `eoperation_eexceptions`
>>>> 4486 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>> table if exists `ereference_ekeys`
>>>> 4488 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>> table if exists details
>>>>
>>>>
>>>> Questions:
>>>> 1. Since I'm neither changing EcorePackage('ecore') nor
>>>> XMLTypePackage('type') - only modifying package 'elv', why do I have
>>>> to re-initialize HbDataStore?
>>>>
>>>>
>>>> 2. It makes sense to store EcorePackage and XMLTypePackage in a
>>>> separate HBDS as the are static, I still need to update custom
>>>> EPackages (e.g. 'elv') which refer to these two packages. How do I
>>>> accomplish this without running into re-initialize issues.
>>>>
>>>>
>>>> Rgds,
>>>> -Mdu
>>>> Martin Taal wrote:
>>>>> Hi Mdu,
>>>>> I see that you add the xmltypepackage here.
>>>>>
>>>>> Is the database empty after the re-initialize? (so is the issue
>>>>> with the querying or with the database being cleared during
>>>>> reinitialize. In the last case, do you have hibernate.properties in
>>>>> the root of the classpath which set the update database option of
>>>>> hibernate to create-drop?
>>>>>
>>>>> Can you enable log4j logging for hibernate
>>>>> (http://www.elver.org/hibernate/samples/log4j.properties) to see
>>>>> what hibernate does during the re-initialize?
>>>>>
>>>>> Note that hibernate has some limitations when updating an existing
>>>>> db schema, it will create tables, columns, foreign keys, but afaik
>>>>> no indexes are created (if you have separate index annotations).
>>>>>
>>>>> Btw, instead of using one datastore for both the package itself and
>>>>> the data you can also use a different datastore for the
>>>>> ecorepackage/xmltypepackage (storing the epackages) and the data
>>>>> (instances of the eclasses of elvpackage). Unless you have
>>>>> references from your data to the model then one datastore is required.
>>>>>
>>>>> Hope this helps a bit.
>>>>>
>>>>> I am on holiday so my answers take a bit longer than normal.
>>>>>
>>>>> gr. Martin
>>>>>
>>>>> Mdu wrote:
>>>>>> Hi All,
>>>>>> I've gone a little further with a test case. I first run test
>>>>>> case testStoreEPackages() to create Ecore, XML, and new EPackage
>>>>>> 'elv'. I'm having a problem in testAddClassToElvEPackage(): please
>>>>>> see a block comment with PROBLEM: section.
>>>>>>
>>>>>> Thanks,
>>>>>> -Mdu
>>>>>>
>>>>>> package com.sbide.das.tests;
>>>>>>
>>>>>> import java.util.List;
>>>>>> import java.util.Properties;
>>>>>>
>>>>>> import junit.framework.TestCase;
>>>>>>
>>>>>>
>>>>>> import org.eclipse.emf.ecore.EAttribute;
>>>>>> import org.eclipse.emf.ecore.EClass;
>>>>>> import org.eclipse.emf.ecore.EObject;
>>>>>> import org.eclipse.emf.ecore.EPackage;
>>>>>> import org.eclipse.emf.ecore.EReference;
>>>>>> import org.eclipse.emf.ecore.EcoreFactory;
>>>>>> import org.eclipse.emf.ecore.EcorePackage;
>>>>>> import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
>>>>>> import org.eclipse.emf.teneo.hibernate.HbDataStore;
>>>>>> import org.eclipse.emf.teneo.hibernate.HbHelper;
>>>>>> import org.hibernate.Query;
>>>>>> import org.hibernate.Session;
>>>>>> import org.hibernate.SessionFactory;
>>>>>> import org.hibernate.Transaction;
>>>>>> import org.hibernate.cfg.Environment;
>>>>>>
>>>>>> import com.irg.eaf.common.util.ApplicationProperties;
>>>>>> import com.sbide.das.EnvironmentConstants;
>>>>>>
>>>>>> public class DynamicEPackageTestCase extends TestCase {
>>>>>>
>>>>>> public void setUp()
>>>>>> {
>>>>>> }
>>>>>>
>>>>>> public void testStoreEPackages()
>>>>>> {
>>>>>> final EcoreFactory efactory =
>>>>>> EcoreFactory.eINSTANCE;
>>>>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>>>>> final XMLTypePackage xmlpackage =
>>>>>> XMLTypePackage.eINSTANCE;
>>>>>> EPackage.Registry.INSTANCE.put(epackage.getNsURI(), epackage);
>>>>>> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),
>>>>>> xmlpackage);
>>>>>> // create the SchoolBook EClass
>>>>>> EClass schoolBookEClass = efactory.createEClass();
>>>>>> schoolBookEClass.setName("SchoolBook");
>>>>>> // create a new attribute for this EClass
>>>>>> EAttribute level = efactory.createEAttribute();
>>>>>> level.setName("level");
>>>>>> level.setEType(epackage.getEInt());
>>>>>> schoolBookEClass.getEStructuralFeatures().add(level);
>>>>>>
>>>>>> // create a course
>>>>>> EClass courseEClass = efactory.createEClass();
>>>>>> courseEClass.setName("Course");
>>>>>> // give the Course a name
>>>>>> EAttribute courseName = efactory.createEAttribute();
>>>>>> courseName.setName("courseName");
>>>>>> courseName.setEType(epackage.getEString());
>>>>>> courseEClass.getEStructuralFeatures().add(courseName);
>>>>>> // A course always uses one SchoolBook
>>>>>> EReference courseBook = efactory.createEReference();
>>>>>> courseBook.setName("courseBook");
>>>>>> courseBook.setEType(schoolBookEClass);
>>>>>> courseBook.setContainment(false);
>>>>>> courseEClass.getEStructuralFeatures().add(courseBook);
>>>>>> // Create a new EPackage and add the new EClasses
>>>>>> EPackage schoolPackage = efactory.createEPackage();
>>>>>> schoolPackage.setName("elv");
>>>>>> schoolPackage.setNsPrefix("elv");
>>>>>> schoolPackage.setNsURI("http:///www.elver.org/School");
>>>>>> schoolPackage.getEClassifiers().add(courseEClass);
>>>>>> schoolPackage.getEClassifiers().add(schoolBookEClass);
>>>>>> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),
>>>>>> schoolPackage);
>>>>>> // Now reset the epackages in the datastore
>>>>>> HbDataStore hbds = createHBDS(new
>>>>>> EPackage[]{epackage,schoolPackage,xmlpackage});
>>>>>> // print the hibernate.hbm.xml for demo purposes
>>>>>> System.err.println(hbds.getMappingXML());
>>>>>> // and create a course
>>>>>> EObject course =
>>>>>> schoolPackage.getEFactoryInstance().create(courseEClass);
>>>>>> course.eSet(courseName, "Dutch Literature Level 1");
>>>>>> final SessionFactory sessionFactory =
>>>>>> hbds.getSessionFactory();
>>>>>> // now persist them all
>>>>>> Session session = sessionFactory.openSession();
>>>>>> Transaction tx = session.getTransaction();
>>>>>> tx.begin();
>>>>>> session.save(xmlpackage);
>>>>>> session.save(epackage);
>>>>>> session.save(schoolPackage);
>>>>>> session.save(course);
>>>>>> tx.commit();
>>>>>>
>>>>>> session.close();
>>>>>> }
>>>>>> public void testAddClassToElvEPackage()
>>>>>> {
>>>>>>
>>>>>> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
>>>>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>>>>> // Now reset the epackages in the datastore
>>>>>> HbDataStore hbds = createHBDS(new EPackage[]{epackage});
>>>>>>
>>>>>> SessionFactory sessionFactory = hbds.getSessionFactory();
>>>>>> /**
>>>>>> * Read all relevant epackages to use them for later
>>>>>> calling setPackages
>>>>>> */
>>>>>> Session session = sessionFactory.openSession();
>>>>>> Transaction tx = session.getTransaction();
>>>>>> tx.begin();
>>>>>> Query qry = session.createQuery("from EPackage where
>>>>>> name='elv'");
>>>>>> List<EPackage> pkgs = qry.list();
>>>>>> EPackage elvPkg = pkgs.get(0);
>>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>>>>> qry = session.createQuery("from EPackage where
>>>>>> name='ecore'");
>>>>>> pkgs = qry.list();
>>>>>> EPackage ecorePkg = pkgs.get(0);
>>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>>>>> qry = session.createQuery("from EPackage where name='type'");
>>>>>> pkgs = qry.list();
>>>>>> EPackage typePkg = pkgs.get(0);
>>>>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>>>>> typePkg); System.out.println("Collection Count:
>>>>>> "+session.getStatistics().getCollectionCount());
>>>>>> System.out.println("Entity Count:
>>>>>> "+session.getStatistics().getEntityCount());
>>>>>> elvPkg.getEClassifiers().size();
>>>>>> tx.commit();
>>>>>> /**
>>>>>> * Re-initialize store
>>>>>> */
>>>>>> hbds.setEPackages((new
>>>>>> EPackage[]{ecorePkg,elvPkg,typePkg}));
>>>>>> hbds.initialize(); sessionFactory =
>>>>>> hbds.getSessionFactory(); session =
>>>>>> sessionFactory.openSession();
>>>>>> tx = session.getTransaction();
>>>>>> /**
>>>>>> * Re-read epackages - if they are not re-read, they'd be
>>>>>> still attached to
>>>>>> * the old session before we called .setEPackages() and
>>>>>> .initialize():
>>>>>> *
>>>>>> * PROBLEM: Each EPackage query below returns empty list
>>>>>> (qry.list()).
>>>>>> * QUESTION: 1. I expected these to be still in the
>>>>>> database - why is the query
>>>>>> * returning empty results?
>>>>>> */
>>>>>> tx.begin();
>>>>>> qry = session.createQuery("from EPackage where name='type'");
>>>>>> pkgs = qry.list();
>>>>>> typePkg = pkgs.get(0);
>>>>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>>>>> typePkg); qry = session.createQuery("from
>>>>>> EPackage where name='ecore'");
>>>>>> pkgs = qry.list();
>>>>>> ecorePkg = pkgs.get(0);
>>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>>>>> qry = session.createQuery("from EPackage where name='elv'");
>>>>>> pkgs = qry.list();
>>>>>> elvPkg = pkgs.get(0);
>>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(),
>>>>>> elvPkg); tx.commit();
>>>>>> /**
>>>>>> * Fetch all three epackages for reference and modification
>>>>>> */
>>>>>> tx.begin();
>>>>>> System.out.println("Collection Count:
>>>>>> "+session.getStatistics().getCollectionCount());
>>>>>> System.out.println("Entity Count:
>>>>>> "+session.getStatistics().getEntityCount());
>>>>>> qry = session.createQuery("from EPackage where
>>>>>> name='ecore'");
>>>>>> pkgs = qry.list();
>>>>>> ecorePkg = pkgs.get(0);
>>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(),
>>>>>> ecorePkg); qry = session.createQuery("from EPackage
>>>>>> where name='elv'");
>>>>>> pkgs = qry.list();
>>>>>> elvPkg = pkgs.get(0);
>>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>>>>> tx.commit(); tx.begin();
>>>>>> EClass testClass1 = efactory.createEClass();
>>>>>> testClass1.setName("TestClass1");
>>>>>> EAttribute attr1 = efactory.createEAttribute();
>>>>>> attr1.setName("intAttr");
>>>>>> attr1.setEType(epackage.getEInt());
>>>>>>
>>>>>> testClass1.getEStructuralFeatures().add(attr1);
>>>>>> session.save(attr1);
>>>>>> session.save(testClass1);
>>>>>> elvPkg.getEClassifiers().add(testClass1);
>>>>>> session.saveOrUpdate(typePkg);
>>>>>> session.saveOrUpdate(ecorePkg);
>>>>>> session.saveOrUpdate(elvPkg); tx.commit();
>>>>>>
>>>>>> session.close();
>>>>>> } public static HbDataStore createHBDS(EPackage[]
>>>>>> epackages)
>>>>>> { HbDataStore hbds =
>>>>>> (HbDataStore)HbHelper.INSTANCE.createRegisterDataStore(Appli cationProperties.get(EnvironmentConstants.TENEO_DS_NAME));
>>>>>>
>>>>>>
>>>>>> final Properties props = new Properties();
>>>>>> props.setProperty(Environment.DRIVER,
>>>>>> ApplicationProperties.get(Environment.DRIVER));
>>>>>> props.setProperty(Environment.USER,
>>>>>> ApplicationProperties.get(Environment.USER));
>>>>>> props.setProperty(Environment.URL,
>>>>>> ApplicationProperties.get(Environment.URL));
>>>>>> props.setProperty(Environment.PASS,
>>>>>> ApplicationProperties.get(Environment.PASS));
>>>>>> props.setProperty(Environment.DIALECT,
>>>>>> ApplicationProperties.get(Environment.DIALECT));
>>>>>> hbds.setProperties(props);
>>>>>> hbds.setEPackages(epackages);
>>>>>> hbds.initialize();
>>>>>> return hbds;
>>>>>> } }
>>>>>> Mdu wrote:
>>>>>>> Hi,
>>>>>>> I'm just about given up with the previous trial-and-error in
>>>>>>> working with EPackages stored via teneo. Is there an example (I
>>>>>>> also looked through Teneo test cases) that demonstrates the
>>>>>>> following:
>>>>>>>
>>>>>>> 1. Create a dynamic (empty) package and save it using Teneo
>>>>>>> 'session.save()'.
>>>>>>>
>>>>>>> 2. Load the package in 1. properly (registration and all) and add
>>>>>>> a simple EClass to it; save it back to using 'session.save()'.
>>>>>>>
>>>>>>> 3. Load the package again and use it to create an EOBject from a
>>>>>>> class added in 2.; and save the newly created EObject.
>>>>>>>
>>>>>>> 4. Do 1., 2., and 3. in separate test cases?
>>>>>>>
>>>>>>>
>>>>>>> I'm using the recent version of Teneo (1.0.1) and dependent EMF
>>>>>>> libraries.
>>>>>>> Any help will be much appreciated!
>>>>>>>
>>>>>>> Rgds,
>>>>>>> -Mdu
>>>>>
>>>>>
>>>
>>>
>
>
Re: [Teneo] Example to dynamically modify EPackages stored using Teneo [message #426381 is a reply to message #426377] Thu, 01 January 2009 20:30 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Mdu,
Sorry my confusion/mistake, you also need to store the ecore package and the xmlpackage in the
emfMDRepoHBS.

gr. Martin

Mdu wrote:
> Thanks Martin for being patient with this. I have gone a little further:
>
> SUMMARY:
> Error occurs when saving the EDataType of the attribute 'level' below
> (see TRACE and ERROR).
> BTW- The tables (including ecore_eobject and SchoolBool) are being
> created - but obviously no data in them.
>
> 1. Test Case:
> public void testPersistDynamicPackage()
> {
> //--
> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
> final EcorePackage epackage = EcorePackage.eINSTANCE;
> final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;
>
> //--
> EPackage.Registry.INSTANCE.put(epackage.getNsURI(),epackage) ;
> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),xmlpack age);
>
> //--
> EPackage schoolPackage = efactory.createEPackage();
> schoolPackage.setName("SchoolPackage");
> schoolPackage.setNsPrefix("SchoolPackage");
> schoolPackage.setNsURI("http:///www.elver.org/School");
>
> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),scho olPackage);
>
> EClass schoolBookEClass = efactory.createEClass();
> schoolBookEClass.setName("SchoolBook");
> EAttribute level = efactory.createEAttribute();
> level.setName("level");
> level.setEType(epackage.getEInt());
> schoolBookEClass.getEStructuralFeatures().add(level);
> schoolPackage.getEClassifiers().add(schoolBookEClass);
> //--
> HbDataStore emfMDRepoHBS = DbUtil.createEMFRepoHBDS(new
> EPackage[]{epackage,xmlpackage});
> SessionFactory emfRepoSessionFactory =
> emfMDRepoHBS.getSessionFactory();
> Session emfRepoSession = emfRepoSessionFactory.openSession();
> Transaction txEmfRepo = emfRepoSession.getTransaction();
>
> txEmfRepo.begin();
> emfRepoSession.save(schoolPackage);
> txEmfRepo.commit();
>
> emfRepoSession.close();
> }
>
> 2. DEBUG TRACE:
> 6059 [main] DEBUG
> org.eclipse.emf.teneo.hibernate.mapping.identifier.Identifie rCacheHandler
> - ID for object org.eclipse.emf.ecore.impl.EDataTypeImpl not found in
> id cache
> 6059 [main] DEBUG
> org.eclipse.emf.teneo.hibernate.mapping.identifier.Identifie rCacheHandler
> - ID for object org.eclipse.emf.ecore.impl.EDataTypeImpl not found in
> id cache
>
>
> 3. ERROR (EXCEPTION):
> org.hibernate.TransientObjectException: object references an unsaved
> transient instance - save the transient instance before flushing:
> EClassifier
> at
> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>
> at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
> at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java: 265)
> at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:61 9)
> at
> org.hibernate.persister.entity.AbstractEntityPersister.findD irty(AbstractEntityPersister.java:3151)
>
> at
> org.hibernate.event.def.DefaultFlushEntityEventListener.dirt yCheck(DefaultFlushEntityEventListener.java:501)
>
> at
> org.hibernate.event.def.DefaultFlushEntityEventListener.isUp dateNecessary(DefaultFlushEntityEventListener.java:227)
>
> at
> org.hibernate.event.def.DefaultFlushEntityEventListener.onFl ushEntity(DefaultFlushEntityEventListener.java:150)
>
> at
> org.hibernate.event.def.AbstractFlushingEventListener.flushE ntities(AbstractFlushingEventListener.java:219)
>
> at
> org.hibernate.event.def.AbstractFlushingEventListener.flushE verythingToExecutions(AbstractFlushingEventListener.java:99)
>
> at
> org.hibernate.event.def.DefaultFlushEventListener.onFlush(De faultFlushEventListener.java:49)
>
> at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
> at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java :365)
> at
> org.hibernate.transaction.JDBCTransaction.commit(JDBCTransac tion.java:137)
> at
> com.sbide.das.tests.EcoreDTOUtilTestCase2.testPersistDynamic Package(EcoreDTOUtilTestCase2.java:105)
>
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce ssorImpl.java:39)
>
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe thodAccessorImpl.java:25)
>
> at java.lang.reflect.Method.invoke(Method.java:585)
> at junit.framework.TestCase.runTest(TestCase.java:164)
> at junit.framework.TestCase.runBare(TestCase.java:130)
> at junit.framework.TestResult$1.protect(TestResult.java:106)
> at junit.framework.TestResult.runProtected(TestResult.java:124)
> at junit.framework.TestResult.run(TestResult.java:109)
> at junit.framework.TestCase.run(TestCase.java:120)
> at
> org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestRefer ence.run(JUnit3TestReference.java:130)
>
> at
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(Test Execution.java:38)
>
> at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:460)
>
> at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:673)
>
> at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(R emoteTestRunner.java:386)
>
> at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main( RemoteTestRunner.java:196)
>
>
>
>
>
> Martin Taal wrote:
>> Hi Mdu,
>> See comments inline.
>>
>> Regarding not getting results when reading epackages:
>> When you check the database, is the epackage present in the database?
>>
>> gr. Martin
>>
>> Mdu wrote:
>>> Hi Martin - thanks for taking the time form your well deserved break!
>>> See my comments below:
>>> Martin Taal wrote:
>>>> Hi Mdu,
>>>> Create-drop will drop the database when re-initializing the
>>>> datastore, so in your case I would use update.
>>> I tried my 'update' option - and still have the same problem of a
>>> query that returns no epackages after re-initialize; and also got the
>>> following errors during re-initalize:
>>>
>>> 51027 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter
>>> table `eobject` add index emodelelement_eannotations
>>> (`eannotation_emodelelement_e_id`), add constraint
>>> emodelelement_eannotations foreign key
>>> (`eannotation_emodelelement_e_id`) references `eobject` (e_id)
>>> 51209 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate -
>>> Unsuccessful: alter table `eobject` add index
>>> emodelelement_eannotations (`eannotation_emodelelement_e_id`), add
>>> constraint emodelelement_eannotations foreign key
>>> (`eannotation_emodelelement_e_id`) references `eobject` (e_id)
>>> 51209 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - Can't
>>> create table 'solutionassembly.#sql-1208_1c' (errno: 121)
>>>>
>> MT>> Does the log earlier show errors when doing create table?
>>
>>>> Answers:
>>>> 1) I did not completely understand the question, if the elv package
>>>> changes then new tables/columns need to be created this is done when
>>>> (re-)initializing the datastore.
>>>> 2) You can use 2 datastores (one for the ecore/xml package and one
>>>> for the elv package) if there are no direct references from elv to
>>>> ecore. With direct I mean that you did not specify in your model an
>>>> ereference which references the EClass type in the ecore package (or
>>>> eattribute, or ereference). Re-initializating should not be a
>>>> problem with the hibernate update option.
>>> This is where my ignorance shows a lot. Here's my understand how what
>>> you are suggesting would work:
>>>
>>> Let DS_Elv be data store for Elv package;
>>> DS_Ecore be data store for Ecore and XMLType packages
>>> HBDS_Elv be HB data source for DS_Elv
>>> HBDS_Ecore be HB data source for DS_Ecore
>>> Ssn_Elv be session from HBDS_Elv
>>> Ssn_Ecore be session from HBDS_Ecore
>>> ecorePkg and xmlTypePkg be persistent packages saved and loaded
>>> via Ssn_Ecore
>> MT>> What do you mean with Hb data source? The datastore?
>>
>> It is not necessary to store and retrieve the ecore and xml packages
>> from the db as they won't change.
>>
>> I would say that:
>> - The HBDS_Ecore will store instances of the Ecore package (not the
>> ecore package itself), the Elv EPackage is an instance of the ecore
>> epackage, so the HBDS_Ecore will store the Elv EPackage itself.
>> - The HBDS_Elv will store instances of the Elv model (so assume that
>> the Elv EPackage has an eClass Teneo, then this store will save an
>> EObject which has eClass Teneo).
>>
>> With this in mind I will react below.
>>
>>>
>>> 1. Create HBDS_Elv:
>>> - HBDS_Elv.setEPackages({*}) - should * be ecorePkg and xmlTypePkg
>>> from Ssn_Ecore?
>>> - HBDS_Elv.initialize()
>> MT>> I would say that * is the Elv Package
>>
>> To initialize the HBDS_Ecore:
>> HBDS_Ecore.setEPackages(new EPackage[]{ecorepkg, xmlpkg})
>> HBDS_Ecore.initialize();
>>
>>>
>>> 2. Create and save Elv package 'elvPkg':
>>> - Obtain Ssn_Elv from HBDS_Elv sessionFactory
>>> - Create elvPkg and class 'class1' with 'attr1'
>>> - Transactionally call Ssn_Elv.save(elvPkg)
>> MT>> Obtain Ssn_Ecore from HBDS_Ecore and call Ssn_Ecore.save(elvPkg)
>> (and commit the transaction)
>>
>>> QUESTION: Doesn't this require persistent ecorePkg and xmlTypePkg
>>> implicilty to save 'class1' and 'attr1' i.e. instances from
>>> Ssn_Ecore. If so, wouldn't I get errors when I save a elvPkg that
>>> implicitly depends on epackages loaded from Ssn_Ecore - a separate
>>> session?
>>>
>>> 3. Read elvPkg back for modification:
>>> - HBDS_Elv.setEPackages({**}) - should ** be same ecorePkg and
>>> xmlTypePkg from Ssn_Ecore?
>>> - HBDS_Elv.initialize()
>>> - Create query 'from EPackage where name='Elv'
>>> - Assign elvPkg to the result
>>> - Add another 'class2' with 'attr2' to elvPkg
>>> - Transactionally call Ssn_Elv.save(elvPkg)
>>> QUESTION: Same question as above
>> MT>> Instead of HBDS_Elv I would you use HBDS_Ecore here.
>> After doing Ssn_Ecore.save(elvPkg) re-initialize the HBDS_Elv to
>> update the database schema.
>>
>> Also be aware that there is a global EPackage registry which stores
>> mappings from nsuri to EPackages. Also a resourceset has an epackage
>> registry which stores this relation. So after reading the elvpackage
>> from the database set it in the global epackage registry.
>>
>>>
>>> The only way I could see this working from here is by:
>>> Everytime I change elvPkg, I first have to read it; clone it using
>>> elvPkgClone = EcoreUtil.copy(); modify it; HBDS_Env.initialize(); and
>>> Ssn_Elv.save(elvPkgClone).
>> MT>> Afaics you don't need to clone, I would use HBDS_Ecore.
>>
>>>
>>> Unless I fix my Hibernate (errno: 121) errors - assuming that's what
>>> preventing 'update' from working; this is my only choice. I also
>>> realize that I'm starting to meddle 'model evolution' issues here as
>>> well - that is how to evolve metamodels to be always in sync with
>>> their underlying models. I noticed that this has been discussed on
>>> CDO forums a bit.
>> MT>> Yes it is also a topic for CDO. The update of the database schema
>> is one part of the story. In the end changing relational database
>> schemes on the fly is a touchy thing, on the other hand adding
>> nullable columns and new tables should work fine.
>>
>>>
>>> Regards,
>>> -Mdu
>>>
>>>>
>>>> gr. Martin
>>>>
>>>> Mdu wrote:
>>>>> Thanks Martin,
>>>>> I turned on debugging and used create-drop option. Part of the
>>>>> log shows:
>>>>>
>>>>>
>>>>> 4476 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>> table if exists `eannotation_contents`
>>>>> 4479 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>> table if exists `eannotation_references`
>>>>> 4480 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>> table if exists `eclass_esupertypes`
>>>>> 4483 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>> table if exists `ecore_eobject`
>>>>> 4485 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>> table if exists `eoperation_eexceptions`
>>>>> 4486 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>> table if exists `ereference_ekeys`
>>>>> 4488 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>> table if exists details
>>>>>
>>>>>
>>>>> Questions:
>>>>> 1. Since I'm neither changing EcorePackage('ecore') nor
>>>>> XMLTypePackage('type') - only modifying package 'elv', why do I
>>>>> have to re-initialize HbDataStore?
>>>>>
>>>>>
>>>>> 2. It makes sense to store EcorePackage and XMLTypePackage in a
>>>>> separate HBDS as the are static, I still need to update custom
>>>>> EPackages (e.g. 'elv') which refer to these two packages. How do I
>>>>> accomplish this without running into re-initialize issues.
>>>>>
>>>>>
>>>>> Rgds,
>>>>> -Mdu
>>>>> Martin Taal wrote:
>>>>>> Hi Mdu,
>>>>>> I see that you add the xmltypepackage here.
>>>>>>
>>>>>> Is the database empty after the re-initialize? (so is the issue
>>>>>> with the querying or with the database being cleared during
>>>>>> reinitialize. In the last case, do you have hibernate.properties
>>>>>> in the root of the classpath which set the update database option
>>>>>> of hibernate to create-drop?
>>>>>>
>>>>>> Can you enable log4j logging for hibernate
>>>>>> (http://www.elver.org/hibernate/samples/log4j.properties) to see
>>>>>> what hibernate does during the re-initialize?
>>>>>>
>>>>>> Note that hibernate has some limitations when updating an existing
>>>>>> db schema, it will create tables, columns, foreign keys, but afaik
>>>>>> no indexes are created (if you have separate index annotations).
>>>>>>
>>>>>> Btw, instead of using one datastore for both the package itself
>>>>>> and the data you can also use a different datastore for the
>>>>>> ecorepackage/xmltypepackage (storing the epackages) and the data
>>>>>> (instances of the eclasses of elvpackage). Unless you have
>>>>>> references from your data to the model then one datastore is
>>>>>> required.
>>>>>>
>>>>>> Hope this helps a bit.
>>>>>>
>>>>>> I am on holiday so my answers take a bit longer than normal.
>>>>>>
>>>>>> gr. Martin
>>>>>>
>>>>>> Mdu wrote:
>>>>>>> Hi All,
>>>>>>> I've gone a little further with a test case. I first run test
>>>>>>> case testStoreEPackages() to create Ecore, XML, and new EPackage
>>>>>>> 'elv'. I'm having a problem in testAddClassToElvEPackage():
>>>>>>> please see a block comment with PROBLEM: section.
>>>>>>>
>>>>>>> Thanks,
>>>>>>> -Mdu
>>>>>>>
>>>>>>> package com.sbide.das.tests;
>>>>>>>
>>>>>>> import java.util.List;
>>>>>>> import java.util.Properties;
>>>>>>>
>>>>>>> import junit.framework.TestCase;
>>>>>>>
>>>>>>>
>>>>>>> import org.eclipse.emf.ecore.EAttribute;
>>>>>>> import org.eclipse.emf.ecore.EClass;
>>>>>>> import org.eclipse.emf.ecore.EObject;
>>>>>>> import org.eclipse.emf.ecore.EPackage;
>>>>>>> import org.eclipse.emf.ecore.EReference;
>>>>>>> import org.eclipse.emf.ecore.EcoreFactory;
>>>>>>> import org.eclipse.emf.ecore.EcorePackage;
>>>>>>> import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
>>>>>>> import org.eclipse.emf.teneo.hibernate.HbDataStore;
>>>>>>> import org.eclipse.emf.teneo.hibernate.HbHelper;
>>>>>>> import org.hibernate.Query;
>>>>>>> import org.hibernate.Session;
>>>>>>> import org.hibernate.SessionFactory;
>>>>>>> import org.hibernate.Transaction;
>>>>>>> import org.hibernate.cfg.Environment;
>>>>>>>
>>>>>>> import com.irg.eaf.common.util.ApplicationProperties;
>>>>>>> import com.sbide.das.EnvironmentConstants;
>>>>>>>
>>>>>>> public class DynamicEPackageTestCase extends TestCase {
>>>>>>>
>>>>>>> public void setUp()
>>>>>>> {
>>>>>>> }
>>>>>>>
>>>>>>> public void testStoreEPackages()
>>>>>>> {
>>>>>>> final EcoreFactory efactory =
>>>>>>> EcoreFactory.eINSTANCE;
>>>>>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>>>>>> final XMLTypePackage xmlpackage =
>>>>>>> XMLTypePackage.eINSTANCE;
>>>>>>> EPackage.Registry.INSTANCE.put(epackage.getNsURI(), epackage);
>>>>>>> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),
>>>>>>> xmlpackage);
>>>>>>> // create the SchoolBook EClass
>>>>>>> EClass schoolBookEClass = efactory.createEClass();
>>>>>>> schoolBookEClass.setName("SchoolBook");
>>>>>>> // create a new attribute for this EClass
>>>>>>> EAttribute level = efactory.createEAttribute();
>>>>>>> level.setName("level");
>>>>>>> level.setEType(epackage.getEInt());
>>>>>>> schoolBookEClass.getEStructuralFeatures().add(level);
>>>>>>>
>>>>>>> // create a course
>>>>>>> EClass courseEClass = efactory.createEClass();
>>>>>>> courseEClass.setName("Course");
>>>>>>> // give the Course a name
>>>>>>> EAttribute courseName = efactory.createEAttribute();
>>>>>>> courseName.setName("courseName");
>>>>>>> courseName.setEType(epackage.getEString());
>>>>>>> courseEClass.getEStructuralFeatures().add(courseName);
>>>>>>> // A course always uses one SchoolBook
>>>>>>> EReference courseBook = efactory.createEReference();
>>>>>>> courseBook.setName("courseBook");
>>>>>>> courseBook.setEType(schoolBookEClass);
>>>>>>> courseBook.setContainment(false);
>>>>>>> courseEClass.getEStructuralFeatures().add(courseBook);
>>>>>>> // Create a new EPackage and add the new EClasses
>>>>>>> EPackage schoolPackage = efactory.createEPackage();
>>>>>>> schoolPackage.setName("elv");
>>>>>>> schoolPackage.setNsPrefix("elv");
>>>>>>> schoolPackage.setNsURI("http:///www.elver.org/School");
>>>>>>> schoolPackage.getEClassifiers().add(courseEClass);
>>>>>>> schoolPackage.getEClassifiers().add(schoolBookEClass);
>>>>>>> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),
>>>>>>> schoolPackage);
>>>>>>> // Now reset the epackages in the datastore
>>>>>>> HbDataStore hbds = createHBDS(new
>>>>>>> EPackage[]{epackage,schoolPackage,xmlpackage});
>>>>>>> // print the hibernate.hbm.xml for demo purposes
>>>>>>> System.err.println(hbds.getMappingXML());
>>>>>>> // and create a course
>>>>>>> EObject course =
>>>>>>> schoolPackage.getEFactoryInstance().create(courseEClass);
>>>>>>> course.eSet(courseName, "Dutch Literature Level 1");
>>>>>>> final SessionFactory sessionFactory =
>>>>>>> hbds.getSessionFactory();
>>>>>>> // now persist them all
>>>>>>> Session session = sessionFactory.openSession();
>>>>>>> Transaction tx = session.getTransaction();
>>>>>>> tx.begin();
>>>>>>> session.save(xmlpackage);
>>>>>>> session.save(epackage);
>>>>>>> session.save(schoolPackage);
>>>>>>> session.save(course);
>>>>>>> tx.commit();
>>>>>>>
>>>>>>> session.close();
>>>>>>> }
>>>>>>> public void testAddClassToElvEPackage()
>>>>>>> {
>>>>>>>
>>>>>>> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
>>>>>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>>>>>> // Now reset the epackages in the datastore
>>>>>>> HbDataStore hbds = createHBDS(new EPackage[]{epackage});
>>>>>>>
>>>>>>> SessionFactory sessionFactory = hbds.getSessionFactory();
>>>>>>> /**
>>>>>>> * Read all relevant epackages to use them for later
>>>>>>> calling setPackages
>>>>>>> */
>>>>>>> Session session = sessionFactory.openSession();
>>>>>>> Transaction tx = session.getTransaction();
>>>>>>> tx.begin();
>>>>>>> Query qry = session.createQuery("from EPackage where
>>>>>>> name='elv'");
>>>>>>> List<EPackage> pkgs = qry.list();
>>>>>>> EPackage elvPkg = pkgs.get(0);
>>>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>>>>>> qry = session.createQuery("from EPackage where
>>>>>>> name='ecore'");
>>>>>>> pkgs = qry.list();
>>>>>>> EPackage ecorePkg = pkgs.get(0);
>>>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>>>>>> qry = session.createQuery("from EPackage where
>>>>>>> name='type'");
>>>>>>> pkgs = qry.list();
>>>>>>> EPackage typePkg = pkgs.get(0);
>>>>>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>>>>>> typePkg); System.out.println("Collection Count:
>>>>>>> "+session.getStatistics().getCollectionCount());
>>>>>>> System.out.println("Entity Count:
>>>>>>> "+session.getStatistics().getEntityCount());
>>>>>>> elvPkg.getEClassifiers().size();
>>>>>>> tx.commit();
>>>>>>> /**
>>>>>>> * Re-initialize store
>>>>>>> */
>>>>>>> hbds.setEPackages((new
>>>>>>> EPackage[]{ecorePkg,elvPkg,typePkg}));
>>>>>>> hbds.initialize(); sessionFactory =
>>>>>>> hbds.getSessionFactory(); session =
>>>>>>> sessionFactory.openSession();
>>>>>>> tx = session.getTransaction();
>>>>>>> /**
>>>>>>> * Re-read epackages - if they are not re-read, they'd be
>>>>>>> still attached to
>>>>>>> * the old session before we called .setEPackages() and
>>>>>>> .initialize():
>>>>>>> *
>>>>>>> * PROBLEM: Each EPackage query below returns empty list
>>>>>>> (qry.list()).
>>>>>>> * QUESTION: 1. I expected these to be still in the
>>>>>>> database - why is the query
>>>>>>> * returning empty results?
>>>>>>> */
>>>>>>> tx.begin();
>>>>>>> qry = session.createQuery("from EPackage where
>>>>>>> name='type'");
>>>>>>> pkgs = qry.list();
>>>>>>> typePkg = pkgs.get(0);
>>>>>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>>>>>> typePkg); qry = session.createQuery("from
>>>>>>> EPackage where name='ecore'");
>>>>>>> pkgs = qry.list();
>>>>>>> ecorePkg = pkgs.get(0);
>>>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>>>>>> qry = session.createQuery("from EPackage where name='elv'");
>>>>>>> pkgs = qry.list();
>>>>>>> elvPkg = pkgs.get(0);
>>>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(),
>>>>>>> elvPkg); tx.commit();
>>>>>>> /**
>>>>>>> * Fetch all three epackages for reference and modification
>>>>>>> */
>>>>>>> tx.begin();
>>>>>>> System.out.println("Collection Count:
>>>>>>> "+session.getStatistics().getCollectionCount());
>>>>>>> System.out.println("Entity Count:
>>>>>>> "+session.getStatistics().getEntityCount());
>>>>>>> qry = session.createQuery("from EPackage where
>>>>>>> name='ecore'");
>>>>>>> pkgs = qry.list();
>>>>>>> ecorePkg = pkgs.get(0);
>>>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(),
>>>>>>> ecorePkg); qry = session.createQuery("from EPackage
>>>>>>> where name='elv'");
>>>>>>> pkgs = qry.list();
>>>>>>> elvPkg = pkgs.get(0);
>>>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>>>>>> tx.commit(); tx.begin();
>>>>>>> EClass testClass1 = efactory.createEClass();
>>>>>>> testClass1.setName("TestClass1");
>>>>>>> EAttribute attr1 = efactory.createEAttribute();
>>>>>>> attr1.setName("intAttr");
>>>>>>> attr1.setEType(epackage.getEInt());
>>>>>>>
>>>>>>> testClass1.getEStructuralFeatures().add(attr1);
>>>>>>> session.save(attr1);
>>>>>>> session.save(testClass1);
>>>>>>> elvPkg.getEClassifiers().add(testClass1);
>>>>>>> session.saveOrUpdate(typePkg);
>>>>>>> session.saveOrUpdate(ecorePkg);
>>>>>>> session.saveOrUpdate(elvPkg); tx.commit();
>>>>>>>
>>>>>>> session.close();
>>>>>>> } public static HbDataStore createHBDS(EPackage[]
>>>>>>> epackages)
>>>>>>> { HbDataStore hbds =
>>>>>>> (HbDataStore)HbHelper.INSTANCE.createRegisterDataStore(Appli cationProperties.get(EnvironmentConstants.TENEO_DS_NAME));
>>>>>>>
>>>>>>>
>>>>>>> final Properties props = new Properties();
>>>>>>> props.setProperty(Environment.DRIVER,
>>>>>>> ApplicationProperties.get(Environment.DRIVER));
>>>>>>> props.setProperty(Environment.USER,
>>>>>>> ApplicationProperties.get(Environment.USER));
>>>>>>> props.setProperty(Environment.URL,
>>>>>>> ApplicationProperties.get(Environment.URL));
>>>>>>> props.setProperty(Environment.PASS,
>>>>>>> ApplicationProperties.get(Environment.PASS));
>>>>>>> props.setProperty(Environment.DIALECT,
>>>>>>> ApplicationProperties.get(Environment.DIALECT));
>>>>>>> hbds.setProperties(props);
>>>>>>> hbds.setEPackages(epackages);
>>>>>>> hbds.initialize();
>>>>>>> return hbds;
>>>>>>> } }
>>>>>>> Mdu wrote:
>>>>>>>> Hi,
>>>>>>>> I'm just about given up with the previous trial-and-error in
>>>>>>>> working with EPackages stored via teneo. Is there an example (I
>>>>>>>> also looked through Teneo test cases) that demonstrates the
>>>>>>>> following:
>>>>>>>>
>>>>>>>> 1. Create a dynamic (empty) package and save it using Teneo
>>>>>>>> 'session.save()'.
>>>>>>>>
>>>>>>>> 2. Load the package in 1. properly (registration and all) and
>>>>>>>> add a simple EClass to it; save it back to using 'session.save()'.
>>>>>>>>
>>>>>>>> 3. Load the package again and use it to create an EOBject from a
>>>>>>>> class added in 2.; and save the newly created EObject.
>>>>>>>>
>>>>>>>> 4. Do 1., 2., and 3. in separate test cases?
>>>>>>>>
>>>>>>>>
>>>>>>>> I'm using the recent version of Teneo (1.0.1) and dependent EMF
>>>>>>>> libraries.
>>>>>>>> Any help will be much appreciated!
>>>>>>>>
>>>>>>>> Rgds,
>>>>>>>> -Mdu
>>>>>>
>>>>>>
>>>>
>>>>
>>
>>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] Example to dynamically modify EPackages stored using Teneo - Got it! [message #426388 is a reply to message #426381] Fri, 02 January 2009 02:21 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mduduzi.keswa.isizwe.com

Happy New Year Martin - I finally figured out how to incorporate all
your suggestions:

Lessons Learnt (for me):
1. Use EcorePackage.eINSTANCE and XMLTypePackage.eINSTANCE for
setEPackages()

2. When creating dynamic packages/classes/attributes, use structural
features loaded from persistent versions of EcorePackage.eINSTANCE and
XMLTypePackage.eINSTANCE.

....and other subtleties on Teneo. At least I know now how much I don't
know - this is a comfort spot for me on the learning curve.


See updated test cases below:

Thanks once more,
-Mdu
-----------xxxxx---------------

public void testPersistDynamicPackage_Case1Step1()
{
//--
final EcoreFactory efactory = EcoreFactory.eINSTANCE;
final EcorePackage epackage = EcorePackage.eINSTANCE;
final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;

//--
EPackage.Registry.INSTANCE.put(epackage.getNsURI(),epackage) ;
EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),xmlpack age);

//--
EPackage schoolPackage = efactory.createEPackage();
schoolPackage.setName("SchoolPackage");
schoolPackage.setNsPrefix("SchoolPackage");
schoolPackage.setNsURI("http:///www.elver.org/School");

EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),scho olPackage);

EClass schoolBookEClass = efactory.createEClass();
schoolBookEClass.setName("SchoolBook");
EAttribute level = efactory.createEAttribute();
level.setName("level");
level.setEType(epackage.getEInt());
schoolBookEClass.getEStructuralFeatures().add(level);
schoolPackage.getEClassifiers().add(schoolBookEClass);
//--
HbDataStore emfMDRepoHBS = DbUtil.createEMFRepoHBDS(new
EPackage[]{epackage,xmlpackage,schoolPackage});
SessionFactory emfRepoSessionFactory =
emfMDRepoHBS.getSessionFactory();
Session emfRepoSession = emfRepoSessionFactory.openSession();
Transaction txEmfRepo = emfRepoSession.getTransaction();

txEmfRepo.begin();
emfRepoSession.save(epackage);
emfRepoSession.save(xmlpackage);
emfRepoSession.save(schoolPackage);
txEmfRepo.commit();

emfRepoSession.close();
}

public void testModifyDynamicPackage_Case1Step2()
{
//-- Declare factories and built-in packages
final EcoreFactory efactory = EcoreFactory.eINSTANCE;
final EcorePackage epackage = EcorePackage.eINSTANCE;
final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;

//-- Is this necessary?
EPackage.Registry.INSTANCE.put(epackage.getNsURI(),epackage) ;
EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),xmlpack age);

//-- Initialize Hb DS
HbDataStore emfMDRepoHBS = DbUtil.createEMFRepoHBDS(new
EPackage[]{epackage,xmlpackage});
SessionFactory emfRepoSessionFactory =
emfMDRepoHBS.getSessionFactory();
Session emfRepoSession = emfRepoSessionFactory.openSession();
Transaction txEmfRepo = emfRepoSession.getTransaction();

//-- Load Hb stored Ecore and XMLType packages
txEmfRepo.begin();
Query query = emfRepoSession.createQuery("from EPackage WHERE
name='ecore'");
EPackage eCorePkg = (EPackage)query.list().get(0);
Registry.INSTANCE.put(eCorePkg.getNsURI(),eCorePkg);

query = emfRepoSession.createQuery("from EPackage WHERE name='type'");
EPackage xmlTypePkg = (EPackage)query.list().get(0);
Registry.INSTANCE.put(xmlTypePkg.getNsURI(),xmlTypePkg);

query = emfRepoSession.createQuery("from EPackage WHERE
name='SchoolPackage'");
EPackage schPkg = (EPackage)query.list().get(0);
Registry.INSTANCE.put(schPkg.getNsURI(),schPkg);
txEmfRepo.commit();


//-- Re-initialize Hb DS: Set packages with Teneo - use un-persistent
Ecore/XMLType
//-- packages.
emfMDRepoHBS = DbUtil.createEMFRepoHBDS(new
EPackage[]{epackage,xmlpackage,schPkg});
emfRepoSessionFactory = emfMDRepoHBS.getSessionFactory();
emfRepoSession = emfRepoSessionFactory.openSession();

txEmfRepo = emfRepoSession.getTransaction();

//-- Lookup dynamic types and packages for modification
txEmfRepo.begin();
query = emfRepoSession.createQuery("from EDataType WHERE name='EInt'");
EDataType eIntDT = (EDataType)query.list().get(0);

query = emfRepoSession.createQuery("from EPackage WHERE
name='SchoolPackage'");
EPackage schoolPkg = (EPackage)query.list().get(0);
EPackage.Registry.INSTANCE.put(schoolPkg.getNsURI(),schoolPk g);

//-- Create custom class/attribute to add package
EClass schoolBookEClass = efactory.createEClass();
schoolBookEClass.setName("SchoolBook4");
emfRepoSession.save(schoolBookEClass);
EAttribute level = efactory.createEAttribute();
level.setName("level4");
level.setEType(eIntDT); //Use DataType loaded from Hb DS
schoolBookEClass.getEStructuralFeatures().add(level);
schoolPkg.getEClassifiers().add(schoolBookEClass);
emfRepoSession.save(schoolPkg);
txEmfRepo.commit();

emfRepoSession.close();
}

public void testCreateDynamicPackageEObjects_Case1Step3()
{
//-- Declare factories and built-in packages
final EcoreFactory efactory = EcoreFactory.eINSTANCE;
final EcorePackage epackage = EcorePackage.eINSTANCE;
final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;

//-- Is this necessary?
EPackage.Registry.INSTANCE.put(epackage.getNsURI(),epackage) ;
EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),xmlpack age);

//-- Initialize Meatadata Hb DS
HbDataStore emfMDRepoHBS = DbUtil.createEMFRepoHBDS(new
EPackage[]{epackage,xmlpackage});
SessionFactory emfRepoSessionFactory =
emfMDRepoHBS.getSessionFactory();
Session emfRepoSession = emfRepoSessionFactory.openSession();
Transaction txEmfRepo = emfRepoSession.getTransaction();

//-- Load Hb stored Ecore and XMLType packages
txEmfRepo.begin();
Query query = emfRepoSession.createQuery("from EPackage WHERE
name='SchoolPackage'");
EPackage schPkg = (EPackage)query.list().get(0);
Registry.INSTANCE.put(schPkg.getNsURI(),schPkg);
txEmfRepo.commit();

//-- Create eobjects
HbDataStore sbMDRepoHBS = DbUtil.createSBMDRepoHBDS(new
EPackage[]{epackage,xmlpackage,schPkg});
SessionFactory sbRepoSessionFactory = sbMDRepoHBS.getSessionFactory();
Session sbRepoSession = sbRepoSessionFactory.openSession();
Transaction txSbRepo = sbRepoSession.getTransaction();

txSbRepo.begin();

EClass schoolBookEClass = (EClass)schPkg.getEClassifier("SchoolBook");
EAttribute schoolBookLevelEAttr =
(EAttribute)schoolBookEClass.getEStructuralFeature("level");
EObject englishSchoolBook =
schPkg.getEFactoryInstance().create((EClass)schoolBookEClass );
englishSchoolBook.eSet(schoolBookLevelEAttr, 2);

sbRepoSession.save(englishSchoolBook);

txSbRepo.commit();

sbRepoSession.close();
}

------------xxxxxxxxxxxxxxxx-----------------
1. Martin Taal wrote:
> Hi Mdu,
> Sorry my confusion/mistake, you also need to store the ecore package and
> the xmlpackage in the emfMDRepoHBS.
>
> gr. Martin
>
> Mdu wrote:
>> Thanks Martin for being patient with this. I have gone a little further:
>>
>> SUMMARY:
>> Error occurs when saving the EDataType of the attribute 'level' below
>> (see TRACE and ERROR).
>> BTW- The tables (including ecore_eobject and SchoolBool) are being
>> created - but obviously no data in them.
>>
>> 1. Test Case:
>> public void testPersistDynamicPackage()
>> {
>> //--
>> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>> final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;
>> //--
>> EPackage.Registry.INSTANCE.put(epackage.getNsURI(),epackage) ;
>> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),xmlpack age);
>>
>> //--
>> EPackage schoolPackage = efactory.createEPackage();
>> schoolPackage.setName("SchoolPackage");
>> schoolPackage.setNsPrefix("SchoolPackage");
>> schoolPackage.setNsURI("http:///www.elver.org/School");
>>
>> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),scho olPackage);
>> EClass schoolBookEClass = efactory.createEClass();
>> schoolBookEClass.setName("SchoolBook");
>> EAttribute level = efactory.createEAttribute();
>> level.setName("level");
>> level.setEType(epackage.getEInt());
>> schoolBookEClass.getEStructuralFeatures().add(level);
>> schoolPackage.getEClassifiers().add(schoolBookEClass);
>> //--
>> HbDataStore emfMDRepoHBS = DbUtil.createEMFRepoHBDS(new
>> EPackage[]{epackage,xmlpackage});
>> SessionFactory emfRepoSessionFactory =
>> emfMDRepoHBS.getSessionFactory();
>> Session emfRepoSession = emfRepoSessionFactory.openSession();
>> Transaction txEmfRepo = emfRepoSession.getTransaction();
>> txEmfRepo.begin();
>> emfRepoSession.save(schoolPackage);
>> txEmfRepo.commit();
>> emfRepoSession.close(); }
>> 2. DEBUG TRACE:
>> 6059 [main] DEBUG
>> org.eclipse.emf.teneo.hibernate.mapping.identifier.Identifie rCacheHandler
>> - ID for object org.eclipse.emf.ecore.impl.EDataTypeImpl not found in
>> id cache
>> 6059 [main] DEBUG
>> org.eclipse.emf.teneo.hibernate.mapping.identifier.Identifie rCacheHandler
>> - ID for object org.eclipse.emf.ecore.impl.EDataTypeImpl not found in
>> id cache
>>
>>
>> 3. ERROR (EXCEPTION):
>> org.hibernate.TransientObjectException: object references an unsaved
>> transient instance - save the transient instance before flushing:
>> EClassifier
>> at
>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>
>> at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>> at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java: 265)
>> at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:61 9)
>> at
>> org.hibernate.persister.entity.AbstractEntityPersister.findD irty(AbstractEntityPersister.java:3151)
>>
>> at
>> org.hibernate.event.def.DefaultFlushEntityEventListener.dirt yCheck(DefaultFlushEntityEventListener.java:501)
>>
>> at
>> org.hibernate.event.def.DefaultFlushEntityEventListener.isUp dateNecessary(DefaultFlushEntityEventListener.java:227)
>>
>> at
>> org.hibernate.event.def.DefaultFlushEntityEventListener.onFl ushEntity(DefaultFlushEntityEventListener.java:150)
>>
>> at
>> org.hibernate.event.def.AbstractFlushingEventListener.flushE ntities(AbstractFlushingEventListener.java:219)
>>
>> at
>> org.hibernate.event.def.AbstractFlushingEventListener.flushE verythingToExecutions(AbstractFlushingEventListener.java:99)
>>
>> at
>> org.hibernate.event.def.DefaultFlushEventListener.onFlush(De faultFlushEventListener.java:49)
>>
>> at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
>> at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java :365)
>> at
>> org.hibernate.transaction.JDBCTransaction.commit(JDBCTransac tion.java:137)
>>
>> at
>> com.sbide.das.tests.EcoreDTOUtilTestCase2.testPersistDynamic Package(EcoreDTOUtilTestCase2.java:105)
>>
>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>> at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce ssorImpl.java:39)
>>
>> at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe thodAccessorImpl.java:25)
>>
>> at java.lang.reflect.Method.invoke(Method.java:585)
>> at junit.framework.TestCase.runTest(TestCase.java:164)
>> at junit.framework.TestCase.runBare(TestCase.java:130)
>> at junit.framework.TestResult$1.protect(TestResult.java:106)
>> at junit.framework.TestResult.runProtected(TestResult.java:124)
>> at junit.framework.TestResult.run(TestResult.java:109)
>> at junit.framework.TestCase.run(TestCase.java:120)
>> at
>> org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestRefer ence.run(JUnit3TestReference.java:130)
>>
>> at
>> org.eclipse.jdt.internal.junit.runner.TestExecution.run(Test Execution.java:38)
>>
>> at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:460)
>>
>> at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:673)
>>
>> at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(R emoteTestRunner.java:386)
>>
>> at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main( RemoteTestRunner.java:196)
>>
>>
>>
>>
>>
>> Martin Taal wrote:
>>> Hi Mdu,
>>> See comments inline.
>>>
>>> Regarding not getting results when reading epackages:
>>> When you check the database, is the epackage present in the database?
>>>
>>> gr. Martin
>>>
>>> Mdu wrote:
>>>> Hi Martin - thanks for taking the time form your well deserved
>>>> break! See my comments below:
>>>> Martin Taal wrote:
>>>>> Hi Mdu,
>>>>> Create-drop will drop the database when re-initializing the
>>>>> datastore, so in your case I would use update.
>>>> I tried my 'update' option - and still have the same problem of a
>>>> query that returns no epackages after re-initialize; and also got
>>>> the following errors during re-initalize:
>>>>
>>>> 51027 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter
>>>> table `eobject` add index emodelelement_eannotations
>>>> (`eannotation_emodelelement_e_id`), add constraint
>>>> emodelelement_eannotations foreign key
>>>> (`eannotation_emodelelement_e_id`) references `eobject` (e_id)
>>>> 51209 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate -
>>>> Unsuccessful: alter table `eobject` add index
>>>> emodelelement_eannotations (`eannotation_emodelelement_e_id`), add
>>>> constraint emodelelement_eannotations foreign key
>>>> (`eannotation_emodelelement_e_id`) references `eobject` (e_id)
>>>> 51209 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - Can't
>>>> create table 'solutionassembly.#sql-1208_1c' (errno: 121)
>>>>>
>>> MT>> Does the log earlier show errors when doing create table?
>>>
>>>>> Answers:
>>>>> 1) I did not completely understand the question, if the elv package
>>>>> changes then new tables/columns need to be created this is done
>>>>> when (re-)initializing the datastore.
>>>>> 2) You can use 2 datastores (one for the ecore/xml package and one
>>>>> for the elv package) if there are no direct references from elv to
>>>>> ecore. With direct I mean that you did not specify in your model an
>>>>> ereference which references the EClass type in the ecore package
>>>>> (or eattribute, or ereference). Re-initializating should not be a
>>>>> problem with the hibernate update option.
>>>> This is where my ignorance shows a lot. Here's my understand how
>>>> what you are suggesting would work:
>>>>
>>>> Let DS_Elv be data store for Elv package;
>>>> DS_Ecore be data store for Ecore and XMLType packages
>>>> HBDS_Elv be HB data source for DS_Elv
>>>> HBDS_Ecore be HB data source for DS_Ecore
>>>> Ssn_Elv be session from HBDS_Elv
>>>> Ssn_Ecore be session from HBDS_Ecore
>>>> ecorePkg and xmlTypePkg be persistent packages saved and loaded
>>>> via Ssn_Ecore
>>> MT>> What do you mean with Hb data source? The datastore?
>>>
>>> It is not necessary to store and retrieve the ecore and xml packages
>>> from the db as they won't change.
>>>
>>> I would say that:
>>> - The HBDS_Ecore will store instances of the Ecore package (not the
>>> ecore package itself), the Elv EPackage is an instance of the ecore
>>> epackage, so the HBDS_Ecore will store the Elv EPackage itself.
>>> - The HBDS_Elv will store instances of the Elv model (so assume that
>>> the Elv EPackage has an eClass Teneo, then this store will save an
>>> EObject which has eClass Teneo).
>>>
>>> With this in mind I will react below.
>>>
>>>>
>>>> 1. Create HBDS_Elv:
>>>> - HBDS_Elv.setEPackages({*}) - should * be ecorePkg and
>>>> xmlTypePkg from Ssn_Ecore?
>>>> - HBDS_Elv.initialize()
>>> MT>> I would say that * is the Elv Package
>>>
>>> To initialize the HBDS_Ecore:
>>> HBDS_Ecore.setEPackages(new EPackage[]{ecorepkg, xmlpkg})
>>> HBDS_Ecore.initialize();
>>>
>>>>
>>>> 2. Create and save Elv package 'elvPkg':
>>>> - Obtain Ssn_Elv from HBDS_Elv sessionFactory
>>>> - Create elvPkg and class 'class1' with 'attr1'
>>>> - Transactionally call Ssn_Elv.save(elvPkg)
>>> MT>> Obtain Ssn_Ecore from HBDS_Ecore and call Ssn_Ecore.save(elvPkg)
>>> (and commit the transaction)
>>>
>>>> QUESTION: Doesn't this require persistent ecorePkg and xmlTypePkg
>>>> implicilty to save 'class1' and 'attr1' i.e. instances from
>>>> Ssn_Ecore. If so, wouldn't I get errors when I save a elvPkg that
>>>> implicitly depends on epackages loaded from Ssn_Ecore - a separate
>>>> session?
>>>>
>>>> 3. Read elvPkg back for modification:
>>>> - HBDS_Elv.setEPackages({**}) - should ** be same ecorePkg and
>>>> xmlTypePkg from Ssn_Ecore?
>>>> - HBDS_Elv.initialize()
>>>> - Create query 'from EPackage where name='Elv'
>>>> - Assign elvPkg to the result
>>>> - Add another 'class2' with 'attr2' to elvPkg
>>>> - Transactionally call Ssn_Elv.save(elvPkg)
>>>> QUESTION: Same question as above
>>> MT>> Instead of HBDS_Elv I would you use HBDS_Ecore here.
>>> After doing Ssn_Ecore.save(elvPkg) re-initialize the HBDS_Elv to
>>> update the database schema.
>>>
>>> Also be aware that there is a global EPackage registry which stores
>>> mappings from nsuri to EPackages. Also a resourceset has an epackage
>>> registry which stores this relation. So after reading the elvpackage
>>> from the database set it in the global epackage registry.
>>>
>>>>
>>>> The only way I could see this working from here is by:
>>>> Everytime I change elvPkg, I first have to read it; clone it using
>>>> elvPkgClone = EcoreUtil.copy(); modify it; HBDS_Env.initialize();
>>>> and Ssn_Elv.save(elvPkgClone).
>>> MT>> Afaics you don't need to clone, I would use HBDS_Ecore.
>>>
>>>>
>>>> Unless I fix my Hibernate (errno: 121) errors - assuming that's what
>>>> preventing 'update' from working; this is my only choice. I also
>>>> realize that I'm starting to meddle 'model evolution' issues here as
>>>> well - that is how to evolve metamodels to be always in sync with
>>>> their underlying models. I noticed that this has been discussed on
>>>> CDO forums a bit.
>>> MT>> Yes it is also a topic for CDO. The update of the database
>>> schema is one part of the story. In the end changing relational
>>> database schemes on the fly is a touchy thing, on the other hand
>>> adding nullable columns and new tables should work fine.
>>>
>>>>
>>>> Regards,
>>>> -Mdu
>>>>
>>>>>
>>>>> gr. Martin
>>>>>
>>>>> Mdu wrote:
>>>>>> Thanks Martin,
>>>>>> I turned on debugging and used create-drop option. Part of the
>>>>>> log shows:
>>>>>>
>>>>>>
>>>>>> 4476 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>> table if exists `eannotation_contents`
>>>>>> 4479 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>> table if exists `eannotation_references`
>>>>>> 4480 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>> table if exists `eclass_esupertypes`
>>>>>> 4483 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>> table if exists `ecore_eobject`
>>>>>> 4485 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>> table if exists `eoperation_eexceptions`
>>>>>> 4486 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>> table if exists `ereference_ekeys`
>>>>>> 4488 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>> table if exists details
>>>>>>
>>>>>>
>>>>>> Questions:
>>>>>> 1. Since I'm neither changing EcorePackage('ecore') nor
>>>>>> XMLTypePackage('type') - only modifying package 'elv', why do I
>>>>>> have to re-initialize HbDataStore?
>>>>>>
>>>>>>
>>>>>> 2. It makes sense to store EcorePackage and XMLTypePackage in a
>>>>>> separate HBDS as the are static, I still need to update custom
>>>>>> EPackages (e.g. 'elv') which refer to these two packages. How do I
>>>>>> accomplish this without running into re-initialize issues.
>>>>>>
>>>>>>
>>>>>> Rgds,
>>>>>> -Mdu
>>>>>> Martin Taal wrote:
>>>>>>> Hi Mdu,
>>>>>>> I see that you add the xmltypepackage here.
>>>>>>>
>>>>>>> Is the database empty after the re-initialize? (so is the issue
>>>>>>> with the querying or with the database being cleared during
>>>>>>> reinitialize. In the last case, do you have hibernate.properties
>>>>>>> in the root of the classpath which set the update database option
>>>>>>> of hibernate to create-drop?
>>>>>>>
>>>>>>> Can you enable log4j logging for hibernate
>>>>>>> (http://www.elver.org/hibernate/samples/log4j.properties) to see
>>>>>>> what hibernate does during the re-initialize?
>>>>>>>
>>>>>>> Note that hibernate has some limitations when updating an
>>>>>>> existing db schema, it will create tables, columns, foreign keys,
>>>>>>> but afaik no indexes are created (if you have separate index
>>>>>>> annotations).
>>>>>>>
>>>>>>> Btw, instead of using one datastore for both the package itself
>>>>>>> and the data you can also use a different datastore for the
>>>>>>> ecorepackage/xmltypepackage (storing the epackages) and the data
>>>>>>> (instances of the eclasses of elvpackage). Unless you have
>>>>>>> references from your data to the model then one datastore is
>>>>>>> required.
>>>>>>>
>>>>>>> Hope this helps a bit.
>>>>>>>
>>>>>>> I am on holiday so my answers take a bit longer than normal.
>>>>>>>
>>>>>>> gr. Martin
>>>>>>>
>>>>>>> Mdu wrote:
>>>>>>>> Hi All,
>>>>>>>> I've gone a little further with a test case. I first run test
>>>>>>>> case testStoreEPackages() to create Ecore, XML, and new EPackage
>>>>>>>> 'elv'. I'm having a problem in testAddClassToElvEPackage():
>>>>>>>> please see a block comment with PROBLEM: section.
>>>>>>>>
>>>>>>>> Thanks,
>>>>>>>> -Mdu
>>>>>>>>
>>>>>>>> package com.sbide.das.tests;
>>>>>>>>
>>>>>>>> import java.util.List;
>>>>>>>> import java.util.Properties;
>>>>>>>>
>>>>>>>> import junit.framework.TestCase;
>>>>>>>>
>>>>>>>>
>>>>>>>> import org.eclipse.emf.ecore.EAttribute;
>>>>>>>> import org.eclipse.emf.ecore.EClass;
>>>>>>>> import org.eclipse.emf.ecore.EObject;
>>>>>>>> import org.eclipse.emf.ecore.EPackage;
>>>>>>>> import org.eclipse.emf.ecore.EReference;
>>>>>>>> import org.eclipse.emf.ecore.EcoreFactory;
>>>>>>>> import org.eclipse.emf.ecore.EcorePackage;
>>>>>>>> import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
>>>>>>>> import org.eclipse.emf.teneo.hibernate.HbDataStore;
>>>>>>>> import org.eclipse.emf.teneo.hibernate.HbHelper;
>>>>>>>> import org.hibernate.Query;
>>>>>>>> import org.hibernate.Session;
>>>>>>>> import org.hibernate.SessionFactory;
>>>>>>>> import org.hibernate.Transaction;
>>>>>>>> import org.hibernate.cfg.Environment;
>>>>>>>>
>>>>>>>> import com.irg.eaf.common.util.ApplicationProperties;
>>>>>>>> import com.sbide.das.EnvironmentConstants;
>>>>>>>>
>>>>>>>> public class DynamicEPackageTestCase extends TestCase {
>>>>>>>>
>>>>>>>> public void setUp()
>>>>>>>> {
>>>>>>>> }
>>>>>>>>
>>>>>>>> public void testStoreEPackages()
>>>>>>>> {
>>>>>>>> final EcoreFactory efactory =
>>>>>>>> EcoreFactory.eINSTANCE;
>>>>>>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>>>>>>> final XMLTypePackage xmlpackage =
>>>>>>>> XMLTypePackage.eINSTANCE;
>>>>>>>> EPackage.Registry.INSTANCE.put(epackage.getNsURI(), epackage);
>>>>>>>> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),
>>>>>>>> xmlpackage);
>>>>>>>> // create the SchoolBook EClass
>>>>>>>> EClass schoolBookEClass = efactory.createEClass();
>>>>>>>> schoolBookEClass.setName("SchoolBook");
>>>>>>>> // create a new attribute for this EClass
>>>>>>>> EAttribute level = efactory.createEAttribute();
>>>>>>>> level.setName("level");
>>>>>>>> level.setEType(epackage.getEInt());
>>>>>>>> schoolBookEClass.getEStructuralFeatures().add(level);
>>>>>>>>
>>>>>>>> // create a course
>>>>>>>> EClass courseEClass = efactory.createEClass();
>>>>>>>> courseEClass.setName("Course");
>>>>>>>> // give the Course a name
>>>>>>>> EAttribute courseName = efactory.createEAttribute();
>>>>>>>> courseName.setName("courseName");
>>>>>>>> courseName.setEType(epackage.getEString());
>>>>>>>> courseEClass.getEStructuralFeatures().add(courseName);
>>>>>>>> // A course always uses one SchoolBook
>>>>>>>> EReference courseBook = efactory.createEReference();
>>>>>>>> courseBook.setName("courseBook");
>>>>>>>> courseBook.setEType(schoolBookEClass);
>>>>>>>> courseBook.setContainment(false);
>>>>>>>> courseEClass.getEStructuralFeatures().add(courseBook);
>>>>>>>> // Create a new EPackage and add the new EClasses
>>>>>>>> EPackage schoolPackage = efactory.createEPackage();
>>>>>>>> schoolPackage.setName("elv");
>>>>>>>> schoolPackage.setNsPrefix("elv");
>>>>>>>> schoolPackage.setNsURI("http:///www.elver.org/School");
>>>>>>>> schoolPackage.getEClassifiers().add(courseEClass);
>>>>>>>> schoolPackage.getEClassifiers().add(schoolBookEClass);
>>>>>>>> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),
>>>>>>>> schoolPackage);
>>>>>>>> // Now reset the epackages in the datastore
>>>>>>>> HbDataStore hbds = createHBDS(new
>>>>>>>> EPackage[]{epackage,schoolPackage,xmlpackage});
>>>>>>>> // print the hibernate.hbm.xml for demo purposes
>>>>>>>> System.err.println(hbds.getMappingXML());
>>>>>>>> // and create a course
>>>>>>>> EObject course =
>>>>>>>> schoolPackage.getEFactoryInstance().create(courseEClass);
>>>>>>>> course.eSet(courseName, "Dutch Literature Level 1");
>>>>>>>> final SessionFactory sessionFactory =
>>>>>>>> hbds.getSessionFactory();
>>>>>>>> // now persist them all
>>>>>>>> Session session = sessionFactory.openSession();
>>>>>>>> Transaction tx = session.getTransaction();
>>>>>>>> tx.begin();
>>>>>>>> session.save(xmlpackage);
>>>>>>>> session.save(epackage);
>>>>>>>> session.save(schoolPackage);
>>>>>>>> session.save(course);
>>>>>>>> tx.commit();
>>>>>>>>
>>>>>>>> session.close();
>>>>>>>> }
>>>>>>>> public void testAddClassToElvEPackage()
>>>>>>>> {
>>>>>>>>
>>>>>>>> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
>>>>>>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>>>>>>> // Now reset the epackages in the datastore
>>>>>>>> HbDataStore hbds = createHBDS(new EPackage[]{epackage});
>>>>>>>>
>>>>>>>> SessionFactory sessionFactory = hbds.getSessionFactory();
>>>>>>>> /**
>>>>>>>> * Read all relevant epackages to use them for later
>>>>>>>> calling setPackages
>>>>>>>> */
>>>>>>>> Session session = sessionFactory.openSession();
>>>>>>>> Transaction tx = session.getTransaction();
>>>>>>>> tx.begin();
>>>>>>>> Query qry = session.createQuery("from EPackage where
>>>>>>>> name='elv'");
>>>>>>>> List<EPackage> pkgs = qry.list();
>>>>>>>> EPackage elvPkg = pkgs.get(0);
>>>>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>>>>>>> qry = session.createQuery("from EPackage where
>>>>>>>> name='ecore'");
>>>>>>>> pkgs = qry.list();
>>>>>>>> EPackage ecorePkg = pkgs.get(0);
>>>>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>>>>>>> qry = session.createQuery("from EPackage where
>>>>>>>> name='type'");
>>>>>>>> pkgs = qry.list();
>>>>>>>> EPackage typePkg = pkgs.get(0);
>>>>>>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>>>>>>> typePkg); System.out.println("Collection Count:
>>>>>>>> "+session.getStatistics().getCollectionCount());
>>>>>>>> System.out.println("Entity Count:
>>>>>>>> "+session.getStatistics().getEntityCount());
>>>>>>>> elvPkg.getEClassifiers().size();
>>>>>>>> tx.commit();
>>>>>>>> /**
>>>>>>>> * Re-initialize store
>>>>>>>> */
>>>>>>>> hbds.setEPackages((new
>>>>>>>> EPackage[]{ecorePkg,elvPkg,typePkg}));
>>>>>>>> hbds.initialize(); sessionFactory =
>>>>>>>> hbds.getSessionFactory(); session =
>>>>>>>> sessionFactory.openSession();
>>>>>>>> tx = session.getTransaction();
>>>>>>>> /**
>>>>>>>> * Re-read epackages - if they are not re-read, they'd
>>>>>>>> be still attached to
>>>>>>>> * the old session before we called .setEPackages() and
>>>>>>>> .initialize():
>>>>>>>> *
>>>>>>>> * PROBLEM: Each EPackage query below returns empty list
>>>>>>>> (qry.list()).
>>>>>>>> * QUESTION: 1. I expected these to be still in the
>>>>>>>> database - why is the query
>>>>>>>> * returning empty results?
>>>>>>>> */
>>>>>>>> tx.begin();
>>>>>>>> qry = session.createQuery("from EPackage where
>>>>>>>> name='type'");
>>>>>>>> pkgs = qry.list();
>>>>>>>> typePkg = pkgs.get(0);
>>>>>>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>>>>>>> typePkg); qry = session.createQuery("from
>>>>>>>> EPackage where name='ecore'");
>>>>>>>> pkgs = qry.list();
>>>>>>>> ecorePkg = pkgs.get(0);
>>>>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>>>>>>> qry = session.createQuery("from EPackage where
>>>>>>>> name='elv'");
>>>>>>>> pkgs = qry.list();
>>>>>>>> elvPkg = pkgs.get(0);
>>>>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(),
>>>>>>>> elvPkg); tx.commit();
>>>>>>>> /**
>>>>>>>> * Fetch all three epackages for reference and modification
>>>>>>>> */
>>>>>>>> tx.begin();
>>>>>>>> System.out.println("Collection Count:
>>>>>>>> "+session.getStatistics().getCollectionCount());
>>>>>>>> System.out.println("Entity Count:
>>>>>>>> "+session.getStatistics().getEntityCount());
>>>>>>>> qry = session.createQuery("from EPackage where
>>>>>>>> name='ecore'");
>>>>>>>> pkgs = qry.list();
>>>>>>>> ecorePkg = pkgs.get(0);
>>>>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(),
>>>>>>>> ecorePkg); qry = session.createQuery("from
>>>>>>>> EPackage where name='elv'");
>>>>>>>> pkgs = qry.list();
>>>>>>>> elvPkg = pkgs.get(0);
>>>>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>>>>>>> tx.commit(); tx.begin();
>>>>>>>> EClass testClass1 = efactory.createEClass();
>>>>>>>> testClass1.setName("TestClass1");
>>>>>>>> EAttribute attr1 = efactory.createEAttribute();
>>>>>>>> attr1.setName("intAttr");
>>>>>>>> attr1.setEType(epackage.getEInt());
>>>>>>>>
>>>>>>>> testClass1.getEStructuralFeatures().add(attr1);
>>>>>>>> session.save(attr1);
>>>>>>>> session.save(testClass1);
>>>>>>>> elvPkg.getEClassifiers().add(testClass1);
>>>>>>>> session.saveOrUpdate(typePkg);
>>>>>>>> session.saveOrUpdate(ecorePkg);
>>>>>>>> session.saveOrUpdate(elvPkg); tx.commit();
>>>>>>>>
>>>>>>>> session.close();
>>>>>>>> } public static HbDataStore createHBDS(EPackage[]
>>>>>>>> epackages)
>>>>>>>> { HbDataStore hbds =
>>>>>>>> (HbDataStore)HbHelper.INSTANCE.createRegisterDataStore(Appli cationProperties.get(EnvironmentConstants.TENEO_DS_NAME));
>>>>>>>>
>>>>>>>>
>>>>>>>> final Properties props = new Properties();
>>>>>>>> props.setProperty(Environment.DRIVER,
>>>>>>>> ApplicationProperties.get(Environment.DRIVER));
>>>>>>>> props.setProperty(Environment.USER,
>>>>>>>> ApplicationProperties.get(Environment.USER));
>>>>>>>> props.setProperty(Environment.URL,
>>>>>>>> ApplicationProperties.get(Environment.URL));
>>>>>>>> props.setProperty(Environment.PASS,
>>>>>>>> ApplicationProperties.get(Environment.PASS));
>>>>>>>> props.setProperty(Environment.DIALECT,
>>>>>>>> ApplicationProperties.get(Environment.DIALECT));
>>>>>>>> hbds.setProperties(props);
>>>>>>>> hbds.setEPackages(epackages);
>>>>>>>> hbds.initialize();
>>>>>>>> return hbds;
>>>>>>>> } }
>>>>>>>> Mdu wrote:
>>>>>>>>> Hi,
>>>>>>>>> I'm just about given up with the previous trial-and-error in
>>>>>>>>> working with EPackages stored via teneo. Is there an example (I
>>>>>>>>> also looked through Teneo test cases) that demonstrates the
>>>>>>>>> following:
>>>>>>>>>
>>>>>>>>> 1. Create a dynamic (empty) package and save it using Teneo
>>>>>>>>> 'session.save()'.
>>>>>>>>>
>>>>>>>>> 2. Load the package in 1. properly (registration and all) and
>>>>>>>>> add a simple EClass to it; save it back to using 'session.save()'.
>>>>>>>>>
>>>>>>>>> 3. Load the package again and use it to create an EOBject from
>>>>>>>>> a class added in 2.; and save the newly created EObject.
>>>>>>>>>
>>>>>>>>> 4. Do 1., 2., and 3. in separate test cases?
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> I'm using the recent version of Teneo (1.0.1) and dependent EMF
>>>>>>>>> libraries.
>>>>>>>>> Any help will be much appreciated!
>>>>>>>>>
>>>>>>>>> Rgds,
>>>>>>>>> -Mdu
>>>>>>>
>>>>>>>
>>>>>
>>>>>
>>>
>>>
>
>
Re: [Teneo] Example to dynamically modify EPackages stored using Teneo - Got it! [message #426390 is a reply to message #426388] Fri, 02 January 2009 10:34 Go to previous message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Mdu,
Thanks for providing the summary and ofcourse also a happy new year!

gr. Martin

Mdu wrote:
> Happy New Year Martin - I finally figured out how to incorporate all
> your suggestions:
>
> Lessons Learnt (for me):
> 1. Use EcorePackage.eINSTANCE and XMLTypePackage.eINSTANCE for
> setEPackages()
>
> 2. When creating dynamic packages/classes/attributes, use structural
> features loaded from persistent versions of EcorePackage.eINSTANCE and
> XMLTypePackage.eINSTANCE.
>
> ....and other subtleties on Teneo. At least I know now how much I don't
> know - this is a comfort spot for me on the learning curve.
>
>
> See updated test cases below:
>
> Thanks once more,
> -Mdu
> -----------xxxxx---------------
>
> public void testPersistDynamicPackage_Case1Step1()
> {
> //--
> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
> final EcorePackage epackage = EcorePackage.eINSTANCE;
> final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;
>
> //--
> EPackage.Registry.INSTANCE.put(epackage.getNsURI(),epackage) ;
> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),xmlpack age);
>
> //--
> EPackage schoolPackage = efactory.createEPackage();
> schoolPackage.setName("SchoolPackage");
> schoolPackage.setNsPrefix("SchoolPackage");
> schoolPackage.setNsURI("http:///www.elver.org/School");
>
> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),scho olPackage);
>
> EClass schoolBookEClass = efactory.createEClass();
> schoolBookEClass.setName("SchoolBook");
> EAttribute level = efactory.createEAttribute();
> level.setName("level");
> level.setEType(epackage.getEInt());
> schoolBookEClass.getEStructuralFeatures().add(level);
> schoolPackage.getEClassifiers().add(schoolBookEClass);
> //--
> HbDataStore emfMDRepoHBS = DbUtil.createEMFRepoHBDS(new
> EPackage[]{epackage,xmlpackage,schoolPackage});
> SessionFactory emfRepoSessionFactory =
> emfMDRepoHBS.getSessionFactory();
> Session emfRepoSession = emfRepoSessionFactory.openSession();
> Transaction txEmfRepo = emfRepoSession.getTransaction();
>
> txEmfRepo.begin();
> emfRepoSession.save(epackage);
> emfRepoSession.save(xmlpackage);
> emfRepoSession.save(schoolPackage);
> txEmfRepo.commit();
>
> emfRepoSession.close();
> }
>
> public void testModifyDynamicPackage_Case1Step2()
> {
> //-- Declare factories and built-in packages
> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
> final EcorePackage epackage = EcorePackage.eINSTANCE;
> final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;
>
> //-- Is this necessary?
> EPackage.Registry.INSTANCE.put(epackage.getNsURI(),epackage) ;
> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),xmlpack age);
>
> //-- Initialize Hb DS
> HbDataStore emfMDRepoHBS = DbUtil.createEMFRepoHBDS(new
> EPackage[]{epackage,xmlpackage});
> SessionFactory emfRepoSessionFactory =
> emfMDRepoHBS.getSessionFactory();
> Session emfRepoSession = emfRepoSessionFactory.openSession();
> Transaction txEmfRepo = emfRepoSession.getTransaction();
>
> //-- Load Hb stored Ecore and XMLType packages
> txEmfRepo.begin();
> Query query = emfRepoSession.createQuery("from EPackage WHERE
> name='ecore'");
> EPackage eCorePkg = (EPackage)query.list().get(0);
> Registry.INSTANCE.put(eCorePkg.getNsURI(),eCorePkg);
>
> query = emfRepoSession.createQuery("from EPackage WHERE
> name='type'");
> EPackage xmlTypePkg = (EPackage)query.list().get(0);
> Registry.INSTANCE.put(xmlTypePkg.getNsURI(),xmlTypePkg);
>
> query = emfRepoSession.createQuery("from EPackage WHERE
> name='SchoolPackage'");
> EPackage schPkg = (EPackage)query.list().get(0);
> Registry.INSTANCE.put(schPkg.getNsURI(),schPkg);
> txEmfRepo.commit();
>
>
> //-- Re-initialize Hb DS: Set packages with Teneo - use
> un-persistent Ecore/XMLType
> //-- packages.
> emfMDRepoHBS = DbUtil.createEMFRepoHBDS(new
> EPackage[]{epackage,xmlpackage,schPkg});
> emfRepoSessionFactory = emfMDRepoHBS.getSessionFactory();
> emfRepoSession = emfRepoSessionFactory.openSession();
>
> txEmfRepo = emfRepoSession.getTransaction();
>
> //-- Lookup dynamic types and packages for modification
> txEmfRepo.begin();
> query = emfRepoSession.createQuery("from EDataType WHERE
> name='EInt'");
> EDataType eIntDT = (EDataType)query.list().get(0);
>
> query = emfRepoSession.createQuery("from EPackage WHERE
> name='SchoolPackage'");
> EPackage schoolPkg = (EPackage)query.list().get(0);
> EPackage.Registry.INSTANCE.put(schoolPkg.getNsURI(),schoolPk g);
>
> //-- Create custom class/attribute to add package
> EClass schoolBookEClass = efactory.createEClass();
> schoolBookEClass.setName("SchoolBook4");
> emfRepoSession.save(schoolBookEClass);
> EAttribute level = efactory.createEAttribute();
> level.setName("level4");
> level.setEType(eIntDT); //Use DataType loaded from Hb DS
> schoolBookEClass.getEStructuralFeatures().add(level);
> schoolPkg.getEClassifiers().add(schoolBookEClass);
> emfRepoSession.save(schoolPkg);
> txEmfRepo.commit();
>
> emfRepoSession.close();
> }
>
> public void testCreateDynamicPackageEObjects_Case1Step3()
> {
> //-- Declare factories and built-in packages
> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
> final EcorePackage epackage = EcorePackage.eINSTANCE;
> final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;
>
> //-- Is this necessary?
> EPackage.Registry.INSTANCE.put(epackage.getNsURI(),epackage) ;
> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),xmlpack age);
>
> //-- Initialize Meatadata Hb DS
> HbDataStore emfMDRepoHBS = DbUtil.createEMFRepoHBDS(new
> EPackage[]{epackage,xmlpackage});
> SessionFactory emfRepoSessionFactory =
> emfMDRepoHBS.getSessionFactory();
> Session emfRepoSession = emfRepoSessionFactory.openSession();
> Transaction txEmfRepo = emfRepoSession.getTransaction();
>
> //-- Load Hb stored Ecore and XMLType packages
> txEmfRepo.begin();
> Query query = emfRepoSession.createQuery("from EPackage WHERE
> name='SchoolPackage'");
> EPackage schPkg = (EPackage)query.list().get(0);
> Registry.INSTANCE.put(schPkg.getNsURI(),schPkg);
> txEmfRepo.commit();
>
> //-- Create eobjects
> HbDataStore sbMDRepoHBS = DbUtil.createSBMDRepoHBDS(new
> EPackage[]{epackage,xmlpackage,schPkg});
> SessionFactory sbRepoSessionFactory =
> sbMDRepoHBS.getSessionFactory();
> Session sbRepoSession = sbRepoSessionFactory.openSession();
> Transaction txSbRepo = sbRepoSession.getTransaction();
>
> txSbRepo.begin();
>
> EClass schoolBookEClass =
> (EClass)schPkg.getEClassifier("SchoolBook");
> EAttribute schoolBookLevelEAttr =
> (EAttribute)schoolBookEClass.getEStructuralFeature("level");
> EObject englishSchoolBook =
> schPkg.getEFactoryInstance().create((EClass)schoolBookEClass );
> englishSchoolBook.eSet(schoolBookLevelEAttr, 2);
>
> sbRepoSession.save(englishSchoolBook);
>
> txSbRepo.commit();
>
> sbRepoSession.close();
> }
>
> ------------xxxxxxxxxxxxxxxx-----------------
> 1. Martin Taal wrote:
>> Hi Mdu,
>> Sorry my confusion/mistake, you also need to store the ecore package
>> and the xmlpackage in the emfMDRepoHBS.
>>
>> gr. Martin
>>
>> Mdu wrote:
>>> Thanks Martin for being patient with this. I have gone a little further:
>>>
>>> SUMMARY:
>>> Error occurs when saving the EDataType of the attribute 'level' below
>>> (see TRACE and ERROR).
>>> BTW- The tables (including ecore_eobject and SchoolBool) are being
>>> created - but obviously no data in them.
>>>
>>> 1. Test Case:
>>> public void testPersistDynamicPackage()
>>> {
>>> //--
>>> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>> final XMLTypePackage xmlpackage = XMLTypePackage.eINSTANCE;
>>> //--
>>> EPackage.Registry.INSTANCE.put(epackage.getNsURI(),epackage) ;
>>>
>>> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),xmlpack age);
>>>
>>> //--
>>> EPackage schoolPackage = efactory.createEPackage();
>>> schoolPackage.setName("SchoolPackage");
>>> schoolPackage.setNsPrefix("SchoolPackage");
>>> schoolPackage.setNsURI("http:///www.elver.org/School");
>>>
>>> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),scho olPackage);
>>> EClass schoolBookEClass = efactory.createEClass();
>>> schoolBookEClass.setName("SchoolBook");
>>> EAttribute level = efactory.createEAttribute();
>>> level.setName("level");
>>> level.setEType(epackage.getEInt());
>>> schoolBookEClass.getEStructuralFeatures().add(level);
>>> schoolPackage.getEClassifiers().add(schoolBookEClass);
>>> //--
>>> HbDataStore emfMDRepoHBS = DbUtil.createEMFRepoHBDS(new
>>> EPackage[]{epackage,xmlpackage});
>>> SessionFactory emfRepoSessionFactory =
>>> emfMDRepoHBS.getSessionFactory();
>>> Session emfRepoSession = emfRepoSessionFactory.openSession();
>>> Transaction txEmfRepo = emfRepoSession.getTransaction();
>>> txEmfRepo.begin();
>>> emfRepoSession.save(schoolPackage);
>>> txEmfRepo.commit();
>>> emfRepoSession.close(); } 2. DEBUG TRACE:
>>> 6059 [main] DEBUG
>>> org.eclipse.emf.teneo.hibernate.mapping.identifier.Identifie rCacheHandler
>>> - ID for object org.eclipse.emf.ecore.impl.EDataTypeImpl not found
>>> in id cache
>>> 6059 [main] DEBUG
>>> org.eclipse.emf.teneo.hibernate.mapping.identifier.Identifie rCacheHandler
>>> - ID for object org.eclipse.emf.ecore.impl.EDataTypeImpl not found
>>> in id cache
>>>
>>>
>>> 3. ERROR (EXCEPTION):
>>> org.hibernate.TransientObjectException: object references an unsaved
>>> transient instance - save the transient instance before flushing:
>>> EClassifier
>>> at
>>> org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUns aved(ForeignKeys.java:242)
>>>
>>> at org.hibernate.type.EntityType.getIdentifier(EntityType.java: 430)
>>> at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java: 265)
>>> at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:61 9)
>>> at
>>> org.hibernate.persister.entity.AbstractEntityPersister.findD irty(AbstractEntityPersister.java:3151)
>>>
>>> at
>>> org.hibernate.event.def.DefaultFlushEntityEventListener.dirt yCheck(DefaultFlushEntityEventListener.java:501)
>>>
>>> at
>>> org.hibernate.event.def.DefaultFlushEntityEventListener.isUp dateNecessary(DefaultFlushEntityEventListener.java:227)
>>>
>>> at
>>> org.hibernate.event.def.DefaultFlushEntityEventListener.onFl ushEntity(DefaultFlushEntityEventListener.java:150)
>>>
>>> at
>>> org.hibernate.event.def.AbstractFlushingEventListener.flushE ntities(AbstractFlushingEventListener.java:219)
>>>
>>> at
>>> org.hibernate.event.def.AbstractFlushingEventListener.flushE verythingToExecutions(AbstractFlushingEventListener.java:99)
>>>
>>> at
>>> org.hibernate.event.def.DefaultFlushEventListener.onFlush(De faultFlushEventListener.java:49)
>>>
>>> at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
>>> at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java :365)
>>> at
>>> org.hibernate.transaction.JDBCTransaction.commit(JDBCTransac tion.java:137)
>>>
>>> at
>>> com.sbide.das.tests.EcoreDTOUtilTestCase2.testPersistDynamic Package(EcoreDTOUtilTestCase2.java:105)
>>>
>>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>> at
>>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce ssorImpl.java:39)
>>>
>>> at
>>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe thodAccessorImpl.java:25)
>>>
>>> at java.lang.reflect.Method.invoke(Method.java:585)
>>> at junit.framework.TestCase.runTest(TestCase.java:164)
>>> at junit.framework.TestCase.runBare(TestCase.java:130)
>>> at junit.framework.TestResult$1.protect(TestResult.java:106)
>>> at junit.framework.TestResult.runProtected(TestResult.java:124)
>>> at junit.framework.TestResult.run(TestResult.java:109)
>>> at junit.framework.TestCase.run(TestCase.java:120)
>>> at
>>> org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestRefer ence.run(JUnit3TestReference.java:130)
>>>
>>> at
>>> org.eclipse.jdt.internal.junit.runner.TestExecution.run(Test Execution.java:38)
>>>
>>> at
>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:460)
>>>
>>> at
>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTe sts(RemoteTestRunner.java:673)
>>>
>>> at
>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(R emoteTestRunner.java:386)
>>>
>>> at
>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main( RemoteTestRunner.java:196)
>>>
>>>
>>>
>>>
>>>
>>> Martin Taal wrote:
>>>> Hi Mdu,
>>>> See comments inline.
>>>>
>>>> Regarding not getting results when reading epackages:
>>>> When you check the database, is the epackage present in the database?
>>>>
>>>> gr. Martin
>>>>
>>>> Mdu wrote:
>>>>> Hi Martin - thanks for taking the time form your well deserved
>>>>> break! See my comments below:
>>>>> Martin Taal wrote:
>>>>>> Hi Mdu,
>>>>>> Create-drop will drop the database when re-initializing the
>>>>>> datastore, so in your case I would use update.
>>>>> I tried my 'update' option - and still have the same problem of a
>>>>> query that returns no epackages after re-initialize; and also got
>>>>> the following errors during re-initalize:
>>>>>
>>>>> 51027 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter
>>>>> table `eobject` add index emodelelement_eannotations
>>>>> (`eannotation_emodelelement_e_id`), add constraint
>>>>> emodelelement_eannotations foreign key
>>>>> (`eannotation_emodelelement_e_id`) references `eobject` (e_id)
>>>>> 51209 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate -
>>>>> Unsuccessful: alter table `eobject` add index
>>>>> emodelelement_eannotations (`eannotation_emodelelement_e_id`), add
>>>>> constraint emodelelement_eannotations foreign key
>>>>> (`eannotation_emodelelement_e_id`) references `eobject` (e_id)
>>>>> 51209 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - Can't
>>>>> create table 'solutionassembly.#sql-1208_1c' (errno: 121)
>>>>>>
>>>> MT>> Does the log earlier show errors when doing create table?
>>>>
>>>>>> Answers:
>>>>>> 1) I did not completely understand the question, if the elv
>>>>>> package changes then new tables/columns need to be created this is
>>>>>> done when (re-)initializing the datastore.
>>>>>> 2) You can use 2 datastores (one for the ecore/xml package and one
>>>>>> for the elv package) if there are no direct references from elv to
>>>>>> ecore. With direct I mean that you did not specify in your model
>>>>>> an ereference which references the EClass type in the ecore
>>>>>> package (or eattribute, or ereference). Re-initializating should
>>>>>> not be a problem with the hibernate update option.
>>>>> This is where my ignorance shows a lot. Here's my understand how
>>>>> what you are suggesting would work:
>>>>>
>>>>> Let DS_Elv be data store for Elv package;
>>>>> DS_Ecore be data store for Ecore and XMLType packages
>>>>> HBDS_Elv be HB data source for DS_Elv
>>>>> HBDS_Ecore be HB data source for DS_Ecore
>>>>> Ssn_Elv be session from HBDS_Elv
>>>>> Ssn_Ecore be session from HBDS_Ecore
>>>>> ecorePkg and xmlTypePkg be persistent packages saved and loaded
>>>>> via Ssn_Ecore
>>>> MT>> What do you mean with Hb data source? The datastore?
>>>>
>>>> It is not necessary to store and retrieve the ecore and xml packages
>>>> from the db as they won't change.
>>>>
>>>> I would say that:
>>>> - The HBDS_Ecore will store instances of the Ecore package (not the
>>>> ecore package itself), the Elv EPackage is an instance of the ecore
>>>> epackage, so the HBDS_Ecore will store the Elv EPackage itself.
>>>> - The HBDS_Elv will store instances of the Elv model (so assume that
>>>> the Elv EPackage has an eClass Teneo, then this store will save an
>>>> EObject which has eClass Teneo).
>>>>
>>>> With this in mind I will react below.
>>>>
>>>>>
>>>>> 1. Create HBDS_Elv:
>>>>> - HBDS_Elv.setEPackages({*}) - should * be ecorePkg and
>>>>> xmlTypePkg from Ssn_Ecore?
>>>>> - HBDS_Elv.initialize()
>>>> MT>> I would say that * is the Elv Package
>>>>
>>>> To initialize the HBDS_Ecore:
>>>> HBDS_Ecore.setEPackages(new EPackage[]{ecorepkg, xmlpkg})
>>>> HBDS_Ecore.initialize();
>>>>
>>>>>
>>>>> 2. Create and save Elv package 'elvPkg':
>>>>> - Obtain Ssn_Elv from HBDS_Elv sessionFactory
>>>>> - Create elvPkg and class 'class1' with 'attr1'
>>>>> - Transactionally call Ssn_Elv.save(elvPkg)
>>>> MT>> Obtain Ssn_Ecore from HBDS_Ecore and call
>>>> Ssn_Ecore.save(elvPkg) (and commit the transaction)
>>>>
>>>>> QUESTION: Doesn't this require persistent ecorePkg and
>>>>> xmlTypePkg implicilty to save 'class1' and 'attr1' i.e. instances
>>>>> from Ssn_Ecore. If so, wouldn't I get errors when I save a elvPkg
>>>>> that implicitly depends on epackages loaded from Ssn_Ecore - a
>>>>> separate session?
>>>>>
>>>>> 3. Read elvPkg back for modification:
>>>>> - HBDS_Elv.setEPackages({**}) - should ** be same ecorePkg and
>>>>> xmlTypePkg from Ssn_Ecore?
>>>>> - HBDS_Elv.initialize()
>>>>> - Create query 'from EPackage where name='Elv'
>>>>> - Assign elvPkg to the result
>>>>> - Add another 'class2' with 'attr2' to elvPkg
>>>>> - Transactionally call Ssn_Elv.save(elvPkg)
>>>>> QUESTION: Same question as above
>>>> MT>> Instead of HBDS_Elv I would you use HBDS_Ecore here.
>>>> After doing Ssn_Ecore.save(elvPkg) re-initialize the HBDS_Elv to
>>>> update the database schema.
>>>>
>>>> Also be aware that there is a global EPackage registry which stores
>>>> mappings from nsuri to EPackages. Also a resourceset has an epackage
>>>> registry which stores this relation. So after reading the elvpackage
>>>> from the database set it in the global epackage registry.
>>>>
>>>>>
>>>>> The only way I could see this working from here is by:
>>>>> Everytime I change elvPkg, I first have to read it; clone it using
>>>>> elvPkgClone = EcoreUtil.copy(); modify it; HBDS_Env.initialize();
>>>>> and Ssn_Elv.save(elvPkgClone).
>>>> MT>> Afaics you don't need to clone, I would use HBDS_Ecore.
>>>>
>>>>>
>>>>> Unless I fix my Hibernate (errno: 121) errors - assuming that's
>>>>> what preventing 'update' from working; this is my only choice. I
>>>>> also realize that I'm starting to meddle 'model evolution' issues
>>>>> here as well - that is how to evolve metamodels to be always in
>>>>> sync with their underlying models. I noticed that this has been
>>>>> discussed on CDO forums a bit.
>>>> MT>> Yes it is also a topic for CDO. The update of the database
>>>> schema is one part of the story. In the end changing relational
>>>> database schemes on the fly is a touchy thing, on the other hand
>>>> adding nullable columns and new tables should work fine.
>>>>
>>>>>
>>>>> Regards,
>>>>> -Mdu
>>>>>
>>>>>>
>>>>>> gr. Martin
>>>>>>
>>>>>> Mdu wrote:
>>>>>>> Thanks Martin,
>>>>>>> I turned on debugging and used create-drop option. Part of the
>>>>>>> log shows:
>>>>>>>
>>>>>>>
>>>>>>> 4476 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>>> table if exists `eannotation_contents`
>>>>>>> 4479 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>>> table if exists `eannotation_references`
>>>>>>> 4480 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>>> table if exists `eclass_esupertypes`
>>>>>>> 4483 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>>> table if exists `ecore_eobject`
>>>>>>> 4485 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>>> table if exists `eoperation_eexceptions`
>>>>>>> 4486 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>>> table if exists `ereference_ekeys`
>>>>>>> 4488 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop
>>>>>>> table if exists details
>>>>>>>
>>>>>>>
>>>>>>> Questions:
>>>>>>> 1. Since I'm neither changing EcorePackage('ecore') nor
>>>>>>> XMLTypePackage('type') - only modifying package 'elv', why do I
>>>>>>> have to re-initialize HbDataStore?
>>>>>>>
>>>>>>>
>>>>>>> 2. It makes sense to store EcorePackage and XMLTypePackage in a
>>>>>>> separate HBDS as the are static, I still need to update custom
>>>>>>> EPackages (e.g. 'elv') which refer to these two packages. How do
>>>>>>> I accomplish this without running into re-initialize issues.
>>>>>>>
>>>>>>>
>>>>>>> Rgds,
>>>>>>> -Mdu
>>>>>>> Martin Taal wrote:
>>>>>>>> Hi Mdu,
>>>>>>>> I see that you add the xmltypepackage here.
>>>>>>>>
>>>>>>>> Is the database empty after the re-initialize? (so is the issue
>>>>>>>> with the querying or with the database being cleared during
>>>>>>>> reinitialize. In the last case, do you have hibernate.properties
>>>>>>>> in the root of the classpath which set the update database
>>>>>>>> option of hibernate to create-drop?
>>>>>>>>
>>>>>>>> Can you enable log4j logging for hibernate
>>>>>>>> (http://www.elver.org/hibernate/samples/log4j.properties) to see
>>>>>>>> what hibernate does during the re-initialize?
>>>>>>>>
>>>>>>>> Note that hibernate has some limitations when updating an
>>>>>>>> existing db schema, it will create tables, columns, foreign
>>>>>>>> keys, but afaik no indexes are created (if you have separate
>>>>>>>> index annotations).
>>>>>>>>
>>>>>>>> Btw, instead of using one datastore for both the package itself
>>>>>>>> and the data you can also use a different datastore for the
>>>>>>>> ecorepackage/xmltypepackage (storing the epackages) and the data
>>>>>>>> (instances of the eclasses of elvpackage). Unless you have
>>>>>>>> references from your data to the model then one datastore is
>>>>>>>> required.
>>>>>>>>
>>>>>>>> Hope this helps a bit.
>>>>>>>>
>>>>>>>> I am on holiday so my answers take a bit longer than normal.
>>>>>>>>
>>>>>>>> gr. Martin
>>>>>>>>
>>>>>>>> Mdu wrote:
>>>>>>>>> Hi All,
>>>>>>>>> I've gone a little further with a test case. I first run test
>>>>>>>>> case testStoreEPackages() to create Ecore, XML, and new
>>>>>>>>> EPackage 'elv'. I'm having a problem in
>>>>>>>>> testAddClassToElvEPackage(): please see a block comment with
>>>>>>>>> PROBLEM: section.
>>>>>>>>>
>>>>>>>>> Thanks,
>>>>>>>>> -Mdu
>>>>>>>>>
>>>>>>>>> package com.sbide.das.tests;
>>>>>>>>>
>>>>>>>>> import java.util.List;
>>>>>>>>> import java.util.Properties;
>>>>>>>>>
>>>>>>>>> import junit.framework.TestCase;
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> import org.eclipse.emf.ecore.EAttribute;
>>>>>>>>> import org.eclipse.emf.ecore.EClass;
>>>>>>>>> import org.eclipse.emf.ecore.EObject;
>>>>>>>>> import org.eclipse.emf.ecore.EPackage;
>>>>>>>>> import org.eclipse.emf.ecore.EReference;
>>>>>>>>> import org.eclipse.emf.ecore.EcoreFactory;
>>>>>>>>> import org.eclipse.emf.ecore.EcorePackage;
>>>>>>>>> import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
>>>>>>>>> import org.eclipse.emf.teneo.hibernate.HbDataStore;
>>>>>>>>> import org.eclipse.emf.teneo.hibernate.HbHelper;
>>>>>>>>> import org.hibernate.Query;
>>>>>>>>> import org.hibernate.Session;
>>>>>>>>> import org.hibernate.SessionFactory;
>>>>>>>>> import org.hibernate.Transaction;
>>>>>>>>> import org.hibernate.cfg.Environment;
>>>>>>>>>
>>>>>>>>> import com.irg.eaf.common.util.ApplicationProperties;
>>>>>>>>> import com.sbide.das.EnvironmentConstants;
>>>>>>>>>
>>>>>>>>> public class DynamicEPackageTestCase extends TestCase {
>>>>>>>>>
>>>>>>>>> public void setUp()
>>>>>>>>> {
>>>>>>>>> }
>>>>>>>>>
>>>>>>>>> public void testStoreEPackages()
>>>>>>>>> {
>>>>>>>>> final EcoreFactory efactory =
>>>>>>>>> EcoreFactory.eINSTANCE;
>>>>>>>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>>>>>>>> final XMLTypePackage xmlpackage =
>>>>>>>>> XMLTypePackage.eINSTANCE;
>>>>>>>>> EPackage.Registry.INSTANCE.put(epackage.getNsURI(), epackage);
>>>>>>>>> EPackage.Registry.INSTANCE.put(xmlpackage.getNsURI(),
>>>>>>>>> xmlpackage);
>>>>>>>>> // create the SchoolBook EClass
>>>>>>>>> EClass schoolBookEClass = efactory.createEClass();
>>>>>>>>> schoolBookEClass.setName("SchoolBook");
>>>>>>>>> // create a new attribute for this EClass
>>>>>>>>> EAttribute level = efactory.createEAttribute();
>>>>>>>>> level.setName("level");
>>>>>>>>> level.setEType(epackage.getEInt());
>>>>>>>>> schoolBookEClass.getEStructuralFeatures().add(level);
>>>>>>>>>
>>>>>>>>> // create a course
>>>>>>>>> EClass courseEClass = efactory.createEClass();
>>>>>>>>> courseEClass.setName("Course");
>>>>>>>>> // give the Course a name
>>>>>>>>> EAttribute courseName = efactory.createEAttribute();
>>>>>>>>> courseName.setName("courseName");
>>>>>>>>> courseName.setEType(epackage.getEString());
>>>>>>>>> courseEClass.getEStructuralFeatures().add(courseName);
>>>>>>>>> // A course always uses one SchoolBook
>>>>>>>>> EReference courseBook = efactory.createEReference();
>>>>>>>>> courseBook.setName("courseBook");
>>>>>>>>> courseBook.setEType(schoolBookEClass);
>>>>>>>>> courseBook.setContainment(false);
>>>>>>>>> courseEClass.getEStructuralFeatures().add(courseBook);
>>>>>>>>> // Create a new EPackage and add the new EClasses
>>>>>>>>> EPackage schoolPackage = efactory.createEPackage();
>>>>>>>>> schoolPackage.setName("elv");
>>>>>>>>> schoolPackage.setNsPrefix("elv");
>>>>>>>>> schoolPackage.setNsURI("http:///www.elver.org/School");
>>>>>>>>> schoolPackage.getEClassifiers().add(courseEClass);
>>>>>>>>> schoolPackage.getEClassifiers().add(schoolBookEClass);
>>>>>>>>>
>>>>>>>>> EPackage.Registry.INSTANCE.put(schoolPackage.getNsURI(),
>>>>>>>>> schoolPackage);
>>>>>>>>> // Now reset the epackages in the datastore
>>>>>>>>> HbDataStore hbds = createHBDS(new
>>>>>>>>> EPackage[]{epackage,schoolPackage,xmlpackage});
>>>>>>>>> // print the hibernate.hbm.xml for demo purposes
>>>>>>>>> System.err.println(hbds.getMappingXML());
>>>>>>>>> // and create a course
>>>>>>>>> EObject course =
>>>>>>>>> schoolPackage.getEFactoryInstance().create(courseEClass);
>>>>>>>>> course.eSet(courseName, "Dutch Literature Level 1");
>>>>>>>>> final SessionFactory sessionFactory =
>>>>>>>>> hbds.getSessionFactory();
>>>>>>>>> // now persist them all
>>>>>>>>> Session session = sessionFactory.openSession();
>>>>>>>>> Transaction tx = session.getTransaction();
>>>>>>>>> tx.begin();
>>>>>>>>> session.save(xmlpackage);
>>>>>>>>> session.save(epackage);
>>>>>>>>> session.save(schoolPackage);
>>>>>>>>> session.save(course);
>>>>>>>>> tx.commit();
>>>>>>>>>
>>>>>>>>> session.close();
>>>>>>>>> }
>>>>>>>>> public void testAddClassToElvEPackage()
>>>>>>>>> {
>>>>>>>>>
>>>>>>>>> final EcoreFactory efactory = EcoreFactory.eINSTANCE;
>>>>>>>>> final EcorePackage epackage = EcorePackage.eINSTANCE;
>>>>>>>>> // Now reset the epackages in the datastore
>>>>>>>>> HbDataStore hbds = createHBDS(new EPackage[]{epackage});
>>>>>>>>>
>>>>>>>>> SessionFactory sessionFactory = hbds.getSessionFactory();
>>>>>>>>> /**
>>>>>>>>> * Read all relevant epackages to use them for later
>>>>>>>>> calling setPackages
>>>>>>>>> */
>>>>>>>>> Session session = sessionFactory.openSession();
>>>>>>>>> Transaction tx = session.getTransaction();
>>>>>>>>> tx.begin();
>>>>>>>>> Query qry = session.createQuery("from EPackage where
>>>>>>>>> name='elv'");
>>>>>>>>> List<EPackage> pkgs = qry.list();
>>>>>>>>> EPackage elvPkg = pkgs.get(0);
>>>>>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>>>>>>>> qry = session.createQuery("from EPackage where
>>>>>>>>> name='ecore'");
>>>>>>>>> pkgs = qry.list();
>>>>>>>>> EPackage ecorePkg = pkgs.get(0);
>>>>>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(), ecorePkg);
>>>>>>>>> qry = session.createQuery("from EPackage where
>>>>>>>>> name='type'");
>>>>>>>>> pkgs = qry.list();
>>>>>>>>> EPackage typePkg = pkgs.get(0);
>>>>>>>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>>>>>>>> typePkg); System.out.println("Collection Count:
>>>>>>>>> "+session.getStatistics().getCollectionCount());
>>>>>>>>> System.out.println("Entity Count:
>>>>>>>>> "+session.getStatistics().getEntityCount());
>>>>>>>>> elvPkg.getEClassifiers().size();
>>>>>>>>> tx.commit();
>>>>>>>>> /**
>>>>>>>>> * Re-initialize store
>>>>>>>>> */
>>>>>>>>> hbds.setEPackages((new
>>>>>>>>> EPackage[]{ecorePkg,elvPkg,typePkg}));
>>>>>>>>> hbds.initialize(); sessionFactory =
>>>>>>>>> hbds.getSessionFactory(); session =
>>>>>>>>> sessionFactory.openSession();
>>>>>>>>> tx = session.getTransaction();
>>>>>>>>> /**
>>>>>>>>> * Re-read epackages - if they are not re-read, they'd
>>>>>>>>> be still attached to
>>>>>>>>> * the old session before we called .setEPackages() and
>>>>>>>>> .initialize():
>>>>>>>>> *
>>>>>>>>> * PROBLEM: Each EPackage query below returns empty
>>>>>>>>> list (qry.list()).
>>>>>>>>> * QUESTION: 1. I expected these to be still in the
>>>>>>>>> database - why is the query
>>>>>>>>> * returning empty results?
>>>>>>>>> */
>>>>>>>>> tx.begin();
>>>>>>>>> qry = session.createQuery("from EPackage where
>>>>>>>>> name='type'");
>>>>>>>>> pkgs = qry.list();
>>>>>>>>> typePkg = pkgs.get(0);
>>>>>>>>> EPackage.Registry.INSTANCE.put(typePkg.getNsURI(),
>>>>>>>>> typePkg); qry = session.createQuery("from
>>>>>>>>> EPackage where name='ecore'");
>>>>>>>>> pkgs = qry.list();
>>>>>>>>> ecorePkg = pkgs.get(0);
>>>>>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(),
>>>>>>>>> ecorePkg); qry = session.createQuery("from EPackage
>>>>>>>>> where name='elv'");
>>>>>>>>> pkgs = qry.list();
>>>>>>>>> elvPkg = pkgs.get(0);
>>>>>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(),
>>>>>>>>> elvPkg); tx.commit();
>>>>>>>>> /**
>>>>>>>>> * Fetch all three epackages for reference and
>>>>>>>>> modification
>>>>>>>>> */
>>>>>>>>> tx.begin();
>>>>>>>>> System.out.println("Collection Count:
>>>>>>>>> "+session.getStatistics().getCollectionCount());
>>>>>>>>> System.out.println("Entity Count:
>>>>>>>>> "+session.getStatistics().getEntityCount());
>>>>>>>>> qry = session.createQuery("from EPackage where
>>>>>>>>> name='ecore'");
>>>>>>>>> pkgs = qry.list();
>>>>>>>>> ecorePkg = pkgs.get(0);
>>>>>>>>> EPackage.Registry.INSTANCE.put(ecorePkg.getNsURI(),
>>>>>>>>> ecorePkg); qry = session.createQuery("from
>>>>>>>>> EPackage where name='elv'");
>>>>>>>>> pkgs = qry.list();
>>>>>>>>> elvPkg = pkgs.get(0);
>>>>>>>>> EPackage.Registry.INSTANCE.put(elvPkg.getNsURI(), elvPkg);
>>>>>>>>> tx.commit(); tx.begin();
>>>>>>>>> EClass testClass1 = efactory.createEClass();
>>>>>>>>> testClass1.setName("TestClass1");
>>>>>>>>> EAttribute attr1 = efactory.createEAttribute();
>>>>>>>>> attr1.setName("intAttr");
>>>>>>>>> attr1.setEType(epackage.getEInt());
>>>>>>>>>
>>>>>>>>> testClass1.getEStructuralFeatures().add(attr1);
>>>>>>>>> session.save(attr1);
>>>>>>>>> session.save(testClass1);
>>>>>>>>> elvPkg.getEClassifiers().add(testClass1);
>>>>>>>>> session.saveOrUpdate(typePkg);
>>>>>>>>> session.saveOrUpdate(ecorePkg);
>>>>>>>>> session.saveOrUpdate(elvPkg); tx.commit();
>>>>>>>>>
>>>>>>>>> session.close();
>>>>>>>>> } public static HbDataStore createHBDS(EPackage[]
>>>>>>>>> epackages)
>>>>>>>>> { HbDataStore hbds =
>>>>>>>>> (HbDataStore)HbHelper.INSTANCE.createRegisterDataStore(Appli cationProperties.get(EnvironmentConstants.TENEO_DS_NAME));
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> final Properties props = new Properties();
>>>>>>>>> props.setProperty(Environment.DRIVER,
>>>>>>>>> ApplicationProperties.get(Environment.DRIVER));
>>>>>>>>> props.setProperty(Environment.USER,
>>>>>>>>> ApplicationProperties.get(Environment.USER));
>>>>>>>>> props.setProperty(Environment.URL,
>>>>>>>>> ApplicationProperties.get(Environment.URL));
>>>>>>>>> props.setProperty(Environment.PASS,
>>>>>>>>> ApplicationProperties.get(Environment.PASS));
>>>>>>>>> props.setProperty(Environment.DIALECT,
>>>>>>>>> ApplicationProperties.get(Environment.DIALECT));
>>>>>>>>> hbds.setProperties(props);
>>>>>>>>> hbds.setEPackages(epackages);
>>>>>>>>> hbds.initialize();
>>>>>>>>> return hbds;
>>>>>>>>> } }
>>>>>>>>> Mdu wrote:
>>>>>>>>>> Hi,
>>>>>>>>>> I'm just about given up with the previous trial-and-error in
>>>>>>>>>> working with EPackages stored via teneo. Is there an example
>>>>>>>>>> (I also looked through Teneo test cases) that demonstrates the
>>>>>>>>>> following:
>>>>>>>>>>
>>>>>>>>>> 1. Create a dynamic (empty) package and save it using Teneo
>>>>>>>>>> 'session.save()'.
>>>>>>>>>>
>>>>>>>>>> 2. Load the package in 1. properly (registration and all) and
>>>>>>>>>> add a simple EClass to it; save it back to using
>>>>>>>>>> 'session.save()'.
>>>>>>>>>>
>>>>>>>>>> 3. Load the package again and use it to create an EOBject from
>>>>>>>>>> a class added in 2.; and save the newly created EObject.
>>>>>>>>>>
>>>>>>>>>> 4. Do 1., 2., and 3. in separate test cases?
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> I'm using the recent version of Teneo (1.0.1) and dependent
>>>>>>>>>> EMF libraries.
>>>>>>>>>> Any help will be much appreciated!
>>>>>>>>>>
>>>>>>>>>> Rgds,
>>>>>>>>>> -Mdu
>>>>>>>>
>>>>>>>>
>>>>>>
>>>>>>
>>>>
>>>>
>>
>>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Previous Topic:eIsSet Do not behave the same things between EStoreEObjectImpl and EObjectImpl.
Next Topic:Wired namespace issue
Goto Forum:
  


Current Time: Thu Sep 19 17:06:48 GMT 2024

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

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

Back to the top