|
Re: [CDO] Programmatically get the DB mapping [message #794291 is a reply to message #788790] |
Thu, 09 February 2012 05:25 |
|
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
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 |
Stefan Winkler 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 #1059652 is a reply to message #1059612] |
Tue, 21 May 2013 06:13 |
|
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!
Cheers
/Eike
----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper
|
|
|
|
Powered by
FUDForum. Page generated in 0.02236 seconds