Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [CDO] Programmatically get the DB mapping
[CDO] Programmatically get the DB mapping [message #788790] Thu, 02 February 2012 08:17 Go to next message
Christophe Moine is currently offline Christophe MoineFriend
Messages: 14
Registered: July 2009
Junior Member
Hi everybody,

Is there a way the consult the DB mapping programmatically ?

My needs is to know:
- What is the table name for a given EClass ?
- What is the column name for the given EStructuralFeature ?

Thanks in adavnce.

Re: [CDO] Programmatically get the DB mapping [message #794291 is a reply to message #788790] Thu, 09 February 2012 05:25 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6682
Registered: July 2009
Senior Member
Hi Christophe,

I apologize for the late reply.

Comments below...


Am 02.02.2012 09:17, schrieb Christophe Moine:
> Hi everybody,
>
> Is there a way the consult the DB mapping programmatically ?
Sure, but not on the client. On the CDO server you can ask:

IRepository repository = getRepository();

EClass eClass = getModel1Package().getAddress();

IDBStore store = (IDBStore)repository.getStore();
IMappingStrategy mappingStrategy = store.getMappingStrategy();
IClassMapping classMapping = mappingStrategy.getClassMapping(eClass);

IDBTable table = classMapping.getDBTables().get(0);
System.out.println(eClass.getName() + " --> " + table);

for (ITypeMapping valueMapping : classMapping.getValueMappings())
{
System.out.println(" " + valueMapping.getFeature().getName() + " --> " + valueMapping.getField());
}

The output will be:

Address --> model1_Address
name --> name
street --> street
city --> city
------------------------- END -------------------------

All columns (fields), including the cdo system ones, can be listed with:

for (IDBField field : table.getFields())
{
System.out.println(" " + field);
}

The output will be:

model1_Address
cdo_id
cdo_version
cdo_created
cdo_revised
cdo_resource
cdo_container
cdo_feature
name
street
city

If you want to use this information at the client-side you need to write your own Net4j mini protocol.
http://www.eclipse.org/cdo/documentation/presentations/DemoCampBerlin_2009/Net4j-Presentation.pdf gives hints on how to
do this.

Cheers
/Eike

----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper


Re: [CDO] Programmatically get the DB mapping [message #794650 is a reply to message #794291] Thu, 09 February 2012 14:34 Go to previous messageGo to next message
Stefan Winkler is currently offline Stefan WinklerFriend
Messages: 307
Registered: July 2009
Location: Germany
Senior Member
Hi Christophe,

comments below.

Am 09.02.12 06:25, schrieb Eike Stepper:
> Hi Christophe,
>
> I apologize for the late reply.

me too :-(

> Comments below...
>
>
> Am 02.02.2012 09:17, schrieb Christophe Moine:
>> Hi everybody,
>>
>> Is there a way the consult the DB mapping programmatically ?

Other that the comment of Eike, you can also specify the table names for
EClasses as an EAnnotation.

The following code is from DBAnnotationsTest.java:

private void addTableNameAnnotation(EPackage model1, String value)
{
EAnnotation annotation = EcoreFactory.eINSTANCE.createEAnnotation();
annotation.setSource("http://www.eclipse.org/CDO/DBStore");
annotation.getDetails().put("tableName", value);

EClass category = (EClass)model1.getEClassifier("Category");
category.getEAnnotations().add(annotation);
}

private void addColumnNameAnnotation(EPackage model1, String value)
{
EAnnotation annotation = EcoreFactory.eINSTANCE.createEAnnotation();
annotation.setSource("http://www.eclipse.org/CDO/DBStore");
annotation.getDetails().put("columnName", value);

EClass category = (EClass)model1.getEClassifier("Category");
EStructuralFeature element =
category.getEStructuralFeature(Model1Package.CATEGORY__NAME);
element.getEAnnotations().add(annotation);
}


Cheers,
Stefan


> Sure, but not on the client. On the CDO server you can ask:
>
> IRepository repository = getRepository();
>
> EClass eClass = getModel1Package().getAddress();
>
> IDBStore store = (IDBStore)repository.getStore();
> IMappingStrategy mappingStrategy = store.getMappingStrategy();
> IClassMapping classMapping = mappingStrategy.getClassMapping(eClass);
>
> IDBTable table = classMapping.getDBTables().get(0);
> System.out.println(eClass.getName() + " --> " + table);
>
> for (ITypeMapping valueMapping : classMapping.getValueMappings())
> {
> System.out.println(" " + valueMapping.getFeature().getName() + " --> " +
> valueMapping.getField());
> }
>
> The output will be:
>
> Address --> model1_Address
> name --> name
> street --> street
> city --> city
> ------------------------- END -------------------------
>
> All columns (fields), including the cdo system ones, can be listed with:
>
> for (IDBField field : table.getFields())
> {
> System.out.println(" " + field);
> }
>
> The output will be:
>
> model1_Address
> cdo_id
> cdo_version
> cdo_created
> cdo_revised
> cdo_resource
> cdo_container
> cdo_feature
> name
> street
> city
>
> If you want to use this information at the client-side you need to write
> your own Net4j mini protocol.
> http://www.eclipse.org/cdo/documentation/presentations/DemoCampBerlin_2009/Net4j-Presentation.pdf
> gives hints on how to do this.
>
> Cheers
> /Eike
>
> ----
> http://www.esc-net.de
> http://thegordian.blogspot.com
> http://twitter.com/eikestepper
>
>
Re: [CDO] Programmatically get the DB mapping [message #794652 is a reply to message #794291] Thu, 09 February 2012 14:39 Go to previous messageGo to next message
Christophe Moine is currently offline Christophe MoineFriend
Messages: 14
Registered: July 2009
Junior Member
Hello Eike and Stefan,

thank you very much for your answers, unfortunatly I would like to know the mapping on the client Sad Stefan I guess your solution is to run your code on the server as well ?

By the way, is there any plans to access the IStore on the client side ? Or this is at the opposite of the CDO architecture ?

Regards,

Christophe.

[Updated on: Thu, 09 February 2012 14:49]

Report message to a moderator

Re: [CDO] Programmatically get the DB mapping [message #794670 is a reply to message #794652] Thu, 09 February 2012 14:58 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6682
Registered: July 2009
Senior Member
Am 09.02.2012 15:39, schrieb Christophe Moine:
> Hello Eike,
>
> thank you very much for your answer, unfortunatly I would like to know the mapping on the client :(
>
> By the way, is there any plans to access the IStore on the client side ? Or this is at the opposite of the CDO
> architecture ?
You can only access the IStore directly if the repository is embedded into the client. Otherwise you'll need some kind
of remoting technology.

Cheers
/Eike

----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper


Re: [CDO] Programmatically get the DB mapping [message #1059612 is a reply to message #794291] Mon, 20 May 2013 19:57 Go to previous messageGo to next message
Kyle B is currently offline Kyle BFriend
Messages: 14
Registered: April 2013
Junior Member
I apologize for reviving an old thread, but I am having an issue running the suggested code.

When I attempt to run :
EClass eClass = getModel1Package().getAddress();

IDBStore store = (IDBStore)repository.getStore();
IMappingStrategy mappingStrategy = store.getMappingStrategy();
IClassMapping classMapping = mappingStrategy.getClassMapping(eClass);


I am getting an IllegalStateException:
Caused by: java.lang.IllegalStateException: session == null
	at org.eclipse.emf.cdo.server.StoreThreadLocal.getSession(StoreThreadLocal.java:62)
	at org.eclipse.emf.cdo.server.StoreThreadLocal.getAccessor(StoreThreadLocal.java:87)
	at org.eclipse.emf.cdo.server.internal.db.MetaDataManager.getMetaID(MetaDataManager.java:88)
	at org.eclipse.emf.cdo.server.internal.db.mapping.AbstractMappingStrategy.getUniqueID(AbstractMappingStrategy.java:410)
	at org.eclipse.emf.cdo.server.internal.db.mapping.AbstractMappingStrategy.getTableName(AbstractMappingStrategy.java:323)
	at org.eclipse.emf.cdo.server.internal.db.mapping.horizontal.AbstractHorizontalClassMapping.initTable(AbstractHorizontalClassMapping.java:115)
	at org.eclipse.emf.cdo.server.internal.db.mapping.horizontal.AbstractHorizontalClassMapping.<init>(AbstractHorizontalClassMapping.java:105)
	at org.eclipse.emf.cdo.server.internal.db.mapping.horizontal.HorizontalAuditClassMapping.<init>(HorizontalAuditClassMapping.java:95)
	at org.eclipse.emf.cdo.server.internal.db.mapping.horizontal.HorizontalAuditMappingStrategy.doCreateClassMapping(HorizontalAuditMappingStrategy.java:49)
	at org.eclipse.emf.cdo.server.internal.db.mapping.AbstractMappingStrategy.createClassMapping(AbstractMappingStrategy.java:506)
	at org.eclipse.emf.cdo.server.internal.db.mapping.AbstractMappingStrategy.getClassMapping(AbstractMappingStrategy.java:553)


I am using CDO 4.2.0.v20121114-0954.

My clients are able to connect to the server and retrieve/send data to the database. Any suggestions?


Thanks!
Re: [CDO] Programmatically get the DB mapping [message #1059652 is a reply to message #1059612] Tue, 21 May 2013 06:13 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6682
Registered: July 2009
Senior Member
Am 20.05.2013 21:57, schrieb Kyle B:
> I apologize for reviving an old thread, but I am having an issue running the suggested code.
> When I attempt to run :
> EClass eClass = getModel1Package().getAddress();
>
> IDBStore store = (IDBStore)repository.getStore();
> IMappingStrategy mappingStrategy = store.getMappingStrategy();
> IClassMapping classMapping = mappingStrategy.getClassMapping(eClass);
>
> I am getting an IllegalStateException:
> Caused by: java.lang.IllegalStateException: session == null
> at org.eclipse.emf.cdo.server.StoreThreadLocal.getSession(StoreThreadLocal.java:62)
> at org.eclipse.emf.cdo.server.StoreThreadLocal.getAccessor(StoreThreadLocal.java:87)
Many methods in a repository need an IStoreAccessor that encapsulates backend-specific connection information such as a
java.sql.Connection for relational databases. This store accessor is kept in the StoreThreadLocal class. All code that
is executed in the scope of client requests receives such a store accessor (through an ISession) in CDOServerReadIndication:

protected void execute(BufferInputStream in, BufferOutputStream out) throws Exception
{
try
{
InternalSession session = getSession();
StoreThreadLocal.setSession(session);
super.execute(in, out);
}
finally
{
StoreThreadLocal.release();
}
}

If you call server code without going through a network request you need to register a store accessor yourself. In
Repository.java there's an example:

protected void readPackageUnits()
{
IStoreAccessor reader = store.getReader(null);
StoreThreadLocal.setAccessor(reader);

try
{
Collection<InternalCDOPackageUnit> packageUnits = reader.readPackageUnits();
for (InternalCDOPackageUnit packageUnit : packageUnits)
{
packageRegistry.putPackageUnit(packageUnit);
}
}
finally
{
StoreThreadLocal.release();
}
}

Does that help?

Cheers
/Eike

----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper


> at org.eclipse.emf.cdo.server.internal.db.MetaDataManager.getMetaID(MetaDataManager.java:88)
> at
> org.eclipse.emf.cdo.server.internal.db.mapping.AbstractMappingStrategy.getUniqueID(AbstractMappingStrategy.java:410)
> at
> org.eclipse.emf.cdo.server.internal.db.mapping.AbstractMappingStrategy.getTableName(AbstractMappingStrategy.java:323)
> at
> org.eclipse.emf.cdo.server.internal.db.mapping.horizontal.AbstractHorizontalClassMapping.initTable(AbstractHorizontalClassMapping.java:115)
> at
> org.eclipse.emf.cdo.server.internal.db.mapping.horizontal.AbstractHorizontalClassMapping.<init>(AbstractHorizontalClassMapping.java:105)
> at
> org.eclipse.emf.cdo.server.internal.db.mapping.horizontal.HorizontalAuditClassMapping.<init>(HorizontalAuditClassMapping.java:95)
> at
> org.eclipse.emf.cdo.server.internal.db.mapping.horizontal.HorizontalAuditMappingStrategy.doCreateClassMapping(HorizontalAuditMappingStrategy.java:49)
> at
> org.eclipse.emf.cdo.server.internal.db.mapping.AbstractMappingStrategy.createClassMapping(AbstractMappingStrategy.java:506)
> at
> org.eclipse.emf.cdo.server.internal.db.mapping.AbstractMappingStrategy.getClassMapping(AbstractMappingStrategy.java:553)
>
> I am using CDO 4.2.0.v20121114-0954.
>
> My clients are able to connect to the server and retrieve/send data to the database. Any suggestions?
>
>
> Thanks!


Re: [CDO] Programmatically get the DB mapping [message #1059726 is a reply to message #1059652] Tue, 21 May 2013 12:11 Go to previous message
Kyle B is currently offline Kyle BFriend
Messages: 14
Registered: April 2013
Junior Member
Thank you Sir. Works perfectly now.
Previous Topic:Automatic population of transient attributes
Next Topic:Customized Edit and Editor generated code that suppress some model objects
Goto Forum:
  


Current Time: Fri Apr 19 04:38:36 GMT 2024

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

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

Back to the top